Home All Groups Group Topic Archive Search About

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

Author
10 Jun 2005 6:08 AM
Nathan Sokalski
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.
--
Nathan Sokalski
njsokal***@hotmail.com
http://www.nathansokalski.com/

Author
10 Jun 2005 7:36 AM
Miha Markic [MVP C#]
Hi Nathan,

Why don't you use a strong typed dataset (created at design time)?
The other option is to use dataadapterSelect.FillSchema method to create proper structure at runtime (time consuming operation)

--
Miha Markic [MVP C#] - RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/
SLODUG - Slovene Developer Users Group www.codezone-si.info
  "Nathan Sokalski" <njsokal***@hotmail.com> wrote in message news:ORGbJLYbFHA.2440@TK2MSFTNGP10.phx.gbl...
  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.
  --
  Nathan Sokalski
  njsokal***@hotmail.com
  http://www.nathansokalski.com/
Are all your drivers up to date? click for free checkup

Author
10 Jun 2005 4:56 PM
Nathan Sokalski
I attempted to use the FillSchema method 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.FillSchema(ratedpoems, SchemaType.Mapped)

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


I tried using both the SchemaType.Mapped and SchemaType.Source, and neither one worked. I would prefer to have the code automatically detect and set the schema, but if necessary, how do I use a "strong typed dataset"? I never had to use those when I used a database in ASP.NET before (although I have always used Oracle 9i at a college for class assignments until now also). If you could give a small example of how to use a "strong typed dataset" I would appreciate it. Thanks.
--
Nathan Sokalski
njsokal***@hotmail.com
http://www.nathansokalski.com/
  "Miha Markic [MVP C#]" <miha at rthand com> wrote in message news:OzxYf8YbFHA.3464@tk2msftngp13.phx.gbl...
  Hi Nathan,

  Why don't you use a strong typed dataset (created at design time)?
  The other option is to use dataadapterSelect.FillSchema method to create proper structure at runtime (time consuming operation)

  --
  Miha Markic [MVP C#] - RightHand .NET consulting & development www.rthand.com
  Blog: http://cs.rthand.com/blogs/blog_with_righthand/
  SLODUG - Slovene Developer Users Group www.codezone-si.info
    "Nathan Sokalski" <njsokal***@hotmail.com> wrote in message news:ORGbJLYbFHA.2440@TK2MSFTNGP10.phx.gbl...
    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.
    --
    Nathan Sokalski
    njsokal***@hotmail.com
    http://www.nathansokalski.com/
Author
11 Jun 2005 7:25 AM
Miha Markic [MVP C#]
Hi Nathan,

I suspect that your problems is with table names.
Instead of
dataadapterSelect.FillSchema(ratedpoems, SchemaType.Mapped)

dataadapterSelect.Fill(ratedpoems, "poemratings")

do this:

ratedpoems.Tables.Add(new DataTable());

dataadapterSelect.FillSchema(ratedpoems.Tables[0], SchemaType.Mapped)

dataadapterSelect.Fill(ratedpoems.Tables[0])

In this way, you are sure that you are always filling the same table.

As per strong typed dataset:

Create a dataadapter with select command (perhaps by dragging&droping a table from Server Explorer window), right click on it, choose Generate Dataset... and there you go.

And please, don't cross post ado.net questions..


--
Miha Markic [MVP C#] - RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/
SLODUG - Slovene Developer Users Group www.codezone-si.info

  "Nathan Sokalski" <njsokal***@hotmail.com> wrote in message news:unWTb1dbFHA.2884@tk2msftngp13.phx.gbl...
  I attempted to use the FillSchema method 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.FillSchema(ratedpoems, SchemaType.Mapped)

  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


  I tried using both the SchemaType.Mapped and SchemaType.Source, and neither one worked. I would prefer to have the code automatically detect and set the schema, but if necessary, how do I use a "strong typed dataset"? I never had to use those when I used a database in ASP.NET before (although I have always used Oracle 9i at a college for class assignments until now also). If you could give a small example of how to use a "strong typed dataset" I would appreciate it. Thanks.
  --
  Nathan Sokalski
  njsokal***@hotmail.com
  http://www.nathansokalski.com/
    "Miha Markic [MVP C#]" <miha at rthand com> wrote in message news:OzxYf8YbFHA.3464@tk2msftngp13.phx.gbl...
    Hi Nathan,

    Why don't you use a strong typed dataset (created at design time)?
    The other option is to use dataadapterSelect.FillSchema method to create proper structure at runtime (time consuming operation)

    --
    Miha Markic [MVP C#] - RightHand .NET consulting & development www.rthand.com
    Blog: http://cs.rthand.com/blogs/blog_with_righthand/
    SLODUG - Slovene Developer Users Group www.codezone-si.info
      "Nathan Sokalski" <njsokal***@hotmail.com> wrote in message news:ORGbJLYbFHA.2440@TK2MSFTNGP10.phx.gbl...
      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.
      --
      Nathan Sokalski
      njsokal***@hotmail.com
      http://www.nathansokalski.com/
Author
10 Jun 2005 7:42 AM
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
-----------------
"Nathan Sokalski" <njsokal***@hotmail.com> wrote in message
news:ORGbJLYbFHA.2440@TK2MSFTNGP10.phx.gbl...
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.
--
Nathan Sokalski
njsokal***@hotmail.com
http://www.nathansokalski.com/

Bookmark and Share