updating primary key

madbohem

New member
Joined
Aug 26, 2008
Messages
3
Programming Experience
1-3
I am pretty much new using SQL and I am in the midst of trying to add data to a table from inside my program.

In a nutshell, as long as I do not have a primary key set on my table I can fill the table fairly easily. I use this statement:

LogEntry.DataEntryTableAdapter.Insert(Now.ToString, SCF(0), SCF(1), SCF(2), SCF(3), SCF(4), SCF(5), SCF(6))

However, I really need a primary key and when I make a table with a field that has one I can create an error.

Is there an easy way to get the primary key to update itself automatically? I tried to simply removing the colum from my datagridview, but the insert commmand still wants me to input data.

any help is appreciatted. thank you.
 
You seem to be confused as to what a primary key is for.. A PK is a field(s) that uniquely identify a particular row. It should not change and not every table needs one. Tables that will not be updated, or do not care about duplicates, or do not form a resolving-value-to-values role do not need PKs
 
please reread my post. I am stating that without a primary key I can use my example code just fine, with a primary key I get an error. I know what a primary key is for and why i need one. I am confused about how to get the primary key field to automatically generate itself.
 
Last edited:
A primary key isn't something you update. The combination of the fields or the data in the field is what you need to make sure doesn't contain a duplicate value (or combination of values) before you insert it into the table. If it's an AutoNumber field, the DB assigns the number automatically.

Also, you keep saying that you "get an error". This doesn't tell us anything other than what you're doing is wrong. I would post the error message since you can't expect anyone here to help you if no one knows what's really going on.
 
...However, I really need a primary key and when I make a table with a field that has one I can create an error.

Is there an easy way to get the primary key to update itself automatically? I tried to simply removing the colum from my datagridview, but the insert commmand still wants me to input data.

Go to the modify screen for your table and select your primary key column. In the Column Properties menu expand the Identity Specification menu and set the (Is Identity) property to Yes. The Identity Seed (1st value) will default to 1 but you can change it to another number if you need to. The Identity Increment (amount to add each time) will also default to 1 but you can change this as well.

Save the changes to your table and SQL will assign the number when you insert a record.
 
thanks matt. ive set the identity to seed now, but I still can't use my insert command because the parameters of the table require me to put something in on the insert command... basically it states this:

Arguement not specified for parameter RecordNumber (had to name it something).

even if i leave the column out of the datagrid view i can't compile and use my insert.

I guess i am going to have to do some investigating.
 
thanks matt. ive set the identity to seed now, but I still can't use my insert command because the parameters of the table require me to put something in on the insert command... basically it states this:

Arguement not specified for parameter RecordNumber (had to name it something).

even if i leave the column out of the datagrid view i can't compile and use my insert.

I guess i am going to have to do some investigating.

Forms Over Data

While the whole series is very good I would recommend watching the first 3 videos as they go over setting up the database and connecting to it using VB.NET.

It should be readily apparent what cjard means by letting the database handle the identity once you've watched them.
 
Back
Top