Question

I can't search my datatable. It keeps repeating the same error "table doesn't have a primary key." and I don't know what to do. I already added the .FillSchema, but it still won't work. I'm using Visual Basic.

    Dim con As New OleDb.OleDbConnection
    Dim da As New OleDb.OleDbDataAdapter
    Dim ds As New DataSet
    Dim sql As String
    Dim pass As Integer
    Dim cmd As New OleDb.OleDbCommand
    Dim dr As OleDb.OleDbDataReader
    Dim dRow As DataRow

    con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Drug Emporium.accdb"
    con.Open()
    cmd.CommandText = "Select * From Inventory"
    cmd.CommandType = CommandType.Text
    cmd.Connection = con
    sql = "Select * From Inventory"
    dr = cmd.ExecuteReader()
    da = New OleDb.OleDbDataAdapter(sql, con)
    da.FillSchema(ds, SchemaType.Source, "Drug Emprorium")
    da.Fill(ds, "Drug Emporium")
    Dim cb As New OleDb.OleDbCommandBuilder(da)
    dRow = ds.Tables("Drug Emporium").Rows.Find(intItemNum)
    dRow.Delete()
    con.Close()

I tried this but it still didn't work. It now gives a null reference exception on the Delete()

    con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Drug Emporium.accdb"
    con.Open()
    cmd.CommandText = "Select * From Inventory"
    cmd.CommandType = CommandType.Text
    cmd.Connection = con
    sql = "Select * From Inventory"
    da = New OleDb.OleDbDataAdapter(sql, con)
    da.Fill(ds, "Drug Emporium")
    da.FillSchema(ds, SchemaType.Source, "Drug Emprorium")
    Dim cb As New OleDb.OleDbCommandBuilder(da)
    ds.Tables("Drug Emporium").PrimaryKey = New DataColumn() {ds.Tables("Drug Emporium").Columns("InventoryNumber")}
    dRow = ds.Tables("Drug Emporium").Rows.Find(intItemNum)
    dRow.Delete()
    con.Close()

No correct solution

OTHER TIPS

Your Problem is in the Table structure. You should normally show the DDL (SQL to create the table), but I understand it is uncommon with Access.

Anyway, I suspect your error happens in the delete: if you want to modify an resultset some Drivers need a primary key in the Table (and the selected colums) in order to be able to sent a clear modification operation back to the DB.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top