Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all i am working with checkedlistbox control. After checking two items in the checkedlist and when i am storing these values in a table only 2nd value is storing the table not the 1st checked value.

Can we extend the functionality of items property from single to multiple?

Plz anybody can give solution for this problem.

Thanks
Rajj

C#
if (chklbForms.CheckedItems.Count != 0)
            {
                cmd = new OleDbCommand();
                cmd.Connection = conn;
                conn.Open();
                int i1 = gdvUser.SelectedCells[0].RowIndex;
                string uname = Convert.ToString(gdvUser["UserName", i1].Value);
                cmd.CommandText = "Select UCode From UserMaster where Username='" + uname + "'";
                string ucode = Convert.ToString(cmd.ExecuteScalar());

                int i2 = gdvModule.SelectedCells[0].RowIndex;
                string mname = Convert.ToString(gdvModule["ModName", i2].Value);
                cmd.CommandText = "Select ModCode From MstModule where ModName='" + mname + "'";
                string modcode = Convert.ToString(cmd.ExecuteScalar());
List<object> checkedList = new List<object>();
C#
foreach (object item in chklbForms.CheckedItems)
                {


                    checkedList.Add(chklbForms.CheckedItems.ToString());
C#
string fname = chklbForms.SelectedValue.ToString();
                       cmd.CommandText = "Select FrmCode From MstForms Where FrmName='" + fname + "'";
                        string frmcode = Convert.ToString(cmd.ExecuteScalar());

                        string str = "";
                        str = "Insert into MstUserRights(UCode,ModCode,FrmCode) values('" + ucode + "','" + modcode + "','" + frmcode + "')";
                        cmd = new OleDbCommand(str, conn);
                        cmd.ExecuteNonQuery();
                        MessageBox.Show("Successful");
C#
}

               conn.Close();
Posted
Updated 23-Nov-11 20:17pm
v2
Comments
Sergey Alexandrovich Kryukov 24-Nov-11 1:34am    
Not clear. Could you post some code sample -- minimalistic, not your working code?
--SA

The CheckedListBox control has a SelectedItems property, which you can use to retrieve multiple selected items. It also has a SelectedIndices property, if you need to access each item by it's index. See CheckedListBox Class[^] for more details.
 
Share this answer
 
Comments
Rajjjjjjj 24-Nov-11 1:56am    
I checked two items from the checkedlistbox and when i am trying to make the loop for checkeditems it doesnt recognize the 1st checked value. Instead it is taking 2nd checkeditem for 2 time.....
Wayne Gaylard 24-Nov-11 2:03am    
You need to post a code snippet - add it to your osiginal question using the Improve Question link.
C#
StringBuilder objStringBuilder = new StringBuilder();
for(int i = 0; i <= (CheckBoxList1.Items.Count - 1); i++)
 {

     if (CheckBoxList1.Items[i].Selected)
         objStringBuilder.Append("," + (CheckBoxList1.Items[i].Value));


 }
if (objStringBuilder.Length > 0)
    objStringBuilder.ToString().Substring(1);



this code will return comma separated string for checked items.
if you not getting first checked item only. please check the item value...
hope this will help... and try to re post or edit your question..
 
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