Solved

MissingPrimaryKeyException Error while Searching an Access Table in VB2008

Posted on 2008-10-29
6
1,110 Views
Last Modified: 2013-11-08
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.
0
Comment
Question by:esmyhs
6 Comments
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 22837117
and was your ID field was retreived in your dataset? can you show the code that fills your dataset?
0
 

Author Comment

by:esmyhs
ID: 22837250
   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"))
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 22884271
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
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 22885555
If you are using DataTable.Rows.Find, then you need to set DataTable.Primary to a column, like this:

objDataSet.Tables(0).PrimaryKey = New DataColumn(){objDataSet.Tables(0).Columns("PrimaryKey")}
0
 

Author Closing Comment

by:esmyhs
ID: 31511491
Thanks. Greatly appreciated.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now