Rabie
Rabie

Reputation: 1

Updating Table in asp.net

protected void Button1_Click(object sender, EventArgs e)
{
    if(FileUpload1.HasFile)
    {
        int Id = Convert.ToInt32(Request.QueryString["Id"]);
        String fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
        FileUpload1.SaveAs(Server.MapPath("~/Order/" + fileName));

        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ToString());
         con.Open();
        String Update =("Update Order set DesignedImage=@DesignedImage where Id=@Id");
        SqlCommand cmd = new SqlCommand( Update , con);
        cmd.Parameters.AddWithValue("@DesignedImage", "Order/" + fileName);
        cmd.Parameters.AddWithValue("@Id", + Id); 
        cmd.ExecuteNonQuery();
        con.Close();
        Label1.Text = "OK"; 
    }

I want to update table Order.
this code is giving me syntax error near keyword Order

Upvotes: 0

Views: 54

Answers (2)

Suvidha
Suvidha

Reputation: 72

You cannot have Order as your table name since it is reserved keyword on sql queries. Rename the table and try.

Upvotes: 0

Soner Gönül
Soner Gönül

Reputation: 98740

Order is a reserved keyword in T-SQL. You need use it with square brackets as [Order].

As a best practice, change it to non-reserved word.

It would be better to use using statement to dispose your connection and command automatically instead of calling Close or Dispose methods manually.

Also don't use AddWithValue as much as you can. It may generate unexpected and surprising results sometimes. Use Add method overload to specify your parameter type (SqlDbType) and it's size.

As a last thing, Open your connection just before you execute your command.

Upvotes: 3

Related Questions