VB.net代码-显示'System.NullReferenceException'Oledb

时间:2019-03-07 09:22:52

标签: vb.net

  

错误 在cust_db.exe中发生了类型为'System.NullReferenceException'的未处理异常   信息:对象引用未设置为对象的实例。在   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

0 个答案:

没有答案