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

Last Reply 22 days ago By pandeyism

Posted 22 days 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();
            }
        }        

 

You are viewing reply posted by: pandeyism 22 days ago.
Posted 22 days ago Modified on 22 days ago

Hi akhter,

Take reference to below code change yor code.

Code

protected void btn_update_Click(object sender, EventArgs e)
{
    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;
    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;
    }
    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(constr);
    string query = "UPDATE Customer SET ";
    SqlCommand cmd = new SqlCommand(query, con);
    if (!string.IsNullOrEmpty(customername))
    {
        query += "CustomerName =@CustomerName,";
        cmd.Parameters.AddWithValue("@CustomerName", customername);
    }
    if (!string.IsNullOrEmpty(filename))
    {
        query += "Name = @Name,";
        cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = filename;
    }
    if (bytes.Length > 0)
    {
        query += "Data=@Data,";
        cmd.Parameters.Add("@Data", SqlDbType.Binary).Value = bytes;
    }
    if (!string.IsNullOrEmpty(contenttype))
    {
        query += "ContentType=@ContentType,";
        cmd.Parameters.Add("@ContentType", SqlDbType.VarChar).Value = contenttype;
    }
    query = query.TrimEnd(',');
    query += " WHERE CustomerId =@Id";
    cmd.Parameters.AddWithValue("@Id", id);
    con.Open();
    cmd.ExecuteNonQuery();
    con.Close();
}