Results 1 to 4 of 4

Thread: Can't add new record to access database

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Posts
    3

    Can't add new record to access database

    Hi, as part of a computing project I am writing a program to add, edit and delete data from an access database.

    I have successfully written code to edit data but when adding a row to the dataset I run into a problem.

    This is the code in my button which adds a new record.

    Code:
    Dim cb As New OleDb.OleDbCommandBuilder(da)
            Dim NewRow As DataRow
            NewRow = ds.Tables("CandidateID").NewRow
    
            NewRow.Item(0) = TxtCandidateID.Text
            NewRow.Item(1) = TxtFirstName.Text
            NewRow.Item(2) = TxtSecondName.Text
            NewRow.Item(3) = TxtHospital.Text
            NewRow.Item(4) = TxtPosistionHeld.Text
            NewRow.Item(5) = TxtOTPhysio.Text
            NewRow.Item(6) = TxtNoYearsInHands.Text
            NewRow.Item(7) = TxtSpeciality.Text
            NewRow.Item(8) = TxtBAHTNo.Text
            NewRow.Item(9) = TxtAddressOfHospital.Text
            NewRow.Item(10) = TxtAddressForCorrespondance.Text
            NewRow.Item(11) = TxtTelHome.Text
            NewRow.Item(12) = TxtTelWork.Text
            NewRow.Item(13) = TxtFax.Text
            NewRow.Item(14) = TxtEmail.Text
            NewRow.Item(15) = TxtReceipt.Text
            NewRow.Item(16) = TxtInvoice.Text
            NewRow.Item(17) = TxtDietaryNeeds.Text
            NewRow.Item(18) = TxtPhysicalLearningNeeds.Text
    
            ds.Tables("CandidateID").Rows.Add(NewRow)
    
            da.Update(ds, "CandidateID")
    
            MsgBox("New record added")
    These are my global declarations:

    Code:
    Dim con As New OleDb.OleDbConnection
        Dim ds As New DataSet
        Dim da As OleDb.OleDbDataAdapter
        Dim sql As String = "SELECT * FROM TblCandidate"
    When I click the button I ge the error message : Object reference not set to an instance of an object.
    The line "NewRow = ds.Tables("CandidateID").NewRow" is highlighted.

    Can anyone help?
    Thanks in advance.

    Joe

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Can't add new record to access database

    Where are you ever loading a table into your dataset?

    Dim ds as New DataSet creates a new dataset object, but it has no tables in it based on the code I see that you posted. So when you try to access the CandidateID table, which does not exist in the dataset, you get the null reference exception thrown.

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Posts
    3

    Re: Can't add new record to access database

    O yeah sorry I forgot to post it.

    I fill it in form load

    Code:
     Private Sub FrmCandidate_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = C:\Joe coursework\NESDatabase.mdb"
            con.Open()
            da = New OleDb.OleDbDataAdapter(sql, con)
            da.Fill(ds, "TblCandidate")
            con.Close()
            ReloadText()
        End Sub

  4. #4

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Posts
    3

    Re: Can't add new record to access database

    I figured out what was wrong.
    I had written CandidateID instead of TblCandidate.
    Thanks for the quick reply

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