0
votes

I have 2 listboxes in a window form, one on left and one on right. The 1st listbox have some items while the 2nd listbox is empty. Also there are 2 buttons between the 2 listboxes which used to move item from/to the 1st and 2nd listbox

My problem here is that after I bind the data to the 1st listbox (from a database, using DisplayMember and ValueMember) , and I try to move 1 of the item from this 1st listbox to the 2nd listbox and I want to update the table. using the for loop i m updating the values in db. but it is only updating the single value not multiple value

private void btnMoveRight_Click(object sender, EventArgs e)
        {

            string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
            using (SqlConnection con = new SqlConnection(CS))
                try
                {

                    SqlCommand sqlcmd = new SqlCommand("USP_UpdateJobTracker_Box", con);
                    sqlcmd.CommandType = CommandType.StoredProcedure;
                    sqlcmd.Parameters.AddWithValue("@BoxName", "");
                    sqlcmd.Parameters.AddWithValue("@Jobname", "");
                    con.Open();
                    for (int x = 0; x <= lstUnAssignedJobs.SelectedItems.Count-1; x++)
                    {
                        if (lstUnAssignedJobs.GetSelected(x) == true)
                        {
             sqlcmd.Parameters[0].Value = listWBox.GetItemText(listWBox.SelectedValue);
             string s = lstUnAssignedJobs.SelectedItems.ToString();
                            string selectedItem = lstUnAssignedJobs.Items[x].Text;
                            sqlcmd.Parameters[1].Value = lstUnAssignedJobs.GetItemText(lstUnAssignedJobs.Items[x].ToString());----here is red redline
                            sqlcmd.ExecuteNonQuery();

                        }

                    }

                    AssignedJobToBox(listWBox.SelectedValue.ToString());

                }

                catch (Exception ex)
                {

                    MessageBox.Show("exception raised");

                }

                finally
                {

                    con.Dispose();
                    con.Close();

                }

thanks in advance

1
I think you are selecting 1 item at a time from listboxvallabha

1 Answers

0
votes

This did not make any sense to me:

 sqlcmd.Parameters[1].Value = lstUnAssignedJobs.GetItemText(lstUnAssignedJobs.Items[x].ToString());----here is red redline

You can do this right?

 sqlcmd.Parameters[1].Value = lstUnAssignedJobs.Items[x].Text.ToString();

If you could explain further it would be appreciated