0

my database query is running but i am facing error "object reference is not set to an instance of 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

I Think error is here but i dnt know how to resolve it

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

error here in this line

If ds.Tables("TICKET_RESERVATION", "FLIGHT_DETAILS").Rows.Count > 0 Then
Ashu Renu
  • 13
  • 4
  • Remove the `Try/Catch` and see what line it blows up on or step through and debug it to find out. Would also help if you post the stack trace. – TyCobb Oct 14 '14 at 16:12
  • if one of your values being returned in the dataset is null, you will get the mentioned exception when trying to cast the value with `.ToString()`. You need to make sure value is not NULL or empty before casting. – Nadeem_MK Oct 14 '14 at 16:22

2 Answers2

1

This is the problem:

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

The DataAdapter does not parse the results into individual tables, so your DataSet wont have a DataTable("TICKET_RESERVATION") nor DataTable("FLIGHT_DETAILS"). The second string is actually for a NameSpace, so since there is no table by that name in that NameSpace, the code is referencing a null object.

.Fill is a function returning the number of rows affected, so you can test that:

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

Either test .Rows.Count or 'NumRowssince the WHERE clause could create an emptyDataTable` (no rows matching the criteria). Then, just reference the first table:

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

Note that in cases when the WHERE clause could return more than one row, the code is arbitrarily using the first one. Even using "TicketNumber" which might be expected to be unique, is probably only unique by Airline. And since it is raw user input who knows whats there (since the criteria is from user input, you should be using parameters).


DataTables can have names, but these are simply assigned by you, which is helpful when you plan to add others to the dataset:

da.Fill(ds, "TICKET_RESERVATION")

Which is what you did in an older version of this question. When you removed the table name parameter, you should have also removed it from the other references.

Since there is only one table/result set and it is a temp DataSet, there is no real reason to do this.

Community
  • 1
  • 1
Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
1

Check your Dataset is nothing or not and then check RowCount. When Dataset is nothing, if you check its RowCount it will gives the error as object reference is not set to an instance of abject

And this is wrong, ds.Tables("TICKET_RESERVATION", "FLIGHT_DETAILS"). you have to use either ds.Tables("TICKET_RESERVATION") OR ds.Tables( "FLIGHT_DETAILS")

Try like this

     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
Vignesh Kumar A
  • 27,863
  • 13
  • 63
  • 115