Akhter HUssain

Akhter HUssain

  • 681
  • 1.3k
  • 95.3k

Incorrect syntax near ‘SET’ in asp.net c#

Aug 28 2019 10:46 AM
When i updating record then this error is coming...please guide
 
below mentioned behind update button code
  1. int id = int.Parse(lbid.Text);  
  2. string filePath = FileUpload1.PostedFile.FileName;  
  3. string customername = txtcus.Text;  
  4. string filename = Path.GetFileName(filePath);  
  5. string ext = Path.GetExtension(filename);  
  6. string contenttype = String.Empty;  
  7. switch (ext) {  
  8.  case ".doc":  
  9.   contenttype = "application/vnd.ms-word";  
  10.   break;  
  11.  case ".docx":  
  12.   contenttype = "application/vnd.ms-word";  
  13.   break;  
  14.  case ".xls":  
  15.   contenttype = "application/vnd.ms-excel";  
  16.   break;  
  17.  case ".xlsx":  
  18.   contenttype = "application/vnd.ms-excel";  
  19.   break;  
  20.  case ".jpg":  
  21.   contenttype = "image/jpg";  
  22.   break;  
  23.  case ".png":  
  24.   contenttype = "image/png";  
  25.   break;  
  26.  case ".gif":  
  27.   contenttype = "image/gif";  
  28.   break;  
  29.  case ".pdf":  
  30.   contenttype = "application/pdf";  
  31.   break;  
  32. }  
  33. Stream fs = FileUpload1.PostedFile.InputStream;  
  34. BinaryReader br = new BinaryReader(fs);  
  35. Byte[] bytes = br.ReadBytes((Int32) fs.Length);  
  36. //string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;    
  37. //SqlConnection con = new SqlConnection(constr);    
  38. string query = "UPDATE Customer SET ";  
  39. SqlCommand cmd = new SqlCommand(query, con);  
  40. if (!string.IsNullOrEmpty(customername)) {  
  41.  query += "CustomerName =@CustomerName,";  
  42.  cmd.Parameters.AddWithValue("@CustomerName", customername);  
  43. }  
  44. if (!string.IsNullOrEmpty(filename)) {  
  45.  query += "Name = @Name,";  
  46.  cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = filename;  
  47. }  
  48. if (bytes.Length > 0) {  
  49.  query += "Data=@Data,";  
  50.  cmd.Parameters.Add("@Data", SqlDbType.Binary).Value = bytes;  
  51. }  
  52. if (!string.IsNullOrEmpty(contenttype)) {  
  53.  query += "ContentType=@ContentType,";  
  54.  cmd.Parameters.Add("@ContentType", SqlDbType.VarChar).Value = contenttype;  
  55. }  
  56. query = query.TrimEnd(',');  
  57. query += " WHERE CustomerId =@Id";  
  58. cmd.Parameters.AddWithValue("@Id", id);  
  59. con.Open();  
  60. cmd.ExecuteNonQuery();  
  61. con.Close();  

Answers (11)