VB.NET Missing ID on new datarow in dataset

105 Views Asked by At

I have got a problem with establishing ID of a new dataset entry. The new entry is returning NullValue for ID but if i get item(1) or bigger then i get normal data.

    Dim id as integer

    dbProvider = "Provider=Microsoft.JET.OLEDB.4.0;"
    TheDatabase = "\dbMag.mdb"
    MyDocFolder = Directory.GetCurrentDirectory()
    FullDatabasePath = MyDocFolder & TheDatabase
    dbSource = "Data Source = " & FullDatabasePath
    con.ConnectionString = dbProvider & dbSource

    Try
        con.Open()
        sqlString = "SELECT * FROM mag"
        da = New OleDb.OleDbDataAdapter(sqlString, con)
        da.Fill(ds, "mag")
        con.Close()
    Catch ex As Exception
        MsgBox("Błąd: ", Err.Description)
    End Try

    Dim cb As New OleDb.OleDbCommandBuilder(da)

    dsNewRow = ds.Tables("mag").NewRow()
    With dsNewRow
        'do something
    end with
    ds.Tables("mag").Rows.Add(dsNewRow)
    da.Update(ds, "mag")
    lastrow = ds.Tables("mag").Rows.Count - 1
    id = ds.Tables("mag").Rows(lastrow).Item(0)

id is returning NullValue

1

There are 1 best solutions below

0
Mateusz On

Found solution:

        Dim identity = "SELECT @@IDENTITY"
        Dim cmd = New OleDb.OleDbCommand(identity, con)
        id = cmd.ExecuteScalar()