I want to add(delete) selected items in listbox from Form1 into sql server.I have three Forms.Wenn I click add button in Form1, Form2 opens and a textbox and save button appear to add the data.It calls from textbox in Form1.The code doesn't give error but nothing happens in database. I can't see the problem.The code is below.
FORM1:
SqlConnection baglan = new SqlConnection(@"Server=10.34.16.219; Database=envanter; User ID=envanter; Password=Er112233;");
SqlCommand cmd = new SqlCommand();
public void button1_Click(object sender, EventArgs e) //from db
{
try
{
baglan.Open();
cmd.Connection = baglan;
cmd.CommandType = CommandType.Text;
cmd.CommandText = @"SELECT @textBox1 FROM Ana";
cmd.Parameters.AddWithValue("@textBox1", textBox1.Text);
cmd.ExecuteNonQuery();
baglan.Close();
}
catch (SqlException exc)
{
MessageBox.Show(exc.Message.ToString(), "Error Message");
}
Form2 f2 = new Form2();
f2.Show();
this.Visible = false;
}
FORM2:
SqlConnection baglan = new SqlConnection(@"Server=10.34.16.219; Database=envanter; User ID=envanter; Password=Er112233;");
SqlCommand cmd = new SqlCommand();
private void button1_Click(object sender, EventArgs e) //add
{
try
{
baglan.Open();
cmd.Connection = baglan;
cmd.CommandType = CommandType.Text;
cmd.CommandText = @"INSERT INTO Ana(f1.textBox1.Text) VALUES(@p1)";
cmd.Parameters.AddWithValue("@p1", textBox1.Text);
MessageBox.Show("Inserted");
baglan.Close();
}
catch (Exception)
{
baglan.Close();
MessageBox.Show("Kayıt yapılmış!");
}
finally
{
Form2_Load(sender, e);
}
Form1 f1 = new Form1();
f1.Show();
this.Hide();
}
INSERTstatement ascmd, you also need to execute that statement! Runcmd.ExecuteNonQuery();after adding the parameter value (but before showing theMessageBox) to actually run that SQL !