Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net NullReference未处理异常_Vb.net - Fatal编程技术网

Vb.net NullReference未处理异常

Vb.net NullReference未处理异常,vb.net,Vb.net,我使用以下代码验证日期和时间值,以查看数据库中datagridview1中的记录。代码的底部根据用户输入的内容更改查询,我希望相应地显示结果 如果给出了textbox1、textbox2等的所有4个值,则会显示预期结果,但如果任何文本框为空,则会显示Null reference was unhandled exception(空引用未处理),并显示以下消息: 对象引用未设置为对象的实例 它指向: Form7.DataGridView1.Columns("booking_time").Defaul

我使用以下代码验证日期和时间值,以查看数据库中
datagridview1
中的记录。代码的底部根据用户输入的内容更改查询,我希望相应地显示结果

如果给出了
textbox1
textbox2
等的所有4个值,则会显示预期结果,但如果任何文本框为空,则会显示Null reference was unhandled exception(空引用未处理),并显示以下消息:

对象引用未设置为对象的实例

它指向:

Form7.DataGridView1.Columns("booking_time").DefaultCellStyle.Format = "T"
获取文本框值并将其传递给sql查询的代码为:

Textbox1
取“日期开始”的值。
Textbox2
显示“日期到”的值。
Textbox3
获取“时间自”的值。
Textbox4
取“到达时间”的值

所有值筛选在特定日期/日期或时间/时间上或之间显示的记录

我想知道为什么会发生此异常,以及如何解决此问题

If Me.TextBox1.Text = "" Then

            MessageBox.Show("Please Enter a Value For 'Date From'", "Stop", MessageBoxButtons.OK, MessageBoxIcon.Stop)
            Exit Sub
        ElseIf Me.TextBox3.Text = "" Then

            MessageBox.Show("Please Enter a Value For 'Time From'", "Stop", MessageBoxButtons.OK, MessageBoxIcon.Stop)
            Exit Sub
        ElseIf Not IsDate(Me.TextBox1.Text) Or Not IsDate(Me.TextBox2.Text) And Not Me.TextBox2.Text = "" Then

            MessageBox.Show("Please Enter Valid Date Values", "Stop", MessageBoxButtons.OK, MessageBoxIcon.Stop)
            Exit Sub
        ElseIf Not IsDate(Me.TextBox3.Text) Or Not IsDate(Me.TextBox4.Text) And Not Me.TextBox4.Text = "" Then

            MessageBox.Show("Please Enter Valid Time Values", "Stop", MessageBoxButtons.OK, MessageBoxIcon.Stop)
            Exit Sub
        Else
            Dim prmdatefrom As New SqlParameter("@booking_date", SqlDbType.DateTime)
            prmdatefrom.Value = TextBox1.Text

            Dim prmdateto As New SqlParameter("@booking_dat", SqlDbType.DateTime)
            prmdateto.Value = TextBox2.Text

            Dim prmtimefrom As New SqlParameter("@booking_time", SqlDbType.DateTime)
            prmtimefrom.Value = TextBox3.Text

            Dim prmtimeto As New SqlParameter("@booking_tim", SqlDbType.DateTime)
            prmtimeto.Value = TextBox4.Text


            If Me.TextBox2.Text = "" Then
                Dim da As New SqlDataAdapter("select * from Bookings WHERE booking_date = @booking_date  AND DateAdd(day, -dateDiff(day, 0, booking_time), booking_time) Between DATEADD(day, -datediff(day, 0, @booking_time), @booking_time) AND DATEADD(day, -datediff(day, 0, @booking_tim), @booking_tim) AND game = " & x, con)
            ElseIf Me.TextBox2.Text = "" And Me.TextBox4.Text = "" Then
                Dim da As New SqlDataAdapter("select * from Bookings WHERE booking_date = @booking_date  AND DateAdd(day, -dateDiff(day, 0, booking_time), booking_time) = DATEADD(day, -datediff(day, 0, @booking_time), @booking_time) AND game = " & x, con)
            ElseIf Me.TextBox4.Text = "" Then
                Dim da As New SqlDataAdapter("select * from Bookings WHERE booking_date Between @booking_date AND @booking_dat AND DateAdd(day, -dateDiff(day, 0, booking_time), booking_time) = DATEADD(day, -datediff(day, 0, @booking_time), @booking_time) AND game = " & x, con)
            Else
                Dim da As New SqlDataAdapter("select * from Bookings WHERE booking_date Between @booking_date AND @booking_dat AND DateAdd(day, -dateDiff(day, 0, booking_time), booking_time) Between DATEADD(day, -datediff(day, 0, @booking_time), @booking_time) AND DATEADD(day, -datediff(day, 0, @booking_tim), @booking_tim) AND game = " & x, con)


                da.SelectCommand.Parameters.Add(prmdatefrom)
                da.SelectCommand.Parameters.Add(prmdateto)
                da.SelectCommand.Parameters.Add(prmtimefrom)
                da.SelectCommand.Parameters.Add(prmtimeto)
                da.Fill(ds, "Bookings")
            End If
            Form7.DataGridView1.DataSource = ds.Tables("Bookings")      'shows dataset results in the datagridview

            Mod1.ViewBookingDG(x)

            Me.Hide()
            Form7.Show()
        End If
    End If
这是这一行的代码……所有这些都是以特定格式格式化datagridview1,因为我需要将结果显示给用户

公共子视图BookingDG(ByVal x作为字符串)


在导致错误的行周围显示代码。Stacktrace会很好-这意味着您没有在某个地方分配引用变量,但是我们会在没有更多信息的情况下进行猜测。
    Form7.DataGridView1.Columns("booking_time").DefaultCellStyle.Format = "T"                   'shows only time value in the time column
    Form7.DataGridView1.Columns("booking_duration").DefaultCellStyle.Format = ("0 Hours")       'shows the word "Hours" infront of number of hours in the Booking Duration Column
    'Puts customized column names for database columns

    Form7.DataGridView1.Columns(0).HeaderText = "Booking ID"
    Form7.DataGridView1.Columns(1).HeaderText = "Customer ID"
    Form7.DataGridView1.Columns(2).HeaderText = "Customer Name"
    Form7.DataGridView1.Columns(3).HeaderText = "Contact Number"
    Form7.DataGridView1.Columns(4).HeaderText = "Game"

    'shows or omits court number, pool number, table number columns depending on the game
    If x = "'Squash'" Or x = "'Badminton'" Or x = "'Lawn Tennis'" Then
        Form7.DataGridView1.Columns(5).HeaderText = "Court Number"
        Form7.DataGridView1.Columns(5).Visible = True           'shows court number column
        Form7.DataGridView1.Columns(6).Visible = False          'hides pool number column
        Form7.DataGridView1.Columns(7).Visible = False          'hides table number column
    ElseIf x = "'Swimming'" Then

        Form7.DataGridView1.Columns(6).HeaderText = "Pool Number"
        Form7.DataGridView1.Columns(6).Visible = True
        Form7.DataGridView1.Columns(5).Visible = False
        Form7.DataGridView1.Columns(7).Visible = False

    ElseIf x = "'Table Tennis'" Then
        Form7.DataGridView1.Columns(7).HeaderText = "Table Number"
        Form7.DataGridView1.Columns(7).Visible = True
        Form7.DataGridView1.Columns(5).Visible = False
        Form7.DataGridView1.Columns(6).Visible = False
    Else

        Form7.DataGridView1.Columns(5).Visible = False
        Form7.DataGridView1.Columns(6).Visible = False
        Form7.DataGridView1.Columns(7).Visible = False
    End If




    Form7.DataGridView1.Columns(8).HeaderText = "Booking Date"
    Form7.DataGridView1.Columns(9).HeaderText = "Booking Time"
    Form7.DataGridView1.Columns(10).HeaderText = "Booking Duration"

End Sub