Klinetel
Klinetel

Reputation: 302

ASP.NET SQL Insert Error

In ASP.NET, I'm trying to configure a DetailsView control with the insert statement. I run the insert command and I get the error:

"Column 'Address' cannot be null"

My SQL:

INSERT INTO Students (Name, Address,College_Status,Email,Fraternity_Status,Major,Pledge_Class,Ship_ID,Dues_Paid,Service_Hours,Money_Donated,Phone,Shirt_Size,Phone_Carrier,Birthday,Chair_Position,Ritual_Proficiency,Library_Hours)  Values (@Name,@Address,@College_Status,@Email,@Fraternity_Status,@Major,@Pledge_Class,@Ship_ID,@Dues_Paid,@Service_Hours,@Money_Donated,@Phone,@Shirt_Size,@Phone_Carrier,@Birthday,@Chair_Position,@Ritual_Proficiency,@Library_Hours)

Any ideas would be greatly appreciated. I think it's just a simple SQL syntax error. Thanks

Upvotes: 0

Views: 155

Answers (3)

Jetti
Jetti

Reputation: 2458

It would appear that you are not entering all of the columns for the Students table. Try doing

INSERT INTO Students (*column names*) VALUES (*values*)

This will make sure that your query works even if you add a new column as well as help you with a quick sanity check to ensure that you have all of the columns required.

EDIT:

Based on your new results, I see the problem. Your SQL is wrong. It should be something like this:

INSERT INTO Students (Name) VALUES (@Name)

You shouldn't be using the column name = parameter name in the value section.

EDIT 2:

For the "Address cannot be null error", you need to check the value you are inserting into Address. It is a non-nullable field in the database and the value you are entering is null. This should have you return an error to the user saying they need an address.

Upvotes: 2

Drew
Drew

Reputation: 24959

seems kinda simple. are you still stuck ? http://sqlfiddle.com/#!2/47ed30/1

Upvotes: 1

Rumit Parakhiya
Rumit Parakhiya

Reputation: 2714

The syntax you have written for insert looks strange to me.

It should be,

Insert into table_name (col1, col2) values (@val_col1, @val_col2)

Upvotes: 1

Related Questions