Last post Apr 16, 2009 12:01 AM by greatnaqvi
Jan 21, 2008 01:45 PM|pentup|LINK
I'm new to ASP/VS/Linq and I'm having a small problem.
I have one table setup in SQL Server Express 2005 through Visual Studio 2008. The table name is "Users" and has three columns (accountID, userName, email). AccountID is the primary key and set to auto incriment. I've added a couple of records by hand
and it works.
I have a single form with a button, a label, and two text boxes. The button code is below. After entering some fake data that does not already exist in the database and clicking the button I get this.
Cannot insert explicit value for identity column in table 'Users' when IDENTITY_INSERT is set to OFF.
I understand that it is trying to insert something into the accountID field but I don't understand why since I'm only providing a username and e-mail address to insert.
Your help is greatly appreciated.
protected void Button1_Click(object sender, EventArgs e)
MyDatabaseDataContext db = new MyDatabaseDataContext();
var query = from u in db.Users
where u.email == txtEmail.Text
var count = query.Count();
if (count == 0)
//Create a new user object.
User newUser = new User();
newUser.username = txtUsername.Text;
newUser.email = txtEmail.Text;
//Add the user to the User table.
Label1.Text = txtEmail.Text + " already exists in the database.";
Jan 21, 2008 10:28 PM|pentup|LINK
This is driving me crazy. Any LINQ guru's out there?
Jan 22, 2008 10:07 PM|Wencui Qian - MSFT|LINK
It is a strange issue as all of your codes run well on my computer. So I think you maybe have some issues when creating the table. Please check the table definition, and if convenient, you can re-create the table to see whether the table
has any problems. This is the my .sql scripts to create the table.
Hope my suggestion helps you!
Jan 23, 2008 10:59 AM|pentup|LINK
Thanks for your reply but I found the answer on another forum.
Check the primary key on the mapping class. It should have an attribute something like this:
<div class=codeseg> <div class=codecontent> <div class=codesniptitle>[Column(Storage="_FileID",
AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL IDENTITY", IsPrimaryKey=true,
If you're using the designer, just regenerate this class (by deleting the table in the designer and dropping it from the server explorer).
Apr 22, 2008 04:24 PM|BFreakout|LINK
[Yes] Thank you!!!!! i have the self problem and your post help me...
Mar 18, 2009 12:28 PM|henesse|LINK
If you made changes to the table in the db after you created your table in the server explorer (*.dbml file)
You need to drop the table from the designer and add it back in to the server explorer.
Apr 16, 2009 12:01 AM|greatnaqvi|LINK
U have made a table and map into linq.. file for example db.html. then after that u go into database and set identity yes... and not refresh the db.html
refresh linq file after making changes in database.. then u will not find this problem..
Assistant Manager IT