+ Reply to Thread
Results 1 to 7 of 7

Thread: Why my table doesn't add new row?

  1. #1
    NewComer is offline VB.NET Forum Enthusiast NewComer is on a distinguished programming path ahead
    .NET Framework
    .NET 2.0
    Join Date
    Jan 2010
    Posts
    31
    Reputation
    9

    Default Why my table doesn't add new row?

    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?

  2. #2
    MattP is offline VB.NET Forum All-Mighty MattP has a reputation beyond repute MattP has a reputation beyond repute MattP has a reputation beyond repute MattP has a reputation beyond repute MattP has a reputation beyond repute MattP has a reputation beyond repute MattP has a reputation beyond repute MattP has a reputation beyond repute MattP has a reputation beyond repute MattP has a reputation beyond repute MattP has a reputation beyond repute
    .NET Framework
    .NET 4.0
    Join Date
    Feb 2008
    Location
    USA
    Posts
    1,016
    Reputation
    533

    Default

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

    Code:
    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.

  3. #3
    NewComer is offline VB.NET Forum Enthusiast NewComer is on a distinguished programming path ahead
    .NET Framework
    .NET 2.0
    Join Date
    Jan 2010
    Posts
    31
    Reputation
    9

    Default 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?

  4. #4
    MattP is offline VB.NET Forum All-Mighty MattP has a reputation beyond repute MattP has a reputation beyond repute MattP has a reputation beyond repute MattP has a reputation beyond repute MattP has a reputation beyond repute MattP has a reputation beyond repute MattP has a reputation beyond repute MattP has a reputation beyond repute MattP has a reputation beyond repute MattP has a reputation beyond repute MattP has a reputation beyond repute
    .NET Framework
    .NET 4.0
    Join Date
    Feb 2008
    Location
    USA
    Posts
    1,016
    Reputation
    533

    Default

    This works just fine for me.

    Code:
            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()

  5. #5
    NewComer is offline VB.NET Forum Enthusiast NewComer is on a distinguished programming path ahead
    .NET Framework
    .NET 2.0
    Join Date
    Jan 2010
    Posts
    31
    Reputation
    9

    Default

    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?

  6. #6
    MattP is offline VB.NET Forum All-Mighty MattP has a reputation beyond repute MattP has a reputation beyond repute MattP has a reputation beyond repute MattP has a reputation beyond repute MattP has a reputation beyond repute MattP has a reputation beyond repute MattP has a reputation beyond repute MattP has a reputation beyond repute MattP has a reputation beyond repute MattP has a reputation beyond repute MattP has a reputation beyond repute
    .NET Framework
    .NET 4.0
    Join Date
    Feb 2008
    Location
    USA
    Posts
    1,016
    Reputation
    533

    Default

    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.

  7. #7
    NewComer is offline VB.NET Forum Enthusiast NewComer is on a distinguished programming path ahead
    .NET Framework
    .NET 2.0
    Join Date
    Jan 2010
    Posts
    31
    Reputation
    9

    Default 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

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts