Click here to Skip to main content
15,878,430 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

I want to know how to view data from a table in a database in CheckedListBox.

Given three tables:
users: id, username, password
privileges: pri_num, privilege
user_privileges: id, pri_num

user_privileges provides the mechanism for joining.

I would like to view the list of available privileges for a given user and save the checked (enabled) privileges to the database.
Updated 25-Jun-10 16:41pm
TheyCallMeMrJames 25-Jun-10 22:44pm    
I cleaned this question up, but you'll still need to put a discreet question in here. What part are you having trouble with? Would you like suggestions on how to display the data? Save to the database? What are you using to retrieve the data (LINQ, ADO.NET)? What, if anything, have you tried?

I think the solution to your entire question, regardless of what you've tried, is already here[
Share this answer
this is my tried to display the data in checkedlistbox from database(SQL Server 2005 enterprise)
look this code
public void Privileges()
    this.mcode.query = "select Pri_num,Pri_name from Privileges";
    d_adapter = new SqlDataAdapter(query,connect);
    d_set = new DataSet();
    for (int i = 0; i< this.mcode.d_set.Tables[0].Rows.Count; i++)

but i don't know how to make the value of the items in checkedlistbox is the privileges number (Pri_num)and i would like to save the checked(enable) for the user in the database by its value (Pri_num) in User_privileges

thank you for your cooperation
Share this answer
You have only retrieved the list of privileges, but not their assignment to a specific user.

You will need to load both the list of privileges and user_privileges (filtered by your current user). If this is a new set of permissions, you need to store the id of the privileges and not just the text.

Please check out the sample application I linked to. The information you are looking for is already there.

Share this answer
i did this code but when i executed and marked one item or more than one items it gave me this message "Conversion failed when converting the varchar '1' to data type int"

<br />
private void u_done_but_Click(object sender, EventArgs e)<br />
{<br />
string mycmd = "insert into users (id, u_name, pass) values ('" + txtUserID.Text + "', '" + txtUsername.Text + "', '" + @txtPass.Text + "')"; <br />
SqlConnection connect = new SqlConnection(conString); <br />
connect.Open(); <br />
SqlCommand cmd = new SqlCommand(mycmd, connect); <br />
cmd.ExecuteNonQuery(); <br />
foreach (DataRowView chk in checkedListBox1.CheckedItems)<br />
{<br />
s += string.Format("{0}\n", chk[this.u_privleges_chListBox.ValueMember]);<br />
mycmd = "insert into Users_Privileges (id, pri_num) values ('" + txtUserID.Text+"','"+ s +"')";<br />
cmd = new SqlCommand(mycmd, connect);<br />
cmd.ExecuteNonQuery();<br />
}<br />
connect.Close(); <br />
}<br />

this code to display data in checkedlistbox

<br />
public void Privileges()<br />
{<br />
this.mcode.query ="select * from Privileges";<br />
this.mcode.view();<br />
this.u_privleges_chListBox.DataSource = this.mcode.d_set.Tables[this.mcode.query].DefaultView;<br />
this.u_privleges_chListBox.DisplayMember = "Pri_name";<br />
this.u_privleges_chListBox.ValueMember = "Pri_num";<br />
}<br />
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900