NullReference是未处理的异常

时间:2011-09-12 09:42:44

标签: vb.net

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

如果给出textbox1textbox2等所有4个值,则会显示预期结果,但如果任何文本框保留为空,则 Null引用为未处理的异常< / em>显示此消息:

  

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

指向:

Form7.DataGridView1.Columns("booking_time").DefaultCellStyle.Format = "T"

获取文本框值并将它们传递给sql查询的代码是:

Textbox1取“Date From”的值 Textbox2显示“Date To”的值 Textbox3取“Time From”的值 Textbox4取值为“Time To”。

所有值过滤哪些记录将显示在特定日期/日期或时间/时间之间或之间。

我想知道为什么会发生这种异常,我该如何解决这个问题......

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,因为我需要向用户显示结果

Public Sub ViewBookingDG(ByVal x As String)

    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

0 个答案:

没有答案