Solved

MissingPrimaryKeyException Error while Searching an Access Table in VB2008

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

910 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

19 Experts available now in Live!

Get 1:1 Help Now