Saturday, March 31, 2012

TableAdapter Insert (Primary Key)

Hi

I have just started to learn ASP.Net. However I have some experience with ASP.

Anyway I have created a asp-form which should create a new entry in the database after you have clicked the submit button.

I created a tableadapter and following simple code:
tbUsersTableAdapter topAdapter=new tbUsersTableAdapter();
topAdapter.InsertUser(DateTime.Now, DateTime.Now, DateTime.Now,true,false, txtNickname.Text, 2, txtEMail.Text, txtPassword.Text,"");

Here is the SQl Command without the primary key:
INSERTINTO[tbUsers] ([CDate], [MDate], [ADate], [Active], [Activated],[Nickname], [Role],Email, [Password],[IP])VALUES (@dotnet.itags.org.CDate, @dotnet.itags.org.MDate, @dotnet.itags.org.ADate, @dotnet.itags.org.Active, @dotnet.itags.org.Activated, @dotnet.itags.org.Nickname, @dotnet.itags.org.Role, @dotnet.itags.org.eMail, @dotnet.itags.org.Password, @dotnet.itags.org.IP)

This works fine as long as I put the ID (Primary Key) into the Insert command. But I don't know how I can find out the next avaiable primary key. I have thought it would be automatically incremented. Anyway I went to the properties of the tableadapter and there was a property which I switched on for my primary key field "AutoIncrement". However it still doesn't work.
Before I used to work with MS Access and OleDB Connection. There I just skipped the Primary Key Value and it has been created automatically. Here I get an error message that the primary key can't be null.

I hope somebody could help me with that or has an idea what I'm doing wrong.

thx

Hi

I searched and tried out a lot of things. Finally I found the problem today. I had to open the table with the Server Explorer. Actually I set my ID field to Primary Key but I forgot to set the Identity Column. After I set the Identity Column to my ID field it worked... great;-)))) Now I can just add a new record with skipping the Primary Key and it will be generated automatically.


thx all

0 comments:

Post a Comment