Solved

MissingPrimaryKeyException Error while Searching an Access Table in VB2008

Posted on 2008-10-29
6
1,126 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 70

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

831 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