-2

I am getting this error when trying to run the code below. Any insight on to what I am doing wrong? I am pretty new to this, and want to get this to work pretty bad. I have gotten zero help from this site so far in a previous question I asked. But decided to give this forum another shot, before giving up on stackoverflow

protected void SaveButton_Click(object sender, EventArgs e)
{
    SqlConnection myConnection =
    new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=TESTdatabase;Integrated Security=True");
    SqlCommand myCommand = new SqlCommand(
    "INSERT into tblGenerator (GeneratorName, GeneratorAddress, GeneratorCity, GeneratorState, GeneratorZip, GeneratorPhone, GeneratorContact, GeneratorEPAID)" +
    "VALUES (@GenName, @GenAdd, @GenCity, @GenState, @GenZip, @GenPhone, @GenContact, @GenEPAID), myConnection");
    myCommand.Parameters.AddWithValue("@GeneratorName", GenName.Text);
    myCommand.Parameters.AddWithValue("@GeneratorAddress", GenAdd.Text);
    myCommand.Parameters.AddWithValue("@GeneratorCity", GenCity.Text);
    myCommand.Parameters.AddWithValue("@GeneratorState", GenState.Text);
    myCommand.Parameters.AddWithValue("@GeneratorZip", GenZip.Text);
    myCommand.Parameters.AddWithValue("@GeneratorPhone", GenPhone.Text);
    myCommand.Parameters.AddWithValue("@GeneratorContact", GenContact.Text);
    myCommand.Parameters.AddWithValue("@GeneratorEPAID", GenEPAID.Text);            

    myConnection.Open();
    myCommand.Connection = myConnection;
    myCommand.ExecuteNonQuery();
    myConnection.Close();
}
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Mr.B
  • 1
  • 6
  • 1
    [@GenName] is not the same as [@GeneratorName] – LarsTech Jan 19 '19 at 20:54
  • 2
    Like your previous question, the connection object does not go inside the double quotes. – LarsTech Jan 19 '19 at 20:55
  • Thank you very much for the response, I was mistaken as to what object I was supposed to put in the quotes, and was using the name of the fields from the sql table. – Mr.B Jan 19 '19 at 21:03
  • @Mr.B LarsTech was talking about `myConnection` that you also put inside the query string like in your previous question. You have it highlighted with syntax. You are supposed to call https://learn.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlcommand.-ctor?view=netframework-4.7.2#System_Data_SqlClient_SqlCommand__ctor_System_String_System_Data_SqlClient_SqlConnection_, you are calling https://learn.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlcommand.-ctor?view=netframework-4.7.2#System_Data_SqlClient_SqlCommand__ctor_System_String_. – GSerg Jan 19 '19 at 21:04
  • Aaaaaha. I see now @GSerg. Thank you for that sir. – Mr.B Jan 19 '19 at 21:38
  • I need to thank GSerg, LarsTech, and SomoKRoceS . The three of you combined helped me fix this. I appreciate all of your input, and help! – Mr.B Jan 19 '19 at 21:52

1 Answers1

1

First, Stackoverflow is very helpful :) Don't give up on this platform.

Now for your question:

The parameters your SQL statement is expecting are:

(@GenName, @GenAdd, @GenCity, @GenState, @GenZip, @GenPhone, @GenContact, @GenEPAID)

While you later assign them with different names.

It should be:

    myCommand.Parameters.AddWithValue("@GenName", GenName.Text);
    myCommand.Parameters.AddWithValue("@GenAdd", GenAdd.Text);
    myCommand.Parameters.AddWithValue("@GenCity", GenCity.Text);
    myCommand.Parameters.AddWithValue("@GenState", GenState.Text);
    myCommand.Parameters.AddWithValue("@GenZip", GenZip.Text);
    myCommand.Parameters.AddWithValue("@GenPhone", GenPhone.Text);
    myCommand.Parameters.AddWithValue("@GenContact", GenContact.Text);
    myCommand.Parameters.AddWithValue("@GenEPAID", GenEPAID.Text);

When using @parameter , you must assign a value to a parameter with the same exact name.

SomoKRoceS
  • 2,934
  • 2
  • 19
  • 30