1

I am working on a school project that includes data-bases. I got the basiscs of SQL programming but I still getting problems. Now I'm stuck on the code that is supposed to add new data into the grid, but it's just not working for some reason.. I tried to find a solution for this problem (as you can see from the code), but nothing worked.. Here is the code.

private void InsertData()
    {
        string NewCode = GenerateCode();
        string NewLetter = txtNewData.Text;

        try
        {
            Connection.Open();
            string AddData = "INSERT INTO DictionaryTest (BinNumber,Letter) VALUE (@NewCode,@NewLetter)";
            //SqlDataAdapter DataIns = new SqlDataAdapter(AddData, Connection);
            SqlCommand DataAdd = new SqlCommand(AddData, Connection);
            DataAdd.Parameters.AddWithValue("@NewCode", "00" + NewCode);
            DataAdd.Parameters.AddWithValue("@NewLetter", NewLetter);
            //DataAdd.ExecuteNonQuery();
            //Connection.Open();
            //var dt = new DataTable();
            //DataIns.Fill(dt);
            Connection.Close();
        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }
    }

The data that I'm getting from the fucntion GenerateCode() is ok, I tried to insert it into a text box and it was right.

2
  • You've commented out the code that executes the query, and you need to open the connection before you do. Put your open connection code above your execute query code, and uncomment both :) Commented Dec 29, 2013 at 18:30
  • No problem, obviously I totally missed the pluralization on Values which AkbarRezaee pointed out, which I'm guessing you worked out. Commented Dec 29, 2013 at 18:50

2 Answers 2

2

You need to execute the sqlcommand

try adding this before the close

DataAdd.ExecuteNonQuery();

But beware SqlConnection, and SqlCommand both implement IDisposable, so you should be calling .Dispose on them (or more preferably using a using statement).

This tends to be how I write sql queries:

using(SqlConnection connection = new SqlConnection("connection string"))
{
    using(SqlCommand command = new SqlCommand("command",connection);
    {
        //Add params as in your example
        connection.Open();
        command.ExecuteNonQuery(); // Or Execute reader to get results back
        connection.close(); //The using statement will close dispose, which will close the connection so this isn't strictly required.
    }
}

Hope that helps.

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

3 Comments

Umm how exactly am I using this .Dispose option?
@user3144416 There's a good reference on MSDN here
Here is another good place to start
1

change value to values

INSERT INTO DictionaryTest (BinNumber,Letter) VALUES (@NewCode,@NewLetter)

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.