i am making use of two listboxes. the data from one listbox is transfered to other listbox on button click, bt teh the data which is selected must be added to the table in the sql server. but it is not storing values it is throwing execption...
plzz..help...
private void InsertRecords(StringCollection sc)
{
SqlConnection conn = new SqlConnection("Data Source=ABCD;Integrated Security=True");
StringBuilder sb = new StringBuilder(string.Empty);
foreach (string item in sc)
{
const string sqlStatement = "INSERT INTO Class1 (Name) VALUES";
sb.AppendFormat("{0}('{1}'); ", sqlStatement, item);
}
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(sb.ToString(), conn);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
MessageBox.Show("data saved");
}
catch (System.Data.SqlClient.SqlException ex)
{
string msg = "Insert Error:";
msg += ex.Message;
throw new Exception(msg);
}
finally
{
conn.Close();
}
}
private void btn_save_Click(object sender, EventArgs e)
{
StringCollection sc = new StringCollection();
foreach (ListItem item in lstSelected.Items)
{
if (item.Selected)
{
sc.Add(item.Text);
}
}
InsertRecords(sc);
}