Solved

MissingPrimaryKeyException Error while Searching an Access Table in VB2008

Posted on 2008-10-29
6
1,139 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

726 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