-1

I have searched this forum, and have tried a multitude of possible solutions I found, but nothing is working. can anyone shed some light on this situation? Thanks!

SqlConnection con = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=TESTdatabase;Integrated Security=True");
con.Open();

SqlCommand cmd = new SqlCommand(
  "INSERT into tblGenerator (GeneratorName, GeneratorAddress, GeneratorCity, GeneratorState, GeneratorZip, GeneratorPhone, GeneratorContact, GeneratorEPAID) " +
  "VALUES ('" + GenName.Text + "' , '" + GenAdd.Text + "' , '" + GenCity.Text + "' , '" + GenState.Text + "' , '" + GenZip.Text + "' , '" + GenPhone.Text + "' ," +
 " '" + GenContact.Text + "' , '" + GenEPAID.Text + "' ), con");

cmd.ExecuteNonQuery();
con.Close();
GSerg
  • 76,472
  • 17
  • 159
  • 346
Mr.B
  • 1
  • 6

2 Answers2

0

It looks like when you are creating your SqlCommand, you have the connection as part of the Insert statement. Specifically, ", con" is still wrapped inside your text string. If you move your last double quote to after the parenthesis, it should work.

However, I would suggest rewriting your code like this:

using (var con = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=TESTdatabase;Integrated Security=True"))
{
    if(ConnectionState.Closed == con.State) con.Open();
    using (var cmd = con.CreateCommand())
    {
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = $@"INSERT INTO tblGenerator (GeneratorName, GeneratorAddress, GeneratorCity, GeneratorState, GeneratorZip, GeneratorPhone, GeneratorContact, GeneratorEPAID)
                            VALUES ('{GenName.Text}', '{GenAdd.Text}', '{GenCity.Text}', '{GenState.Text}', '{GenZip.Text}', '{GenPhone.Text}', '{GenContact.Text}', '{GenEPAID.Text}')";
        cmd.ExecuteNonQuery();
    }
}
Tim Robinson
  • 445
  • 3
  • 7
0

This is the code that I ended up using. Thanks everyone for your help.

        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)");
        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);



        myConnection.Open();
        myCommand.Connection = myConnection;
        MessageBox.Show("You Have Successfully Added a New Generator To SQL");
        myCommand.ExecuteNonQuery();
        myConnection.Close();
Mr.B
  • 1
  • 6