sql server 2012 - C#: How to Insert data into database? -


I want to add items to the list from Form 1 to SQL Server. I have three forms. In Form 1 button, Form 2 opens and a text box and Save button appear to add data. It calls from textbox in Form 1. This code does not return the error, but nothing happens in the database. I do not see the problem. The code is below.

FORM1:

  SqlConnection baglan = New SqlConnection ("Server = 10.34.16.219; Database = Entrator; User ID = Password; Password = Er 112233;"); SqlCommand CMD = new SQL Commands (); Public Zero Button 1_Click (Object Sender, EventArgs E) // DB {try {baglan.Open ()) Cmd.Connection = Blocking; Cmd.CommandType = CommandType.Text; Cmd.CommandText = @ "Select Text Box 1" from Ana; Cmd.Parameters.AddWithValue ("@ textbox1", textbox1.Text); cmd.ExecuteNonQuery (); baglan.Close ();} Hold (SqlException exc } {MessageBox.Show (exc.Message.ToString (), "Error Message");} Form2F2 = New Form 2 (); f2.Show (); This.Visible = false;}  < / Pre> 

FORM2:

  SqlConnection baglan = New SqlConnection ("Server = 10.34.16.219; Database = Entrator; User ID = Password; Password = Er 112233;"); SqlCommand CMD = New SQL Commands (); Private Zero Button 1_Click (Object Sender, EventArgs E) // Add // {try {baglan.Open ()) Cmd.Connection = Ignore; Cmd.Comm AndType = CommandType.Text; Cmd.CommandText = @ "Add to Ana (f1.textbox1.Text) Price (@ P1)"; Cmd.Parameters.AddWithValue ("@ P1", Text Box 1. Text); MessageBox.Show ("inserted"); baglan.Close ();} hold (exception) {collapse;) MessageBox.Show ("Kayıt yapılmış!");} {Form 2_load (sender, e) at the end; } Form 1 F1 = New Form 1 (); F1.Show (); This.Hide (); }  

You can not parameter your columns. You can only parameter your values.

That's why you can not write;

  cmd.CommandText = @ "SELECT @ textBox1 Ana"; Cmd.Parameters.AddWithValue ("@ textbox1", textbox1.Text); Actually you can do this, it's a valid syntax for C #, but this is not a valid SQL. If you  actually  create your column parameters, take a look  

and you set your SqlCommand in your Form2 Have not executed.


Comments