Check (Select) CheckBox in GridView from database based on condition using C# in ASP.Net

lingers
 
on Oct 07, 2021 04:32 AM
630 Views

I have this page that has gridview with ckeckboxes, it insert and updates values in single quote e.g 'die33'.On page load the check box is checked or unchecked if a cell value is present in the database.

While i wrote the code the value in the database is in single quote e.g 'die33', so it reads perfectly well into the check box.But the actual database has double quote around the values e.g "die33",so the values are not read into the check box even if the cell value is present in the database ,because the double quoted is not programmed in my code. What i want is for my code to insert and update in double quote so that my code will read from the actual database of the project

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Oct 09, 2021 01:51 AM
lingers says:
if (chk.Checked)
            {
                if (acceptedCylinder == "[]")
                {
                    using (SqlCommand cmd = new SqlCommand("UPDATE issuesheet SET cylinders=@cylinders where pid='" + pid + "'", con))
                    {
                        cmd.Parameters.AddWithValue("@cylinders", "['" + number.Trim() + "']");
                        con.Open();
                        cmd.ExecuteNonQuery();
                        con.Close();
                    }
                    ClientScript.RegisterStartupScript(this.GetType(), "Popup", "ShowPopup('Record Inserted successfully.');", true);
                }
                else
                {
                    List<string> CylinderNo = acceptedCylinder.Replace("[", "").Replace("]", "").Replace("'", "").Split(',').ToList();
                    CylinderNo.Add(number);
                    using (SqlCommand cmd = new SqlCommand("UPDATE issuesheet SET cylinders=@cylinders where pid='" + pid + "'", con))
                    {
                        cmd.Parameters.AddWithValue("@cylinders", "['" + string.Join("','", CylinderNo.Distinct()) + "']");
                        con.Open();
                        cmd.ExecuteNonQuery();
                        con.Close();
                    }
                    ClientScript.RegisterStartupScript(this.GetType(), "Popup", "ShowPopup('Record Updated Successfully.');", true);
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(acceptedCylinder))
                {
                    List<string> CylinderNo = acceptedCylinder.Replace("[", "").Replace("]", "").Replace("'", "").Split(',').ToList();
                    CylinderNo.Remove(number);
                    using (SqlCommand cmd = new SqlCommand("UPDATE issuesheet SET cylinders=@cylinders where pid='" + pid + "' ", con))
                    {
                        cmd.Parameters.AddWithValue("@cylinders", CylinderNo.Distinct().Count() > 0 ? "['" + string.Join("','", CylinderNo.Distinct()) + "']" : "[]");
                        con.Open();
                        cmd.ExecuteNonQuery();
                        con.Close();
                    }
                    ClientScript.RegisterStartupScript(this.GetType(), "Popup", "ShowPopup('Record Deleted Successfully.');", true);
                }
            }

In this code you are updating the colun value with single quote. So its inserting values in single quote in the database e.g ['23a','23b']

You neet to modify the code to insert double quote.

    protected void OnChckedChanged(object sender, EventArgs e)
    {
        CheckBox chk = (CheckBox)sender;
        GridViewRow row = (GridViewRow)chk.NamingContainer;
 
        string number = row.Cells[2].Text;
        string pid = Label27.Text;
 
        string constring = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constring))
        {
            string acceptedCylinder = GetAcceptedCylinder(pid);
            if (chk.Checked)
            {
                if (acceptedCylinder == "[]")
                {
                    using (SqlCommand cmd = new SqlCommand("UPDATE issuesheet SET cylinders=@cylinders where pid='" + pid + "'", con))
                    {
                        cmd.Parameters.AddWithValue("@cylinders", "[\"" + number.Trim() + "\"]");
                        con.Open();
                        cmd.ExecuteNonQuery();
                        con.Close();
                    }
                    ClientScript.RegisterStartupScript(this.GetType(), "Popup", "ShowPopup('Record Inserted successfully.');", true);
                }
                else
                {
                    List<string> CylinderNo = acceptedCylinder.Replace("[", "").Replace("]", "").Replace("\"", "").Split(',').ToList();
                    CylinderNo.Add(number);
                    using (SqlCommand cmd = new SqlCommand("UPDATE issuesheet SET cylinders=@cylinders where pid='" + pid + "'", con))
                    {
                        cmd.Parameters.AddWithValue("@cylinders", "[\"" + string.Join("\",\"", CylinderNo.Distinct()) + "\"]");
                        con.Open();
                        cmd.ExecuteNonQuery();
                        con.Close();
                    }
                    ClientScript.RegisterStartupScript(this.GetType(), "Popup", "ShowPopup('Record Updated Successfully.');", true);
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(acceptedCylinder))
                {
                    List<string> CylinderNo = acceptedCylinder.Replace("[", "").Replace("]", "").Replace("\"", "").Split(',').ToList();
                    CylinderNo.Remove(number);
                    using (SqlCommand cmd = new SqlCommand("UPDATE issuesheet SET cylinders=@cylinders where pid='" + pid + "' ", con))
                    {
                        cmd.Parameters.AddWithValue("@cylinders", CylinderNo.Distinct().Count() > 0 ? "[\"" + string.Join("\",\"", CylinderNo.Distinct()) + "\"]" : "[]");
                        con.Open();
                        cmd.ExecuteNonQuery();
                        con.Close();
                    }
                    ClientScript.RegisterStartupScript(this.GetType(), "Popup", "ShowPopup('Record Deleted Successfully.');", true);
                }
            }
        }
    }