简体   繁体   English

从数据库检索CheckboxList值

[英]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. 我将CheckBoxList值保存在数据库中。 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. 因此,我使用此代码将CheckBoxList值保存到数据库中。 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) 用户1(FK_IS_ID),兴趣2(FK_IC_ID)
  • User-1 (FK_IS_ID), Interest-3 (FK_IC_ID) 用户1(FK_IS_ID),兴趣3(FK_IC_ID)
  • User-2 (FK_IS_ID), Interest-4 (FK_IC_ID) 用户2(FK_IS_ID),兴趣4(FK_IC_ID)
  • User-2 (FK_IS_ID), Interest-2 (FK_IC_ID) 用户2(FK_IS_ID),兴趣2(FK_IC_ID)

The interest 2-3 indicates the interest from the 'category' table, which has : 利息2-3表示“类别”表中的利息,该表具有:

  • category-1(c_id), Sales (c_name) 类别1(c_id),销售(c_name)
  • category-2(c_id), Marketing (c_name) 类别2(c_id),行销(c_name)
  • category-3(c_id), IT (c_name) 类别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). 因此,现在我想要的是,在页面加载功能上,将根据用户(SID)及其兴趣(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 ";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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