Click here to Skip to main content
15,905,967 members
Please Sign up or sign in to vote.
1.44/5 (3 votes)
See more:
How to retrieve checkbox values from a database .
Posted
Comments
[no name] 14-Apr-14 7:28am    
what do you mean?
Could you tell clearly your requirement ?
[no name] 14-Apr-14 7:35am    
You write a query. How else would you think?
Murugesan22 14-Apr-14 9:01am    
Post Code here
Member 10578683 15-Apr-14 4:20am    
populateGridview();
Member 10578683 15-Apr-14 4:20am    
public partial class _Default : System.Web.UI.Page
{
DataUtility dut = new DataUtility();
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Con"]);

protected void Page_Load(object sender, EventArgs e)
{


Label5.Text = (string)Session["Role_Name"];

if (!IsPostBack)
{

populateGridview();

}

}
public void populateGridview()
{
if (Session["Role_Name"] != null && !string.IsNullOrEmpty(Session["Role_Name"].ToString()))
{
string qry = "select ID,Pages,[Add],[View],Edit,[Delete] from Role1 where Role_Name= '" + Session["Role_Name"].ToString() + "'";

DataSet ds = dut.GetDataSet(qry);
GridView1.DataSource = ds;
GridView1.DataBind();
}
//string qry = "select Pages,[Add],[View],Edit,[Delete] from Role1 where Role_Name= '"+Label5.Text+"'";
//DataSet ds = dut.GetDataSet(qry);
//GridView1.DataSource = ds;
//GridView1.DataBind();
}

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{

}
protected void Button7_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
if (Session["Role_Name"] != null && !string.IsNullOrEmpty(Session["Role_Name"].ToString()))
{
CheckBox chkAdd = (row.Cells[2].FindControl("CheckBox1") as CheckBox);

CheckBox chkEdit = (row.Cells[3].FindControl("CheckBox2") as CheckBox);

CheckBox chkView = (row.Cells[4].FindControl("CheckBox3") as CheckBox);

CheckBox chkDelete = (row.Cells[5].FindControl("CheckBox4") as CheckBox);

// string strID = row.Cells[0].Text;
string ID = row.Cells[0].Text;
string Pages = row.Cells[1].Text;
con.Open();



string query = "Update Role1 set [Add]='" + (chkAdd.Checked == true ? 'Y' : 'N') + "', [View]='" + (chkView.Checked == true ? 'Y' : 'N') + "' ,[Edit]='" + (chkEdit.Checked == true ? 'Y' : 'N') + "' ,[Delete]='" + (chkDelete.Checked == true ? 'Y' : 'N') + "' where Pages ='" + Pages + "' and Role_Name='" + Session["Role_Name"].ToString() + "'and ID='" + ID + "'";
SqlCommand cmd = new SqlCommand(query, con);
cmd.ExecuteNonQuery();

con.Close();

}
}
}
populateGridview();

GridView1.DataBind();
}

}

1 solution

checkbox values are not stored in a database. the database stores e.g. boolean values or what ever you map to a checkbox (like "ja" for checked or "nein" for unchecked and "indifferent" for intermediate state). so, this question is hard to answer and depending on your, let's call it implementation strategy (database first, code first, model first). however sticking to conventions and using the entity framework for your database mappings, esp. when using the "code first" approach, may leave the persistence concerns, and there for the loading, to the magic of the framework (you can have a largely transparent persistence layer). another approach is mapping your db-module/ schema to your domain objects (pocos) either by hand or entity framework (or any other orm/ odm framework) using either annotations, or a fluent api or some other programming manner I forgot to mention. dive deeper into the matter of persistent frameworks (I sugest using the entity framework) and find out. see for example Entity Framework Code First: Mapping to Existing Table in the Database[^]
in simple words: map a Boolean property of your selectable objects to a column in your database and let the entity framework do the binding for you and set the checkbox state programmatically in your code.
 
Share this answer
 
Comments
Rahul VB 15-Apr-14 0:42am    
good,thats good. Very nice Sir.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900