Ram
Ram

Reputation: 45

How to check checkbox based on database values in C#

I have created Checkbox list and Button to store Checked Values in ASP.Net application using C# as follows,

 <asp:CheckBoxList ID="CheckBoxList1" runat="server" Width="120px">
         </asp:CheckBoxList>
<asp:Button ID="AddCheckedData" Height="25px" runat="server" Text="Add Checked Data"  
                                                onclick="AddCheckedData_Click"/>

I have binded CheckBoxList in Code behind as,

public void bindchecklist()
    {
        B.SFS_CODE = Convert.ToInt32(TxtHQCode.Text);
        B.id = 7; //tblparent AND tblchild
        ds8 = A.DATA8(B);
        CheckBoxList1.DataSource = ds8.Tables[0];
        CheckBoxList1.DataTextField = "EMP_NAME";
        CheckBoxList1.DataValueField = "EMP_CODE";
        CheckBoxList1.DataBind();
    }

And the button click event to store the selected values in DataBase as follows,

string selemployee = string.Empty;
foreach (ListItem item in CheckBoxList1.Items)
{
if (item.Selected)
{
selemployee = item.Value;
B.id = 8;//insert into DCR_CHECKED
B.Emp_Code = selemployee;
ds8 = A.DATA8(B);
}
}

Now,i have another button to edit the CheckBoxList Selection.Now,i want to retrieve the data from database and check the checkboxes which are availabe(inserted checkbox values in databse when click save button) in DataBase when Clicking Edit Button.

Upvotes: 1

Views: 7541

Answers (2)

Ram
Ram

Reputation: 45

foreach (DataTable table in ds8.Tables)
                {

                    foreach (DataRow dr in table.Rows)
                    {
                        String S = Convert.ToString(dr["EMP_CODE"].ToString());
                        foreach (ListItem item in CheckBoxList2.Items)
                        {
                            if (S == item.Value)
                            {
                                item.Selected = true;
                            }
                        }
                    }
                }

Upvotes: 0

Koby Douek
Koby Douek

Reputation: 16675

Please try:

for each (DataRow row in resultTable.Rows)
{
    if (Convert.ToBoolean(row["bit_column_name"]))
    {
         // check box is "1" (checked) in the database
         .....
    }
}

Upvotes: 1

Related Questions