简体   繁体   中英

A An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code

I'm trying to insert records in ASP.NET to table but I don't know why I am getting this error. Please help me solve this error. Help is appreciated.

This is the screenshot of table:

[![enter image description here][1]][1]

This is the error code I am getting:

An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code

This is Exceptional error image:

[![2][2]][2]

There are two problems in your query:

  1. Table is a keyword in SQL, so you cannot use it in that way. Either rename the table and give a name that is no keyword in SQL or escape the name in brackets:

     INSERT INTO [Table]... 

    Obviously, that is what the exception message tells you.

  2. If your id column is not auto-incremented, you need to pass a value for this column, too, because it must not be NULL as your table declaration shows.

Try to rename the table name. Also, you can put a breakpoint after you're setting the cmd object to see how the insert statement looks.

SqlCommand cmd = new SqlCommand("insert into TableName values(@username)",con);
cmd.Parameters.AddWithValue("@username", Username.Text);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM