C# .NET - checkbox checking - Asked By g. venkatakiran on 13-May-10 05:14 AM


how to check the checkboxes when the records are fetched from database.

that is the checkbox values present in the database will be checked when they  are fetch
Sagar P replied to g. venkatakiran on 13-May-10 05:29 AM
 You can try something like this for it;

SqlConnection con = new SqlConnection("Your db connection string");

con.Open();

string query = "select checkBoxValue from yourTable";

SqlCommand cmd = new SqlCommand(query, con);

SqlDataReader dr;

dr = cmd.ExecuteReader();

if (dr.Read())

{                

    checkBox1.Checked = dr.GetBoolean(0);                

}
else
{
    checkBox1.Checked = false;
}

dr.Close();

con.Close();

Anoop S replied to g. venkatakiran on 13-May-10 06:58 AM
you can add a label in the form, make it visibility false, then select your value according to check box value, transfer that value to label and use if else statement
if label1.caption==""
{
checkBox1.Checked =true;
}
else
{
checkBox1.Checked =false;
}
Super Man replied to g. venkatakiran on 13-May-10 08:01 AM

 

 

        SqlConnection cn = new SqlConnection("Your connection string");

        cn.Open();

        SqlCommand cmd = new SqlCommand("select checkboxvalue from yourTable", con);

        SqlDataReader dr = cmd.ExecuteReader();

 

       

       

           

            for (int i = 1; dr.Read(); i++)

            {

                ((CheckBox)this.FindControl("CheckBox" + i)).Checked = Convert.ToBoolean(dr["checkboxvalue"]);

                              

            }

      

 

 

        con.Close();