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

时间:2014-10-14 15:57:06

标签: vb.net

我的数据库查询正在运行,但我遇到错误“对象引用未设置为abject的实例”

Try
        If Not con.State = ConnectionState.Open Then
            con.Open()
        End If
        Dim da As New OleDb.OleDbDataAdapter(" SELECT TICKET.TICKET_NO,TICKET.CUSTOMER_ID,FLIGHT.FLIGHT_ID,FLIGHT.FLIGHT_CHARGES FROM TICKET_RESERVATION AS TICKET INNER JOIN FLIGHT_DETAILS AS FLIGHT ON TICKET.FLIGHT_ID = FLIGHT.FLIGHT_ID WHERE [TICKET.TICKET_NO]= '" & txtTicketNo.Text & "'", con)
        Dim ds As New DataSet
        da.Fill(ds)
        If ds.Tables("TICKET_RESERVATION", "FLIGHT_DETAILS").Rows.Count > 0 Then
            txtTicketNo.Text = ds.Tables("TICKET_RESERVATION").Rows(0).Item(0).ToString()
            txtCustomerId.Text = ds.Tables("TICKET_RESERVATION").Rows(0).Item(1).ToString()
            txtFlightId.Text = ds.Tables("TICKET_RESERVATION").Rows(0).Item(2).ToString()
            txtAmount.Text = ds.Tables("FLIGHT_DETAILS").Rows(0).Item(3).ToString()

        End If
        con.Close()
    Catch ex As Exception
        MsgBox(ex.Message.ToString)
    End Try

我认为错误在这里,但我不知道如何解决它

da.Fill(ds)
        If ds.Tables("TICKET_RESERVATION", "FLIGHT_DETAILS").Rows.Count > 0 Then
            txtTicketNo.Text = ds.Tables("TICKET_RESERVATION").Rows(0).Item(0).ToString()
            txtCustomerId.Text = ds.Tables("TICKET_RESERVATION").Rows(0).Item(1).ToString()
            txtFlightId.Text = ds.Tables("TICKET_RESERVATION").Rows(0).Item(2).ToString()
            txtAmount.Text = ds.Tables("FLIGHT_DETAILS").Rows(0).Item(3).ToString()

        End If

此行中的错误

If ds.Tables("TICKET_RESERVATION", "FLIGHT_DETAILS").Rows.Count > 0 Then

2 个答案:

答案 0 :(得分:1)

这是问题所在:

Dim da As New OleDb.OleDbDataAdapter("SELECT  TICKET.TICKET_NO, 
        TICKET.CUSTOMER_ID, FLIGHT.FLIGHT_ID, FLIGHT.FLIGHT_CHARGES 
        FROM TICKET_RESERVATION AS TICKET INNER JOIN 
        FLIGHT_DETAILS AS FLIGHT ON TICKET.FLIGHT_ID = FLIGHT.FLIGHT_ID 
        WHERE [TICKET.TICKET_NO]= '" & txtTicketNo.Text & "'", con)
Dim ds As New DataSet
da.Fill(ds)
If ds.Tables("TICKET_RESERVATION", "FLIGHT_DETAILS").Rows.Count > 0 Then

DataAdapter不会将结果解析为单个表格,因此DataSet不会有DataTable("TICKET_RESERVATION")DataTable("FLIGHT_DETAILS")。第二个字符串实际上是用于NameSpace,因此在该NameSpace中没有该名称的表,代码引用了一个空对象。

.Fill是一个返回受影响行数的函数,因此您可以测试:

Dim NumRows = da.Fill(ds)
If NumRows Then...

测试.Rows.Count或'NumRows since the WHERE clause could create an empty DataTable`(没有符合条件的行)。然后,只需引用第一个表:

If NumRows Then...
    txtTicketNo.Text = ds.Tables(0).Rows(0).Item(0).ToString

请注意,如果WHERE子句可以返回多行,则代码可以任意使用第一行。即使使用可能预期独特的“TicketNumber”,也许只有航空公司才有。并且因为原始用户输入知道那里有什么(因为标准是来自用户输入,所以应该使用参数)。


DataTables 可以拥有名称,但这些只是由您分配,这在您计划将其他人添加到数据集时非常有用:

da.Fill(ds, "TICKET_RESERVATION")

您在an older version of this question中所做的是什么。删除表名参数后,您也应该将其从其他引用中删除。

由于只有一个表/结果集并且它是临时DataSet,因此没有真正的理由这样做。

答案 1 :(得分:1)

检查Dataset是否nothing,然后检查RowCount。当Dataset为空时,如果您检查其RowCount,则会将错误视为object reference is not set to an instance of abject

这是错误的,ds.Tables("TICKET_RESERVATION", "FLIGHT_DETAILS")。你必须使用其中之一 ds.Tables("TICKET_RESERVATION")ds.Tables( "FLIGHT_DETAILS")

试试这个

     Try
        If Not con.State = ConnectionState.Open Then
            con.Open()
        End If
        Dim da As New OleDb.OleDbDataAdapter(" SELECT TICKET.TICKET_NO,TICKET.CUSTOMER_ID,FLIGHT.FLIGHT_ID,FLIGHT.FLIGHT_CHARGES FROM TICKET_RESERVATION AS TICKET INNER JOIN FLIGHT_DETAILS AS FLIGHT ON TICKET.FLIGHT_ID = FLIGHT.FLIGHT_ID WHERE [TICKET.TICKET_NO]= '" & txtTicketNo.Text & "'", con)
        Dim ds As New DataSet
        da.Fill(ds)
        If ds IsNot Nothing AndAlso ds.Tables("TICKET_RESERVATION", "FLIGHT_DETAILS").Rows.Count > 0 Then
            txtTicketNo.Text = ds.Tables("TICKET_RESERVATION").Rows(0).Item(0).ToString()
            txtCustomerId.Text = ds.Tables("TICKET_RESERVATION").Rows(0).Item(1).ToString()
            txtFlightId.Text = ds.Tables("TICKET_RESERVATION").Rows(0).Item(2).ToString()
            txtAmount.Text = ds.Tables("FLIGHT_DETAILS").Rows(0).Item(3).ToString()

        End If
        con.Close()
    Catch ex As Exception
        MsgBox(ex.Message.ToString)
    End Try