set key column

aydinozdemir

Member
Joined
May 15, 2011
Messages
15
Programming Experience
Beginner
hello. i ve created a table inside an access file with the code below.
my problem is; after i fill this table to a datagridview and try to save the changes back, i get an error which says "key column" is needed to save changes.how can i set a key column programmatically? thanks for your help.

Creates Table
con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = C:\Lessons\Lessons.mdb"
cmd = New OleDbCommand("CREATE TABLE " & TABLE & "(Day char(2), Lesson1 char(50),Lesson2 char(50))", con)
con.Open()
cmd.ExecuteNonQuery()
con.Close()

Adds Rows Inside This Table
For int = 1 To 30
Try
con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = C:\Lessons\Lessons.mdb"
cmd = New OleDbCommand("INSERT INTO " & TABLE & "(Day) VALUES('" & int.ToString & "');", con)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
Catch ex As Exception
MsgBox(ErrorToString, MsgBoxStyle.Exclamation)
End Try
Next
 
This can be done through the Primary Key declaration when using the CREATE TABLE command.

cmd = New OleDbCommand("CREATE TABLE " & TABLE & "(Day char(2) PRIMARY KEY,Lesson1 char(50),Lesson2 char(50))", con)
 
Back
Top