Question

I've run into a bit of trouble guys. After days of labouring, debugging and researching, im on the 3rd to last line and im stuck. This isnt the full code, but the relevant parts.

    Dim dbProvider As String
    Dim dbSource As String
    Dim con As New OleDb.OleDbConnection
    Dim ds As New DataSet
    Dim MaxRows As Integer
    Dim sql As String
    Dim TableName As String
    TableName = TbTableName.Text
    Dim da As New OleDb.OleDbDataAdapter("SELECT * FROM  [" & TableName & "]", con)
    Dim cb As New OleDb.OleDbCommandBuilder(da)
    Dim dsNewRow As DataRow
    Dim dsNewColoumn As DataColumn

    dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
    dbSource = "Data Source = E:\A2 Computing\Project\PasswordDatabase.mdb"

    con.ConnectionString = dbProvider & dbSource
    con.Open()

    Dim TableCreate As New OleDb.OleDbCommand("CREATE TABLE [" & TableName & "](" & "ID INTEGER NOT NULL" & ")", con)
    Dim NewColoumn As New OleDb.OleDbCommand("ALTER TABLE [" & TableName & "] ADD " & X & " VARCHAR(60)", con)

    TableCreate.ExecuteNonQuery()
    da.Fill(ds, "NewTable")

    MaxRows = ds.Tables("NewTable").Rows.Count

    ds.Tables("NewTable").PrimaryKey = New DataColumn() {ds.Tables("NewTable").Columns("CustID")}

    X = 0
    Do
        X = X + 1

        dsNewColoumn = ds.Tables("NewTable").Columns.Add
        ds.Tables("NewTable").Columns.Add(X)
        dsNewRow = ds.Tables("NewTable").NewRow()
        ds.Tables("NewTable").Rows.Add(dsNewRow)
    Loop Until X = 30


    da.InsertCommand = cb.GetInsertCommand()
    da.UpdateCommand = cb.GetUpdateCommand()
    da.Update(ds, "NewTable")
End Sub

The problem im having is at this line here

da.UpdateCommand = cb.GetUpdateCommand()

The error is

Dynamic SQL generation for the UpdateCommand is not supported against a SelectCommand that does not return any key column information.

I understand this means my table doesnt have a primary key, but i have set one. Any help would be greatly appreciated! =)

Was it helpful?

Solution

You need the key column in the DB.

The command builder doesn't use the key you set in the datacolumn in the dataset.
In fact, if you look at the code, CB create command used by DA, but CB has no reference to your ds.Tables("NewTable").PrimaryKey, so CB will never be able to take your PrimaryKey in consideration.

So, you need to set a primary key in the DB.

Anyway, why do you have a Database table without a primary key?

Update (after reading the first 9 comments)

  1. You define the Table columns in the TableCreate SQL command, when you execute this command it will create the table AND the column IN the database file.
  2. A table can be empty (no rows) but MUST have at least a column.
  3. You CAN'T use the dataset/datatable abstraction/object to add real column to the real table in the database, it doesent works this way (see point 1)
  4. It give you the error "SSSS.ID' cannot contain a Null" because in the SQL CREATE command you are creating a table with a column called ID that is NOT NULL (see the "ID INTEGER NOT NULL" part of the command) so if you add a row to this table, the column ID MUST contain a value that is not null.
  5. your loop is adding a column at the datatable for each iteration, it doesn't work this way, you cant do that. And if you do, you are doing it wrong.
  6. The column "CustID" you are adding at the datatable exist only in the datatable (the "in-memory" abstraction of the real table) it will never exist in the DB (unless you add it to the CREATE TABLE command)

In my opinion you need to:

  1. Study a good book on RDBMS and SQL (to learn the basics of how a DB works, tables, relations, key, columns, datatype, SQL, null value....)
  2. Read some good article/book on how dataset/datatable/connection interact with a real DB
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top