简体   繁体   中英

Retrieve CheckboxList values from Database

I have tried codes from here and there from all over the place but couldnt get anything to work...

I have my CheckBoxList values saved in a database. All i want is to Retrieve the values from Database and get the CheckBox List items to be selected accordingly.

The code in design page is:

<asp:CheckBoxList ID="CheckBoxList1" runat="server">
<asp:ListItem Value="1">Sales</asp:ListItem>
<asp:ListItem Value="2">Office & Administration</asp:ListItem>
<asp:ListItem Value="3">Domestic Help</asp:ListItem>
<asp:ListItem Value="4">Education & Training</asp:ListItem>
<asp:ListItem Value="5">Accounting and Finance</asp:ListItem>
<asp:ListItem Value="6">Hospitality and Tourism</asp:ListItem>
<asp:ListItem Value="7">Computer and IT</asp:ListItem>
<asp:ListItem Value="8">Customer Service</asp:ListItem>
<asp:ListItem Value="9">Logistics and Transportation</asp:ListItem>
<asp:ListItem Value="10">Healthcare</asp:ListItem>
<asp:ListItem Value="11">Food and Beverage</asp:ListItem>
<asp:ListItem Value="12">Marketing, Media & Communication</asp:ListItem>
<asp:ListItem Value="13">Construction & Architecture</asp:ListItem>
<asp:ListItem Value="14">Human Resources</asp:ListItem>
<asp:ListItem Value="15">Others</asp:ListItem>
</asp:CheckBoxList>

So i use this code to save the CheckBoxList values into the database. WHICH WORKS FINE...

 for (int j = 0; j < CheckBoxList1.Items.Count - 1; j++)
    {
        if (CheckBoxList1.Items[j].Selected == true)
        {
            string r = CheckBoxList1.Items[j].Value.ToString();
            SqlConnection myconn1;
            SqlCommand cmd1;
            string sql1;
            myconn1 = new SqlConnection(WebConfigurationManager.ConnectionStrings["ApplicationServices"].ToString());
            myconn1.Open();
            sql1 = "insert into interests(FK_ic_id,FK_is_id)values(@FK_ic_id,@FK_is_id)";
            cmd1 = new SqlCommand(sql1, myconn1);

            cmd1.Parameters.AddWithValue("@FK_is_id", ssid);
            cmd1.Parameters.AddWithValue("@FK_ic_id", r);
            cmd1.ExecuteNonQuery();
            myconn1.Close();
        }
    }

So the table is 'interests', there is a seperate table for category names (list of interests). The data is saved like:

  • User-1 (FK_IS_ID), Interest-2 (FK_IC_ID)
  • User-1 (FK_IS_ID), Interest-3 (FK_IC_ID)
  • User-2 (FK_IS_ID), Interest-4 (FK_IC_ID)
  • User-2 (FK_IS_ID), Interest-2 (FK_IC_ID)

The interest 2-3 indicates the interest from the 'category' table, which has :

  • category-1(c_id), Sales (c_name)
  • category-2(c_id), Marketing (c_name)
  • category-3(c_id), IT (c_name)

So now all i want is, on page load function the checkbox list to be repopulated, or filled depending on the user (SID) and his interests(c_id/FK_IC_ID). The items in checkboxlist needs to be selected on page load function. There needs to be multiple interests retrieved.

You have to bind your checklistbox to a datasource:

    SqlDataAdapter da = 
            new SqlDataAdapter("SELECT c_id,c_name FROM category",
            new SqlConnection
           (WebConfigurationManager.ConnectionStrings
           ["ApplicationServices"].ToString())); 
            DataSet ds = new DataSet();
            da.Fill(ds);
            checkedListBox1.DataSource = ds;
            checkedListBox1.SelectedValue = "c_id";
            checkedListBox1.SelectedItem = "c_name ";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM