1

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! =)

Mohjo
  • 37
  • 2
  • 7

1 Answers1

1

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
Max
  • 7,408
  • 2
  • 26
  • 32
  • Thanks for quick reply, the table doesnt have a primary key, because the code at runtime makes the table. Considering what you have said I guess I need to find a way to make a primary key before the `da.fill` line is executed. My question is how do i do this? Im nowhere near an expert in programming or using OLEDB however i thought manipulation of the table could only be done through copying it to a data set and then updating the data set to the original table? Well this is what the internet has told me at least =) Thanks again – Mohjo May 08 '12 at 12:54
  • 1
    @mohjo - Just add a primary key yourself then after you create the table: `"ALTER TABLE" & TableName & "ADD PRIMARY KEY (" & ColumName & ")"` – Matt Wilko May 08 '12 at 13:01
  • @MattWilko Ok Im confused xD, do you mean create the table then use command.executenonquery with this as the SQL statement? – Mohjo May 08 '12 at 13:05
  • @MattWilko Hmm, well I thought about this but the problem I thought i'd run into would be that by this point there arent any rows or coloumns so how would I give a coloumn a primary key if it doesnt exist. But ive tried it anyway, I did `Dim Column1 as DataColoumn` then `Dim PrimarySet As New OleDb.OleDbCommand("ALTER TABLE" & TableName & "ADD PRIMARY KEY (" & Column1 & ")", con)` The error im getting now is **Operator '&' is not defined for types 'String' and 'System.Data.DataColumn'.** Any Ideas? – Mohjo May 08 '12 at 13:40
  • Anyone? Can't figure this out. Im sure its something really simple but i just cant get it! – Mohjo May 08 '12 at 13:55
  • 1
    @Mohjo - You need to use the *name* of the column not a new DataColumn object have a look at [this link](http://msdn.microsoft.com/en-us/library/ms188066.aspx) for more information – Matt Wilko May 08 '12 at 14:02
  • @MattWilko Ill be honest, I dont understand almost anything thats on the webpage you gave me the link for. What I do understand is that I cant name a column that i havent made yet. And if i do make it, it will be on the data set and not the actual table. So what do I do? There aren't any coloumn names in the table because there arent any columns :/ Is there any chance you could edit the code above? So i can see where I've gone wrong? – Mohjo May 08 '12 at 14:14
  • Ok figured it out, for anyone looking at this, turns out you can add a primary key and a coloumn to a table regardless if it is the data set or not. And for anyone who is also looking at this, any ideas on this? Comes up on the last line `The field 'SSSS.ID' cannot contain a Null value because the Required property for this field is set to True. Enter a value in this field Does anyone know what this means? My table name is SSSS – Mohjo May 08 '12 at 14:38