简体   繁体   中英

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM