Hi,
I'm trying to get all values from an asp:listbox that enable multiple choices, and i made the following code but it only gets the last selected item, only one.
I need to collect them all
-
for (int i = 0; i <= lstDept.Items.Count-1; i++)
-
{
-
if (lstDept.Items[i].Selected)
-
{
-
try
-
{
-
cmd = new SqlCommand(
-
"INSERT INTO CoDepartmentTable(CompanyID, DeptID) " +
-
" VALUES (@CompanyID, @DeptID) ", cnn);
-
-
cnn.Open();
-
cmd.Parameters.AddWithValue("CompanyID", new Guid(ddlCompany.SelectedValue));
-
cmd.Parameters.AddWithValue("DeptID", new Guid(lstDept.SelectedValue));
-
-
if (cmd.ExecuteNonQuery() == 1)
-
lblErr.Text = "Your data have been saved successfuly";
-
else
-
lblErr.Text = "Unable to insert data </br> please refresh the page then try again";
-
}
-
-
catch (Exception ex)
-
{
-
lblErr.Text = "Cannot insert duplicated value, <br/> The department '" + lstDept.SelectedItem.Text + "' had been assigned to this company before";
-
}
-
finally
-
{
-
if (cnn != null) cnn.Close();
-
}
-
}
-
-
}
-
Any Ideas :(