I have a group checkbook list which i want to preselect from the database table. I amusing following code to for the selection of this but it is not working
Data in table is store in following format
example:-
1,2,
1,5,7,
1,2,3,4,
HTML
<asp:checkboxlist id="chkBoxDaysList" runat="server">
<asp:listitem runat="server" value="1" Text="Sunday" />
<asp:listitem runat="server" value="2" Text="Monday" />
<asp:listitem runat="server" value="3" Text="Tuesday" />
<asp:listitem runat="server" value="4" Text="Wednesday" />
<asp:listitem runat="server" value="5" Text="Thrusday" />
<asp:listitem runat="server" value="6" Text="Friday" />
<asp:listitem runat="server" value="7" Text="Saturday" />
</asp:checkboxlist>
C# Code to preselect checkbox based on data saved previously
public void getSelectedDays()
{
IDataReader dr;
String strSqlDays = "SELECT * FROM EventCalender WHERE rowID = 6";
DataSet ds = new DataSet();
ds = DataProvider.Connect_Select(strSqlDays);
dr = ds.Tables[0].CreateDataReader();
string[] s = new string[50];
while (dr.Read())
{
s = dr["EventDays"].ToString().Split(',');
}
int length = s.Length;
for (int i = 0; i <= s.Length - 1; i++)
{
string cntry = s[i];
for (int j = 0; j <= chkBoxDaysList.Items.Count - 1; j++)
{
if (chkBoxDaysList.Items[j].Text == s[i])
{
chkBoxDaysList.Items[j].Selected = true;
break;
}
}
}
}
Right now code doesn’t generate any error but doesn’t select any checkbox also
You are checking text of checkbox with value from DB. Please check value of the checkboxlist with value from DB record.Corrected code is shown below–>