Click here to Skip to main content
16,008,010 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Please help me.......
thanks in advance
Posted
Comments
Member 9332883 18-Mar-13 3:45am    
try this...

for (int i = 0; i < chHobbies.Items.Count; i++)
{
if (chHobbies.Items[i].Selected == true)
{
//hob += chHobbies.Items[i].Text + ",";
string qr = "insert into stuhob(StuID,HobID) values('" + txtStuID.Text + "','" + chHobbies.Items[i].Text + "')";
SqlCommand cm = new SqlCommand(qr, conn);
if (conn.State == ConnectionState.Open)
{
conn.Close();
}

conn.Open();
cm.ExecuteNonQuery();
conn.Close();
}
}

Hello you can use code something like shown below to retrieve the selected values
C#
string[] items = new string[list_box.SelectedItems.Count()];
for (int idx = 0; idx < list_box.SelectedItems.Count; idx++) {
    items[idx] = list_box.SelectedItems[idx];
}
 
Share this answer
 
You can use the code what Member 9332883555 is given.
you can try this query part too.

C#
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "INSERT INTO stuhob(StuID,HobID) VALUES (@StuID,@HobID)";
cmd.Parameters.AddWithValue("@StuID",txtStuID.Text);
cmd.Parameters.AddWithValue("@HobID",chHobbies.Items[i].Text );
cmd.ExecuteNonQuery();
 
Share this answer
 
If your database table has only a single row and single column where you need to store the selected values you can use for loop to get selected values by user and use a delimiter as comma or any other special character and hold the values in a string and pass it as a parameter to your stored procedure ,but in case you have single row or single column for each values of your checkbox then you can go with solution 1 to insert/update as per your needs.
 
Share this answer
 
hello,try this...

C#
for (int i = 0; i < chHobbies.Items.Count; i++)
            {
                if (chHobbies.Items[i].Selected == true)
                {
                    //hob += chHobbies.Items[i].Text + ",";
                    string qr = "insert into stuhob(StuID,HobID) values('" + txtStuID.Text + "','" + chHobbies.Items[i].Text + "')";
                    SqlCommand cm = new SqlCommand(qr, conn);
                    if (conn.State == ConnectionState.Open)
                    {
                        conn.Close();
                    }

                    conn.Open();
                    cm.ExecuteNonQuery();
                    conn.Close();
                }
            }
 
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