Question Why my table doesn't add new row?

NewComer

Active member
Joined
Jan 2, 2010
Messages
34
Programming Experience
Beginner
I try to add a new row if it doesn't exist as the following codes:

dbConn.Open()

strSQL = "SELECT * FROM Configuration WHERE Title = 'Launch WEB'"
da = New OleDbDataAdapter(strSQL, dbConn)
dt = New DataTable
da.Fill(dt)

cnt = dt.Rows.Count()

If Not (cnt > 0) Then
Dim dsNewRow As DataRow

dsNewRow = dt.NewRow()
dsNewRow.Item(0) = "Launch WEB"
dsNewRow.Item(1) = "YES"
dt.Rows.Add(dsNewRow)
End If

dt.Dispose()
da.Dispose()
dbConn.Close()


But even there is no error happen & the row doesn't exit, when I verify table Configuration ... I don't see any new row

Anyone knows why?
 
So your DataTable doesn't have a new row or you don't get a new row in the database?

VB.NET:
dt.Rows.Add(dsNewRow)
cnt = dt.Rows.Count()

This should show a number 1 higher than before because you're adding it to the DataTable. If you're wanting the record to show up in the database you're going to need to call Update on your DataAdapter.
 
Re:

I don't have a new row in my table!

I am still looking how to Update with DataAdapter but I have not found any available links, can you help?
 
This works just fine for me.

VB.NET:
        Dim ds As New DataSet
        Dim dt As New DataTable("Sample")

        With dt.Columns
            .Add("Column1", GetType(Integer))
            .Add("Column2", GetType(String))
        End With

        Dim cnt As Integer = dt.Rows.Count

        For i As Integer = 0 To 99
            Dim row As DataRow = dt.NewRow()
            row.Item("Column1") = i
            row.Item("Column2") = "Record " & i
            'row.Item(0) = i
            'row.Item(1) = "Record " & i
            dt.Rows.Add(row)
        Next

        cnt = dt.Rows.Count()
 
I did as your posted and the final cnt = 1 (as 1 new row added), however when I did:

dt.Dispose()
da.Dispose()

Then re-scan the same table again, it isn't there! Looks like it hasn't update the table.

I am using VB2008 & MS Access 2003 ... might be required some thing else?
 
That's because you're adding it to the DataTable. Once you've got the record in the DataTable you need to Insert the record into the DataBase.
 
I found the problem

After trying with a lot different ways (Insert, add.row ...) it should work all, but it didn't!

Then I have just recognized that my 2 columns are named conflictly with SQL syntax (Title and Value)

Therefore, I enclose them as [Value] and [Title] then it works!

Thanks for trying to help, I will keep in-mind all comments :D
 
Back
Top