Re: ASP.NET tells me my table doesn't have a primary key

K

Ken Tucker [MVP]

Hi,

A primary key doesnt transfer automatically from a database to a
datatable. You need to add it manually.

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

Ken
-----------------
When running my ASP.NET Application that uses an Access database, I recieve
the following error:

[MissingPrimaryKeyException: Table doesn't have a primary key.]
System.Data.DataRowCollection.Contains(Object key) +97
WebApplication1.poetry.ratepoem.btnSubmit_Click(Object sender, EventArgs
e) in c:\inetpub\wwwroot\WebApplication1\poetry\ratepoem.aspx.vb:91
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +108
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String
eventArgument) +57
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain() +1292

However, I know that my table does because when I open the table in Access
it displays the little key icon next to the field as shown below:



The code I use to access the database and fill the DataSet are as follows:

Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnSubmit.Click
Dim ratedpoems As New DataSet
Dim myconnection As System.Data.OleDb.OleDbConnection =
Global.GetDBConnection()
Dim cmdSubmitToDB As New System.Data.OleDb.OleDbCommand
Dim dataadapterSelect As New System.Data.OleDb.OleDbDataAdapter
cmdSubmitToDB.CommandText = "SELECT * FROM poemratings"
cmdSubmitToDB.Connection = myconnection
dataadapterSelect.SelectCommand = cmdSubmitToDB
dataadapterSelect.Fill(ratedpoems, "poemratings")
If ratedpoems.Tables("poemratings").Rows.Contains(currpoem) Then
Dim currrow As DataRow =
ratedpoems.Tables("poemratings").Rows.Find(currpoem)
cmdSubmitToDB.CommandText = "UPDATE poemratings SET totalpoints=" &
(CInt(currrow("totalpoints")) + score) & ", timesrated=" &
(CInt(currrow("timesrated")) + 1) & " WHERE title='" &
CStr(currrow("title")).Replace("'", "''") & "'"
Else
cmdSubmitToDB.CommandText = "INSERT INTO poemratings VALUES('" &
currpoem.Replace("'", "''") & "'," & score & ",1)"
End If
myconnection.Open()
cmdSubmitToDB.ExecuteNonQuery()
myconnection.Close()
End Sub

Why is it telling me I don't have a primary key? Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top