Update specific column in database based on user input using C# in ASP.Net

Last Reply 10 months ago By pandeyism

Posted 10 months ago

i am facing issue that when i select row from gridview for edit record then just i want to update customer name not a image then customer name is not getting update.

If i change picture and customer name then both value are getting update.

please guide...

here is my update btn code....

        protected void btn_update_Click(object sender, EventArgs e)
        {
            // Read the file and convert it to Byte Array

            int id = int.Parse(lbid.Text);
            string filePath = FileUpload1.PostedFile.FileName;
            string customername = txtcus.Text;
            string filename = Path.GetFileName(filePath);
            string ext = Path.GetExtension(filename);
            string contenttype = String.Empty;
            //Set the contenttype based on File Extension
            switch (ext)
            {
                case ".doc":
                    contenttype = "application/vnd.ms-word";
                    break;
                case ".docx":
                    contenttype = "application/vnd.ms-word";
                    break;
                case ".xls":
                    contenttype = "application/vnd.ms-excel";
                    break;
                case ".xlsx":
                    contenttype = "application/vnd.ms-excel";
                    break;
                case ".jpg":
                    contenttype = "image/jpg";
                    break;
                case ".png":
                    contenttype = "image/png";
                    break;
                case ".gif":
                    contenttype = "image/gif";
                    break;
                case ".pdf":
                    contenttype = "application/pdf";
                    break;
            }

            if (contenttype != String.Empty)
            {
                Stream fs = FileUpload1.PostedFile.InputStream;
                BinaryReader br = new BinaryReader(fs);
                Byte[] bytes = br.ReadBytes((Int32)fs.Length);
                //  string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
                //SqlConnection con = new SqlConnection("Data Source=ATLANTIC\\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=SilverProduction;MultipleActiveResultSets=True;");
                SqlCommand cmd = new SqlCommand("UPDATE Customer SET CustomerName =@CustomerName , Name = @Name,ContentType=@ContentType, Data=@Data WHERE CustomerId =@Id", con);
                cmd.Parameters.AddWithValue("@Id", id);
                cmd.Parameters.AddWithValue("@CustomerName", customername);
                cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = filename;
                cmd.Parameters.Add("@ContentType", SqlDbType.VarChar).Value = contenttype;
                cmd.Parameters.Add("@Data", SqlDbType.Binary).Value = bytes;
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
            }
        }        

 

This question does not have replies that have been liked.