Reputation: 191
In my Web Page, I have 2 List Boxes namely ListBox1,ListBox2.The user select the list of items from ListBox1 and move it to ListBox2.I done up to this, but after i click the 'SAVE' button, it is not save the ListBox2 selected item in the SQL table and It is not throw any error!! how to store it ?
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
lblPage1ID.Text=Server.UrlDecode(Request.QueryString["Parameter"].ToString());
ListBoxWorksPackages();
}
}
protected void ListBoxWorksPackages()
{
ListBox1.Items.Add("General Contractor");
ListBox1.Items.Add("Architecture");
ListBox1.Items.Add("Civil");
ListBox1.Items.Add("Mechanical");
ListBox1.Items.Add("Electrical");
}
protected void btnMoveRight1_Click(object sender, EventArgs e)
{
for (int i = ListBox1.Items.Count - 1; i >= 0; i--)
{
if (ListBox1.Items[i].Selected == true)
{
ListBox2.Items.Add(ListBox1.Items[i]);
ListItem li = ListBox1.Items[i];
ListBox1.Items.Remove(li);
}
}
}
protected void btnMoveLeft1_Click(object sender, EventArgs e)
{
for (int i = ListBox2.Items.Count - 1; i >= 0; i--)
{
if (ListBox2.Items[i].Selected == true)
{
ListBox1.Items.Add(ListBox2.Items[i]);
ListItem li = ListBox2.Items[i];
ListBox2.Items.Remove(li);
}
}
}
protected void BtnSave1_Click(object sender, EventArgs e)
{
SqlConnection SqlCon = new SqlConnection(GetConnectionString());
string Packagevalues = string.Empty;
foreach (ListItem item in ListBox2.Items)
{
if (item.Selected == true)
{
Packagevalues += "," + item.Text;
}
}
string query = "INSERT INTO Contractor_Info2 (Vendor_ID,WorksPackage) VALUES"
+ "(@Vendor_ID,@WorksPackage )";
try
{
SqlCommand cmd = new SqlCommand(query, SqlCon);
cmd.CommandType = CommandType.Text;
SqlCon.Open();
cmd.Parameters.AddWithValue("@Vendor_ID", lblPage1ID.Text);
cmd.Parameters.AddWithValue("@WorksPackage", Packagevalues);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
SqlCon.Close();
}
}
Upvotes: 0
Views: 414
Reputation: 14565
you need to call cmd.ExecuteNonQuery()
after you've added the parameters to your sql procedure. this is what will actually run your sql statement.
try
{
SqlCommand cmd = new SqlCommand(query, SqlCon);
cmd.CommandType = CommandType.Text;
SqlCon.Open();
cmd.Parameters.AddWithValue("@Vendor_ID", lblPage1ID.Text);
cmd.Parameters.AddWithValue("@WorksPackage", Packagevalues);
// add this
cmd.ExecuteNonQuery();
}
Upvotes: 2