You must Sign In to post a response.
  • Category: .NET

    How to retrieve values to checkedlistbox from database?

    Are you looking for information on data fetching functionality? Want to know how to retrieve values to checked listbox from database? Read this thread to learn more about data retrieval from listbox with examples.

    I am doing an application in and c# with sql server as backend. I have a checkedlistbox with values mon,tue,wed,thu,fri,sat,sun. I am inserting the values to database as tue,wed,fri (with comma). I am retrieving the data using dataset as tue,wed,fri but I want to retieve the data and select the inserted value checkboxes i.e., I want to tick the tue,wed,fri how to do this please help me its urgent.
  • #725712

    Split the string value ( tue,wed, fri) with comma separator and then check that if the item is in that list then check that checkbox other wise no need to check.

    Give respect to your work, Instead of trying to impress your boss.

    Blog :

  • #725713
    Hai Sheri,
    It's not the good practice to keep the multiple values in a single column. This is violation of 1st Normal Form.
    So its better to create the separate column for each of the day so that while retrieving , you can simply set those values in the corresponding check-boxes.
    Let's say, you have checked mon, tue, fri then you can set these values as 1 in the database and for rest of the days, you can set as 0.
    By this way, now if you retrieve the values, get the values as 1 and check the corresponding checkboxes.
    Hope it will be helpful to you.

    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)

  • #725717
    First of all you need to set the properties of CheckedListBox.CheckedItems . I adviced you that you need to used foreach loop with comma in days of week .

  • #725721
    you need to fetch teh values from database tot dataset then separate them using comma and loop through checklistbox for each item and get them selected
    check following code snippet

    //get value from dataset
    string szDatafromDB = objDS.Tables[0].Rows[0][1].Tostring();
    //split the value with comma
    string[] words = szDatafromDB.split(',');
    foreach (ListItem li in CheckBoxList1.Items)
    //check if list item exist in array, if yesthem make it selected
    int pos = Array.IndexOf(words, li.Text);
    if (pos > -1)
    li.Selected = true;
    li.Selected = false;

    hope it helps

    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #725747

    As per your requirement complete code is here refer this

    create table tstchk(selval varchar(250))

    using System.Data.SqlClient;
    using System.Data;
    public partial class chklistStore : System.Web.UI.Page
    SqlConnection sqlcon = new SqlConnection(@"Server=RAVI-PC\SQLEXPRESS;database=test1;uid=ravindran;pwd=srirangam;");
    SqlCommand sqlcmd = new SqlCommand();
    SqlDataAdapter da = new SqlDataAdapter();
    DataTable dt = new DataTable();

    CheckBoxList ck = new CheckBoxList();
    protected void Page_Load(object sender, EventArgs e)

    ck.ID = "ck";

    //Store selected values from checkbox list into datbase
    protected void Button1_Click(object sender, EventArgs e)
    string str = "";
    string qry = "";
    foreach (ListItem listItem in ck.Items)
    if (listItem.Selected)
    str = str + listItem + ",";
    str = str.Substring(0, str.Length - 1); //remove last comma
    //Now insert that selected values in to database
    qry = "insert into tstchk values('" + str + "')";
    sqlcmd = new SqlCommand(qry,sqlcon);
    //make it false after store
    foreach (ListItem li in ck.Items)
    li.Selected = false;

    //checked in the items of checkbox list from database value
    protected void Button2_Click(object sender, EventArgs e)
    string[] dbval;
    string qry;
    qry = "select * from tstchk";
    sqlcmd = new SqlCommand(qry, sqlcon);
    da = new SqlDataAdapter(sqlcmd);
    if (dt.Rows.Count > 0)
    dbval = dt.Rows[0][0].ToString().Split(',');
    for (int i = 0; i <= dbval.Length - 1; i++)
    foreach (ListItem listItem in ck.Items)
    if (listItem.Text == dbval[i])
    listItem.Selected = true;

    Let me know if you need any help on this issue

    Your Hard work never fails

  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.