0

I've just started to learn about databases and actually I've no idea why the code below doesn't work. I have searched a lot but I didn't realized what exactly is the happening.

I have a Windows Forms app and there are a textbox and a button in it. When I click on the button, the text inside the textbox should get inserted into the database, but nothing is actually happening.

I created the database with Visual Studio. I right-clicked on "Data connections" in Server Explorer in Visual Studio 2019 Community edition and then clicked on "Create New SQL Server Database".

using (SqlConnection con = new SqlConnection("Data Source = (localdb)\\mssqllocaldb; Initial Catalog = myTestDB; Integrated Security = True; Pooling = False")) 
{
    con.Open();

    SqlCommand cmd = new SqlCommand("INSER INTO [tesTable] VALUES (@fname, @lname)", con);

    cmd.Parameters.AddWithValue("@fname", textBox1.Text);
    cmd.Parameters.AddWithValue("@lname", textBox2.Text);
}

MessageBox.Show("done!");
3
  • 3
    A command needs to be executed before anything happens database side. As a side note, you have used an INSERT command without naming the columns involved. This requires the database table to have exactly the same number of columns in the same exact order in which you add the parameters Commented Aug 11, 2019 at 8:00
  • I am suggesting to check the query Sql Profiler after executing your query ,to know the exact query that is being implemented. Commented Aug 11, 2019 at 8:12
  • 1
    You should check out Can we stop using AddWithValue() already? and stop using .AddWithValue() - it can lead to unexpected and surprising results... Commented Aug 11, 2019 at 9:12

2 Answers 2

4

Because you haven't executed your command:

....
cmd.Parameters.AddWithValue("@lname", textBox2.Text);

cmd.ExecuteNonQuery();

The fixed version of your insert statement should be something like this:

"INSERT INTO tesTable (fname,lname) VALUES (@fname,@lname)"

This also needs to be added, that specifying the type directly and use the Value property is more better than AddWithValue:

cmd.Parameters.Add("@lname", SqlDbType.VarChar).Value = textBox2.Text;

Can we stop using AddWithValue() already?

Sign up to request clarification or add additional context in comments.

6 Comments

I did it but I'm getting this error after adding cmd.ExecuteNonQuery();:System.Data.SqlClient.SqlException: 'Incorrect syntax near the keyword 'INTO'.'
Yes, two columns exactly.
@kiarashyoosefi What if you try this: INSERT INTO tesTable (fname,lname) VALUES (@fname,@lname) ? Just make sure the columns in sql are fname and lname exactly as well.
Notice the typo INSER INTO, missing the T...:-)
Also that tesTable seems suspect. Probably the OP has a malfunctioning T key.
|
0
private void btnaddsave_Click(object sender, EventArgs e)
{
    try
    {
        string connection1 = "server=localhost;user id=root;password=1234;database=library_system";
        con = new MySqlConnection(connection1);
        con.Open();
        string q = "Insert Into library_system.add_book(Book_No,Book_Name)values('"+tbbno.Text+"','" + tbbn.Text+ "')";
        cm1 = new MySqlCommand(q, con);
        cm1.ExecuteNonQuery();
        //con.Open();

        MessageBox.Show("data saved", "information", MessageBoxButtons.OK, MessageBoxIcon.Information);
        con.Close();
        // load();
        tbbno.Text = "";
        tbbn.Text = "";
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.