mechwarrior3
Well-known member
- Joined
- Dec 19, 2005
- Messages
- 67
- Programming Experience
- Beginner
I have an MS Access 2003 database that I am trying to interact with using ADO .NET methods. I generate a data adapter complete with select, insert and update commands. Now, this database does not have a primary key defined. Now, when I want to update a row in the database, I am trying to use the .Find method. However, the .Find method requires a primary key so what I am trying to do is set a primary key within the datatable that is retrieved from the database through my SELECT command. However, this isn't working. Here is my code:
At the .PrimaryKey line I get this error, "An unhandled exception of type 'System.ArgumentException' occurred in system.data.dll.
Additional information: These columns don't currently have unique values."
What does this error mean? Why do the columns have to have unique values to be set as primary keys? Also, is there anyway to work around this so that I can still set a primary key in my datatable to perform the .Find method? Any suggestions and/or advice is greatly appreciated.
VB.NET:
[SIZE=2]AccessAdapter.SelectCommand = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] OleDb.OleDbCommand("SELECT * FROM tbl_HCI_Lot_Inspection_Data", AccessConn)
[/SIZE][SIZE=2]AccessAdapter.Fill(AccessDataSet, "tbl_HCI_Lot_Inspection_Data")
AccessDataSet.Tables("tbl_HCI_Lot_Inspection_Data").PrimaryKey = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] DataColumn() _[/SIZE]
[SIZE=2]{AccessDataSet.Tables("tbl_HCI_Lot_Inspection_Data").Columns("strLotNumber"), AccessDataSet.Tables("tbl_HCI_Lot_Inspection_Data").Columns("strMixID")}[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] strMixNum [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = txtMixLotNum.Text
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] strMfgNum [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = txtMfgLotNum.Text
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] keyArray() [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = {strMfgNum, strMixNum}
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] AccessDataRow [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] DataRow = AccessDataSet.Tables(0).Rows.Find(keyArray)
[/SIZE]
At the .PrimaryKey line I get this error, "An unhandled exception of type 'System.ArgumentException' occurred in system.data.dll.
Additional information: These columns don't currently have unique values."
What does this error mean? Why do the columns have to have unique values to be set as primary keys? Also, is there anyway to work around this so that I can still set a primary key in my datatable to perform the .Find method? Any suggestions and/or advice is greatly appreciated.