I'm trying to add checked values from checkedlistbox using stored procedure and C#
public static void CreateDrugsListBox(CheckedListBox DrugsList)
{
try
{
SqlConnection con = new SqlConnection(@"connection");
SqlCommand cmd = new SqlCommand("InsertData_DrugsListBox", con);
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
foreach (string itemchecked in DrugsList.CheckedItems)
{
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@DrugsValue", itemchecked);
cmd.ExecuteNonQuery();
}
con.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error"+ex, "Error", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
}
}
it displays an error like:
errorSystem.invalidcastException: imposible to make a cast of an object type System.Data.DataRowView in type System.String