Link to home
Start Free TrialLog in
Avatar of esmyhs
esmyhs

asked on

MissingPrimaryKeyException Error while Searching an Access Table in VB2008

I'm new to VB2008 and ADO.NET. I've been using VB6 with DAO extensively and have just started to upgrade to VB2008.

I'm using the following code to try to search for a Value in an Access table. The field that I am trying to search is called "ID".

        Dim foundRow As DataRow
        Dim findThis As Object

        findThis = 21844
        foundRow = objDataSet.Tables(0).Rows.Find(findThis)

I'm getting the following error message on the last line, even though the ID field is set in the table as a Primary Key field:

System.Data.MissingPrimaryKeyException was unhandled
  Message="Table doesn't have a primary key."

Any help would be appreciated. Thanks.
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

and was your ID field was retreived in your dataset? can you show the code that fills your dataset?
Avatar of esmyhs
esmyhs

ASKER

   Dim objConnection As New OleDbConnection _
        ("Provider=Microsoft.Jet.OLEDB.4.0;Data source=C:\CSBView\View.MDB")

    Dim objDataAdapter As New OleDbDataAdapter _
        ("SELECT Nikud.ID, Nikud.text FROM Nikud ORDER BY Nikud.ID", _
         objConnection)

        'Initialize a new instance of the DataSet object...
        objDataSet = New DataSet

        'Fill the DataSet object with data...
        objDataAdapter.Fill(objDataSet, "Nikud")

        'Set the DataView object to the DataSet object...
        objDataView = New DataView(objDataSet.Tables("Nikud"))
Hi,
Another approach is to use the RowFilter property of the DataView associate with DataTable.
Here is an example.

objDataSet.Tables(0).DefaultView.RowFilter="ID=" & txtID.text
if objDataSet.Tables(0).DefaultView.Count >0 then
   messagebox.show(objDataSet.Tables(0).DefaultView.item(0).item("Text"))
end if
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of esmyhs

ASKER

Thanks. Greatly appreciated.