WindowsApplication1.exe中发生System.NullReferenceException附加信息:未将对象引用设置为对象的实例

时间:2013-08-13 16:20:53

标签: sql vb.net

我正在使用vb.net

为sql数据库编写一个非常基本的数据库输入表单

执行代码

dsnewrow = ds.Tables("Car Details").NewRow()

我在此之前在同一个子句中声明了dsnewrow:

 Dim dsnewrow As DataRow

和ds在这里声明:

Dim ds As DataSet

在表单的公共类中。

我知道我的ds变量存在问题,但我不明白它是什么,有人可以帮忙吗?

完整的计划:

Imports System.Data.SqlClient
Imports System.Windows.Forms

Public Class Form1
    Dim inc As Integer
    Dim ds As New DataSet
    Dim da As SqlDataAdapter
    Dim maxrows As Integer

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
        Dim cb As SqlCommandBuilder = New SqlCommandBuilder(da)

        If MessageBox.Show("Are you sure that you want to delete this record?", _
                           "Delete", MessageBoxButtons.YesNo, _
                           MessageBoxIcon.Warning) = Windows.Forms.DialogResult.No Then
            MsgBox("Operation Cancelled")
        Else
            ds.Tables("Car Details").Rows(inc).Delete()
            maxrows = maxrows - 1
            inc = 0
            NavigateRecords()
            da.Update(ds, "authors2")
        End If

    End Sub

    Public Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.Car_DetailsTableAdapter.Fill(Me.CourseworkDataSet.Car_Details)
        Dim sqlstring As String
        Dim conn As SqlConnection
        Dim connstring As String


        connstring = "Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Daniel\Documents\Coursework.mdf;Integrated Security=True;Connect Timeout=30"

        conn = New SqlConnection(connstring)
        conn.Open()
        MsgBox("successful")

        sqlstring = "SELECT * FROM Car Details"
        da = New SqlDataAdapter(sqlstring, conn)
        ds = New DataSet
        da.Fill(ds, "Car Details")

        conn.Close()
        conn = Nothing

        maxrows = ds.Tables("Car Details").Rows.Count
        inc = 0
        navigaterecords()
    End Sub

    Private Sub Car_DetailsBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) Handles Car_DetailsBindingNavigatorSaveItem.Click
        Me.Validate()
        Me.Car_DetailsBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.CourseworkDataSet)

    End Sub

    Private Sub NavigateRecords()
        MakeTextBox.Text = ds.Tables("Car Details").Rows(inc).Item(0)
        ModelTextBox.Text = ds.Tables("Car Details").Rows(inc).Item(1)
        Engine_SizeTextBox.Text = ds.Tables("Car Details").Rows(inc).Item(2)
        ColourTextBox.Text = ds.Tables("Car Details").Rows(inc).Item(3)
        DoorsTextBox.Text = ds.Tables("Car Details").Rows(inc).Item(4)
        Buy_PriceTextBox.Text = ds.Tables("Car Details").Rows(inc).Item(5)
        Sell_PriceTextBox = ds.Tables("Car Details").Rows(inc).Item(6)

    End Sub

    Private Sub btnFwd_Click(sender As Object, e As EventArgs) Handles btnFwd.Click
        If inc <> maxrows - 1 Then
            inc = inc + 1
            NavigateRecords()
        Else
            MsgBox("No More Records")

        End If
    End Sub

    Private Sub btnBack_Click(sender As Object, e As EventArgs) Handles btnBack.Click
        If inc > 0 Then
            inc = inc - 1
            NavigateRecords()
        Else
            MsgBox("First Record")
        End If
    End Sub

    Private Sub btnFirst_Click(sender As Object, e As EventArgs) Handles btnFirst.Click
        If inc <> 0 Then
            inc = 0
            NavigateRecords()
        End If

    End Sub

    Private Sub btnLast_Click(sender As Object, e As EventArgs) Handles btnLast.Click
        If inc <> maxrows - 1 Then
            inc = maxrows - 1
            NavigateRecords()

        End If
    End Sub

    Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
        Dim cb As SqlCommandBuilder = New SqlCommandBuilder(da)
        ds.Tables("Car Details").Rows(inc).Item(0) = MakeTextBox.Text
        ds.Tables("Car Details").Rows(inc).Item(1) = ModelTextBox.Text
        ds.Tables("Car Details").Rows(inc).Item(2) = Engine_SizeTextBox.Text
        ds.Tables("Car Details").Rows(inc).Item(3) = ColourTextBox.Text
        ds.Tables("Car Details").Rows(inc).Item(4) = DoorsTextBox.Text
        ds.Tables("Car Details").Rows(inc).Item(5) = Buy_PriceTextBox.Text
        ds.Tables("Car Details").Rows(inc).Item(6) = Sell_PriceTextBox.Text

    End Sub

    Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
        btnUpdate.Enabled = False
        btnAdd.Enabled = False
        btnConfirm.Enabled = True
        btnDelete.Enabled = False

        ModelTextBox.Clear()
        MakeTextBox.Clear()
        Engine_SizeTextBox.Clear()
        ColourTextBox.Clear()
        DoorsTextBox.Clear()
        Buy_PriceTextBox.Clear()
        Sell_PriceTextBox.Clear()
    End Sub

    Private Sub btnConfirm_Click(sender As Object, e As EventArgs) Handles btnConfirm.Click
        If inc <> -1 Then
            Dim cb As SqlCommandBuilder = New SqlCommandBuilder(da)
            Dim dsnewrow As DataRow

            dsnewrow = ds.Tables("Car Details").NewRow()

            dsnewrow.Item("Make") = MakeTextBox.Text
            dsnewrow.Item("Model") = ModelTextBox.Text
            dsnewrow.Item("Engine Size") = Engine_SizeTextBox.Text
            dsnewrow.Item("Colour") = ColourTextBox.Text
            dsnewrow.Item("Doors") = DoorsTextBox.Text
            dsnewrow.Item("Buy Price") = Buy_PriceTextBox.Text
            dsnewrow.Item("Sell Price") = Sell_PriceTextBox.Text

            btnConfirm.Enabled = False
            btnAdd.Enabled = True
            btnUpdate.Enabled = True
            btnDelete.Enabled = True

        End If
    End Sub

    Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
        btnUpdate.Enabled = True
        btnAdd.Enabled = True
        btnConfirm.Enabled = True
        btnDelete.Enabled = True
        inc = 0
        NavigateRecords()
    End Sub
    End Class

2 个答案:

答案 0 :(得分:2)

您只是声明变量ds。您应该先创建一个实例,然后才能使用这些方法。

Dim ds As DataSet

应该是

Dim ds As New DataSet

Dim ds As DataSet
ds = New DataSet

答案 1 :(得分:2)

Form_Load中的这一行是错误的

sqlstring = "SELECT * FROM Car Details"

包含空格时的表名应括在方括号

sqlstring = "SELECT * FROM [Car Details]"

执行查询时,它不起作用,您的数据集永远不会使用new进行初始化。然后,当您尝试使用它时,您将获得NULL引用异常。我想你在the exceptions in a Form_Load event are ignored

的64位系统上运行这段代码