troubleshooting Question, MS Access query problem - Object reference not set to an instance of an object.

Avatar of gavinandrewmcmillan
gavinandrewmcmillanFlag for Australia asked on
8 Comments1 Solution398 ViewsLast Modified:
Hi All

I am getting the error "Object reference not set to an instance of an object" for the following code on line's 14 and 14 (my sql update command)

Private Sub Form4_gridsetup()

        'creates the database connection for the datagrid
        Dim ConnString As String = Configsettings.ReadSetting("ConString")
        'set the connection string
        Dim DBcon As New OleDb.OleDbConnection
        DBcon.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source =" & ConnString
        'define variables
        Dim ds As New DataSet
        Dim da As OleDb.OleDbDataAdapter
        Dim sql As String
        Dim cmd As OleDb.OleDbCommand
            'update the voucher table to show todays date and also the user ID
            cmd.CommandText = "UPDATE tblVoucher SET Voucher_Redeemed_Date = GETDATE(), Redeemed_By=" & clsUserLevel.UserLevel & " WHERE Store_index=" & cmbStoreName.SelectedValue & " AND Redeemed = False;"
            cmd = Nothing

            'Select items to display in the datagrid
            sql = "SELECT * FROM tblVoucher WHERE Store_Index=" & cmbStoreName.SelectedValue & " AND redeemed = false"
            'fill the dataset
            da = New OleDb.OleDbDataAdapter(sql, DBcon)
            Console.WriteLine("cmbStoreName.SelectedValue: " & cmbStoreName.SelectedValue)
            da.Fill(ds, "tblVoucher")
            dgVoucherTable.DataSource = ds.Tables("tblvoucher")
            Dim oCommandBuilder As New OleDb.OleDbCommandBuilder(da)
            da.Update(ds, "tblvoucher")
            'set the dataset as closed for use by btnSavebutton
        Catch ex As Exception
            MessageBox.Show(ex.Message & " - " & ex.Source)
        End Try
        'pass the dataset and dataadapter to a public dim for use in other functions
        dataset = ds
        dataadapter = da

    End Sub

What exactly causes this error, it is not very descriptive so im not too sure where to start to solve the problem.

Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 8 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 8 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros