Results 1 to 4 of 4

Thread: adding new data to database

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2010
    Posts
    2

    adding new data to database

    hi, when i click the add button it says successful but when i look up in the database nothings added..

    its have 2 textboxes for firstname and surname
    1 button for add
    my table is compose of fields FirstName and Surname
    how can i solve this?
    here's my code... anyway thanks in advance
    Code:
    Public Class Form1
        Dim inc As Integer
        Dim con As New OleDb.OleDbConnection
        Dim dbprovider As String
        Dim dbsource As String
        Dim ds As New DataSet
        Dim da As OleDb.OleDbDataAdapter
        Dim sql As String
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            dbprovider = "Provider=Microsoft.JET.OLEDB.4.0;"
            dbsource = "Data Source=C:\AddressBook.mdb"
            con.ConnectionString = dbprovider & dbsource
    
            con.Open()
    
            sql = "SELECT * FROM tblContacts"
            da = New OleDb.OleDbDataAdapter(sql, con)
            da.Fill(ds, "AddressBook")
            con.Close()
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim cb As New OleDb.OleDbCommandBuilder(da)
            Dim dsNewRow As DataRow
            dsNewRow = ds.Tables("AddressBook").NewRow()
            dsNewRow.Item("FirstName") = TextBox1.Text
            dsNewRow.Item("Surname") = TextBox2.Text
            da.Update(ds, "AddressBook")
            MsgBox("New Record added to the Database")
    
        End Sub
    End Class
    Last edited by cutie0407; Jan 30th, 2010 at 12:13 PM.

  2. #2
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: adding new data to database

    First, the message box is misleading because you aren't checking if any rows were affected by the Update method.

    Capture/check the return value of the Update method, something like

    Code:
    Dim Status As Integer =  da.Update(ds, "AddressBook")
    If Status > 0 Then  
        MsgBox("New Record(s) added to the Database")
    Else
        MsgBox("Nothing to Do")
    End If
    Calling NewRow does not immediately create a new record in the DataTable.
    You need to add the new DataRow object to the DataTable.Rows collection.

    Code:
            
    Dim dsNewRow As DataRow
    dsNewRow = ds.Tables("AddressBook").NewRow()
    dsNewRow.Item("FirstName") = TextBox1.Text
    dsNewRow.Item("Surname") = TextBox2.Text
    
    ds.Tables("AddressBook").Rows.Add(dsNewRow)

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2010
    Posts
    2

    Re: adding new data to database

    it returns a value that is affected but it still don't work

  4. #4
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: adding new data to database

    It works fine for me! Post your modified code.

Posting Permissions

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



Click Here to Expand Forum to Full Width