1

My code:

// Get Connection String
string conn = WebConfigurationManager.ConnectionStrings["GraduatesConnectionString"].ToString();
// Create connection object
SqlConnection connection = new SqlConnection(conn);
SqlCommand command = connection.CreateCommand();
try
{
    // Open the connection.
    connection.Open();
    // Execute the insert command.
    command.CommandText = ("INSERT INTO PersonalInfo(Id,Name,LastName,ContactNumber, Address,Gender, Date_Of_Birth) VALUES(\'"
                + (this.txtID.Text + ("\',\'"
                + (this.txtName.Text + ("\',\'"
                + (this.txtLastName.Text + ("\',\'"
                + (this.txtContactNumber.Text + ("\',\'"
                + (this.txtAddress.Text + ("\',\'"
                + (this.gender + ("\',\'"
                + (this.txtDateofBirth.Text + ("\',\'"
             )))));
    command.ExecuteNonQuery();
}
finally
{
    // Close the connection.
    connection.Close();
}
James
  • 75,060
  • 17
  • 154
  • 220
Alfrezo
  • 19
  • 2

3 Answers3

4
using (SqlConnection connection = new SqlConnection(connectionString))
using (SqlCommand command = connection.CreateCommand())
{
    command.CommandText = "INSERT INTO PersonalInfo (Id, Name, LastName, ContactNumber, Address, Gender, Date_Of_Birth) VALUES (@Id, @Name, @LastName, @LastName, @Address, @Gender, @DateOfBirth)";

    command.Parameters.AddWithValue("@Id", txtID.Text);
    ...

    connection.Open();
    command.ExecuteNonQuery();
}
abatishchev
  • 92,232
  • 78
  • 284
  • 421
  • You didn't actually answer the OP's question you simply re-wrote their code - albeit in a better more reliable way. However, this could have simply been a comment (like mines & @podiluska's). @RonaldWildenberg's answer is more appropriate as he has at least said *why* the exception is being raised. – James Sep 18 '12 at 09:11
3

You are missing a closing ) after txtDateofBirth so your statement is incomplete.

BUT please take note of the comment of @podiluska. This code is really easy to abuse. Suppose I enter something like the following text in txtDateofBirth:

;DROP TABLE PersonalInfo;

You then get a query like:

INSERT INTO PersonalInfo(...)
VALUES (...);DROP TABLE PersonalInfo;

So please use parameterized queries as described by @abatishchev.

Ronald Wildenberg
  • 30,401
  • 12
  • 82
  • 127
1

I'd be tempted to change your code to:

string conn = WebConfigurationManager.ConnectionStrings["GraduatesConnectionString"].ToString();
// Create connection object
using(SqlConnection connection = new SqlConnection(conn))
{
    string queryText = "INSERT INTO PersonalInfo(Id,Name,LastName,ContactNumber, Address,Gender, Date_Of_Birth) VALUES(@id,@name,@lastName,@contactNumber, @address,@gender, @date_Of_Birth)";

    using(SqlCommand command = new SqlCommand(queryText, connection))
    {
        try
        {
            // Open the connection.
            connection.Open();

            command.Parameters.AddWithValue("@id", this.txtID.Text);
            command.Parameters.AddWithValue("@name", this.txtName.Text);
            command.Parameters.AddWithValue("@lastName", this.txtLastName.Text);
            command.Parameters.AddWithValue("@contactNumber", this.txtContactNumber.Text);
            command.Parameters.AddWithValue("@address", this.txtAddress.Text);
            command.Parameters.AddWithValue("@gender",this.gender );
            command.Parameters.AddWithValue("@date_Of_Birth", this.txtDateofBirth.Text);
            command.ExecuteReader();
        }
        finally
        {   
            // Close the connection.
            if(connection.State != ConnectionState.Closed)
                connection.Close();
        }
    }
}
HaemEternal
  • 2,169
  • 5
  • 28
  • 50