List Box Issue while inserting data in Windows App

Posted by Raj.Trivedi under C# on 3/2/2013 | Points: 10 | Views : 1126 | Status : [Member] [MVP] | Replies : 0
Hello Team,

I have one combo box and a Listbox.

Both of the controls are getting data from database.

Now i am selecting the items in combo box and selecting items in list box.

I want that what id of the item selected in the combobox to be updated against all the items selected in the list box.

EG :-

My Combo box is of teamleader.

My List box is of employee.

Now when i select a Teamleader the id of the teamleader should be updated to all the selected employees in the list box.

Eg If i select Teamleader in the combo box as raj whose id = 1

And then if i select 5 employees and click save

then against all the employees the teamleader id should be updated =1

so that means Raj as team leader will have 5 employees.



I am also attaching the for loop but in my for loop it is only updating one value

private void btnMap_Click(object sender, EventArgs e)
con = new SqlConnection(strconn);
for (int items = 0; items < listBox1.SelectedItems.Count; items++)
DataRowView drv = listBox1.SelectedItems[items] as DataRowView;
if (drv != null)
sqlcmd = new SqlCommand("MappEmployees", con);
sqlcmd.CommandType = CommandType.StoredProcedure;
sqlcmd.Parameters.AddWithValue("@TeamLeaderID",SqlDbType.Int).Value = cmbTL.SelectedValue;
sqlcmd.Parameters.AddWithValue("@EmployeeCode", SqlDbType.Int).Value = listBox1.SelectedItems;
int result= sqlcmd.ExecuteNonQuery();

catch (Exception ex)
throw new Exception(ex.Message);

This is my Employee Table.

create table EmployeeDetails
EmpID int identity(1,1),
EmpName varchar(20),
TeamleaderID int

Stored Proc

create proc UpdateTL
@EmpID int,
@TLID int

update employeedetails set TL =@TLID where EmpID =@EmpID

This logic is only updating one Employees TeamleaderID


"Sharing is Caring"
Please mark as answer if your Query is resolved


(No response found.)

Login to post response