MaxCoder88
MaxCoder88

Reputation: 2428

SqlException was unhandled

I'm getting an similar error in the screenshot below. My question is; when I attempt to write to table those informations inside of textboxes,that's gives me error.Where I wrong? By the way,I'm using SQL Server Express 2008. enter image description here

private void btnprnsave_Click(object sender, EventArgs e)
    {          
        SqlConnection conn = DBConfigs.DataCon();
        SqlCommand record = new SqlCommand ("insert into dbo.PersonalInformationTable (PersonalName,PersonalSurname,PersonalEGN) values ("+txtPrnName.Text+","+txtPrnSurName.Text+","+txtPrnEgn.Text+")", conn);
        record.ExecuteNonQuery();
        conn.Close();
   }

Upvotes: 0

Views: 3914

Answers (3)

Tomasz Jaskuλa
Tomasz Jaskuλa

Reputation: 16033

  1. Format properly your SQL (single quotes around tesxtual values or use parameters)

  2. Chekc if the database file exists.

  3. Do not attach the database file at the same time with SQL Server Management Studio Express as you can't attach the same database file to twice to two different SQL Server (Express) instances.

  4. Always make sure you close the database connection. This shouls be achieved with the using statement as regardless how that block of code is left (exception or normal program flow) the Dispose() method will be called. This will always close the connection, and you have avoided a connection leak.

Upvotes: 1

tjg184
tjg184

Reputation: 4686

It's a bit hard to tell, but it looks like you need to escape your values in the insert statement.

"insert into dbo.PersonalInformationTable (PersonalName,PersonalSurname,PersonalEGN) values ('"+txtPrnName.Text+"','"+txtPrnSurName.Text+"','"+txtPrnEgn.Text+"')", conn);

Upvotes: 1

Mr47
Mr47

Reputation: 2655

You need to put single quotes around the textual values in your query. Even better; use parameters...
The exception you get might be caused by something else, but the query in itself is not correct, as stated above...

Upvotes: 0

Related Questions