why is this code not working?(database)

qaisqais

Member
Joined
Mar 29, 2010
Messages
14
Programming Experience
Beginner
error i'm getting : Object reference not set to an instance of an object.
VB.NET:
Public Class CreateInvoice
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 CreateInvoice_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:\Documents and Settings\Administrator\My Documents\Visual Studio 2010\Projects\mySystemRemake\SystemDb.mdb"
        con.ConnectionString = dbProvider & dbSource

    End Sub

 Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        MsgBox("Do you wish to add this Invoice?", MsgBoxStyle.YesNo, "")
        If MsgBoxResult.Yes Then
            con.Open()
            sql = "SELECT * FROM SalesTbl"
            da = New OleDb.OleDbDataAdapter(sql, con)
            Dim cb As New OleDb.OleDbCommandBuilder(da)
            Dim newentry As DataRow
            newentry = ds.Tables("SalesTbl").NewRow()
            newentry.Item("SaleDate") = MaskedTextBox1.Text
            newentry.Item("Subtotal") = TextBox1.Text
            newentry.Item("Discounts") = TextBox2.Text
            newentry.Item("Total") = TextBox3.Text
            newentry.Item("Details") = RichTextBox1.Text
            newentry.Item("SoldTo") = ComboBox1.Text
            ds.Tables("SalesTbl").Rows.Add(newentry)
            da.Update(ds, "SalesTbl")
            con.Close()
            MsgBox("Invoice succesfully added to Database")
        Else
            Me.Close()
        End If
    End Sub
 
Most likely because there are no tables in the dataset ds, yet you are attempting to reference them when assigning a value to the DataRow newentry. It seems you are missing the code to load the dataset.

To be able to verify what variable is not correctly instantiated, set breakpoints in your code. When you come to a breakpoint, you can hover over the variables in Visual Studio and a popup will appear containing their value.
 
Back
Top