0

I'm trying to build a web form that can send information to a database via ASP.Net and Microsoft SQL Server Management Studio.

enter image description here

enter image description here

It says there's incorrect syntax near Table, which is the name of the table in the database I'd like to store the values.

If you need me to post more code for clarification, I'll do that. Does anyone know what could be wrong?

I appreciate any help you can give

Thanks

EDIT:

So I changed my table name to mynameTable and I'm getting this error. It says string or binary data would be truncated

enter image description here

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user3577397
  • 453
  • 3
  • 12
  • 27
  • `Table` is a keyword in SQL. If that really is your table name, surround it with `[]`. – Drew Kennedy Dec 16 '14 at 22:11
  • 1
    If that is really your table name CHANGE it, but you will soon discover that this is not your only problem here. txtFName is a textbox right? – Steve Dec 16 '14 at 22:12
  • Yeah, I named all my text box id values like that. Like txtFName, txtEmail etc.. Why is that wrong? – user3577397 Dec 16 '14 at 22:30

3 Answers3

2

You have at least four issues; two of them cause your INSERT statement to fail (see bold headings below), and two are design flaws (see additional suggestions under each heading below). While I listed the design flaws as "additional suggestions" only, it's actually important to fix these, too.

1. TABLE is a SQL keyword.

TABLE is a SQL keyword. (And so is Table, since SQL syntax is case-insensitive.)

If you actually named your table Table, you need to "escape" the name by putting it in square brackets:

INSERT INTO [Table] …
--          ^     ^
--         add these!

Additional suggestion: Change the table's name so that it hints at what kind of data or facts are stored in the table. Consider that everyone knows that Table is a table. But it is not obvious that Table contains some kind of personal data; so call your table Customers, Employees, Hairdressers, etc. — or if there really isn't any more specific term, call it Persons.

2. You cannot concatenate a string and a TextBox.

It doesn't make sense to append a TextBox into a string:

"… VALUES ('" + txtFName + "', …"

It should be:

"… VALUES ('" + txtFName.Text + "', …"
//                      ^^^^^

Additional suggestion: You are basically dynamically creating a SQL statement using string concatenation. The way how you're doing it opens up the possibility of SQL injection attacks. (This is a kind of security hole; please research this if you don't know what it is!)

Please use parameterized queries instead:

var cmd = new SqlCommand("INSERT INTO Persons (FirstName, LastName, …) VALUES (@FirstName, @LastName, …)", connection);
cmd.Parameters.AddWithValue("@FirstName", txtFName.Text);
cmd.Parameters.AddWithValue("@LastName", txtLName.Text);
…

And your SQL injection security hole is gone. Also, you won't have to worry about correctly escaping quote characters (') that could have been entered by the user in the textboxes.

stakx - no longer contributing
  • 83,039
  • 20
  • 168
  • 268
  • Thanks. I should prolly name it something else. I just put square brackets and it didn't work tho – user3577397 Dec 16 '14 at 22:17
  • 1
    @DrewKennedy: I wholeheartedly agree! I was busy editing my answer while you posted this comment. – stakx - no longer contributing Dec 16 '14 at 22:30
  • Thanks stakx. I'll incorporate your suggestions. Makes sense – user3577397 Dec 16 '14 at 22:50
  • You are doing the lord's work, sir. Thank you so much! That was very helpful. And yikes, I didn't realize I was vulnerable to injections :/ Thanks for the heads up – user3577397 Dec 16 '14 at 23:39
  • This is weird. I followed your directions, and I was able to insert two records no problem. Now I'm getting this error '{"String or binary data would be truncated.\r\nThe statement has been terminated."}' I have no idea why – user3577397 Dec 17 '14 at 01:28
  • because one of your text columns is defined with a smaller capacity than the string's length you're trying to insert. E.g. you're inserting `"ABC"` (length 3) into a column defined as `VARCHAR(2)` (capacity 2; `"C"` will get truncated). – stakx - no longer contributing Dec 17 '14 at 06:47
2

Table is a keyword. You should create a new name for your table.

Reference: http://msdn.microsoft.com/en-us/library/ms189822%28v=sql.105%29.aspx

display name
  • 4,165
  • 2
  • 27
  • 52
2

You are using a reserved keyword TABLE and the first thing to do is to change that name in something more comprehensible. If you cannot change that name then you should use the square brakets around that name.

But this is just the first problem. Then you have another one.
You try to create your sql command concatenating strings but you put a textbox object in the concatenation instead of the Text property of the textbox.

SqlCommand cmd = new SqlCommand("INSERT INTO [TABLE] VALUES('" + txtFName.Text + "',....

And this is still very wrong because this result in code open to SQL Injection, so what you really need to do is start using a parameterized query

SqlCommand cmd = new SqlCommand("INSERT INTO [TABLE] VALUES(@fname, @lname, ..... ");
cmd.Parameters.AddWithValue("@fname", txtFName.Text);
cmd.Parameters.AddWithValue("@lname", txtLName.Text);
... and so on for all the others fields ....
Community
  • 1
  • 1
Steve
  • 213,761
  • 22
  • 232
  • 286
  • I followed the directions you guys gave me, and it worked for two records, and now I'm getting an error. This is the error '{"String or binary data would be truncated.\r\nThe statement has been terminated."}' Why is that? It worked before, now all of the sudden I get this error.. – user3577397 Dec 17 '14 at 01:29
  • It is simply a mismatch between the data length of your inputs and the data length allowed by your database fields. You should not pass more data than the database fields expect. Usually you could prevent this problem setting the MaxLength property on the user interface controls. IE _txtFName.MaxLength = _ has the FirstName field. – Steve Dec 17 '14 at 07:38
  • Thanks. Yeah, I noticed last night. I had varchar(10) for Credit cards, but when I put American Express, since it's more than 10 characters it didn't work. – user3577397 Dec 17 '14 at 15:37