0

Error An unhandled exception of type 'System.NullReferenceException' occurred in cust_db.exe Additional information: Object reference not set to an instance of an object. in vb.net

Imports System.Data
Imports System.Data.OleDb

Public Class Form1
    Private conn As OleDbConnection
    Private adpt As OleDbDataAdapter
    Private cmd As OleDbCommandBuilder
    Public myds As DataSet
    Private str As String

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim cnstr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\database.mdb"
        conn = New OleDbConnection(cnstr)
        conn.Open()

        myds = New DataSet("customer")

        str = "SELECT * FROM customer"

        adpt = New OleDbDataAdapter(str, conn)
        adpt.SelectCommand.CommandText = str

        cmd = New OleDbCommandBuilder(adpt)
        adpt.Fill(myds, "cust")
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If myds.Tables("cust") Is Nothing Then
            Dim newRow As DataRow = myds.Tables("cust").NewRow

            '   Dim newR As DataRow = myds.Tables("cust").NewRow
            newRow("cust_id") = TextBox1.Text
            newRow("cust_name") = TextBox2.Text
            newRow("cust_address") = TextBox3.Text
            newRow("cust_contactno") = TextBox4.Text

            myds.Tables("cust").Rows.Add(newRow)
            adpt.Update(myds, "cust")
            MessageBox.Show("Record successfully added!")
        End If

    End Sub
End Class
Matt
  • 1,455
  • 4
  • 11
  • 27
  • 2
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – jmcilhinney Mar 07 '19 at 10:15
  • 2
    For future reference, it is good that you posted the error message and the relevant code, but that doesn't constitute a good question. You still need to provide a FULL and CLEAR explanation of the problem, which includes a description of what you're trying to achieve and exactly where the issue arises. – jmcilhinney Mar 07 '19 at 10:16
  • What line is the exception thrown? What's the full stacktrace? – FalcoGer Mar 07 '19 at 12:20
  • 1
    In your `Button1_Click` you check for the existence of a table called "cust", and only if that table does **not** exist, then you're using it. – Nostromo Mar 07 '19 at 12:57

0 Answers0