-1

I'm trying to create a DataGridView for one of my forms within my Visual studio application. I have no errors until I 'start' the process and click to go on this specific form, hence the problem is in the form code. Visual studio highlights this code:

    SQL.SQLDA.Fill(SQL.SQLDS, "GettingInfo")

And says: "NullReferenceException was unhandled "

It then states "Object reference not set to an instance of an object."

The code for the form:

    Public Class Form4
Dim SQL As New SQLControl



Private Sub Form4_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    With DGVData

        .Rows.Clear()
        .ColumnCount = 3


        .Columns(0).HeaderText = "Booking ID"
        .Columns(0).Width = 75
        .Columns(0).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        .Columns(0).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter


        .Columns(1).HeaderText = "Payment Confirmation"
        .Columns(1).Width = 100
        .Columns(1).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter

        .Columns(2).HeaderText = "Total Cost"
        .Columns(2).Width = 100
        .Columns(2).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter



    End With

    LoadBookingData()
End Sub
Public Sub LoadBookingData()
    Dim loadSQL As String = "SELECT * FROM booking"
    Dim RowsCount As Integer


    If SQL.SQLCon.State = ConnectionState.Closed Then
        SQL.SQLCon.open()
        SQL.SQLDA.Fill(SQL.SQLDS, "GettingInfo")
        RowsCount = SQL.SQLDS.Tables("GettingInfo").Rows.Count
        If RowsCount < 1 Then
            MsgBox("There is no records", MsgBoxStyle.Critical, "Sorry")
            SQL.SQLDS.Reset()
            SQL.SQLCon.Close()
        Else
            ' there are records !
            DGVData.Rows.Add(RowsCount)
            For i As Integer = 0 To RowsCount - 1
                With DGVData
                    .Rows(1).Cells(0).Value = SQL.SQLDS.Tables("GettingInfo").Rows(i).Item("bookingID")
                    .Rows(1).Cells(0).Value = SQL.SQLDS.Tables("GettingInfo").Rows(i).Item("paymentConfirmation")
                    .Rows(1).Cells(0).Value = SQL.SQLDS.Tables("GettingInfo").Rows(i).Item("totalCost")
                End With
            Next
        End If
        SQL.SQLDS.Reset()
        SQL.SQLCon.Close()

    Else
        ' the connection is already open 
        SQL.SQLDA.Fill(SQL.SQLDS, "GettingInfo")
        RowsCount = SQL.SQLDS.Tables("GettingInfo").Rows.Count
        If RowsCount < 1 Then
            MsgBox("There is no records", MsgBoxStyle.Critical, "Sorry")
            SQL.SQLDS.Reset()
            SQL.SQLCon.Close()
        Else
            ' there are records !
            DGVData.Rows.Add(RowsCount)
            For i As Integer = 0 To RowsCount - 1
                With DGVData
                    .Rows(1).Cells(0).Value = SQL.SQLDS.Tables("GettingInfo").Rows(i).Item("bookingID")
                    .Rows(1).Cells(0).Value = SQL.SQLDS.Tables("GettingInfo").Rows(i).Item("paymentConfirmation")
                    .Rows(1).Cells(0).Value = SQL.SQLDS.Tables("GettingInfo").Rows(i).Item("totalCost")
                End With
            Next
        End If
        SQL.SQLDS.Reset()
        SQL.SQLCon.Close()
    End If
End Sub

Code for SQLControl, which might have something to do with the problem:

    Imports System.Data.SqlClient
    Public Class SQLControl
         Public SQLCon As New SqlConnection With {.ConnectionString = "Data   Source=JENNIFER\DDAP2015;Initial Catalog=zachtravelagency;Integrated Security=True;"}
         Private SQLcmd As SqlCommand
         Public SQLDA As SqlDataAdapter
         Public SQLDS As DataSet

If you can see the problem please state the code needed changing. Thank you.

Brooksie
  • 27
  • 7
  • 1
    I haven't used the `SqlConnection` class and it's related classes, but it looks to me like you are using the `SQLDS` variable before it's initialized in the line you mentioned is throwing the error. If you set a breakpoint on that line and inspect the variables, I'm betting that variable will be `Nothing`. Now that I think about it, you don't seem to have `SQLDA` initialized either. – RianBattle May 05 '15 at 12:53
  • Nearly all NullReference Exceptions have the same set of causes. See [NullReference Exception in Visual Basic](http://stackoverflow.com/a/26761773/1070452) for help on this. You did not give us much help such as *where* the error is happening making this a *debug my code for me* question. As showb, there is nowhere you are initializing `SQLDS`. NRE's are usually easy to find with the debugger once you understand what there are. – Ňɏssa Pøngjǣrdenlarp May 05 '15 at 12:55

2 Answers2

0

Either SQL, SQL.SQLDA, or SQL.SQLDS is null. You have not included in your code where you initialize any of these, but your problem is one or more of these items was never initialized. Using the debugger, you will be able to determine which of these is not defined.

Since you make it past where you open the connection, object SQL is defined, leaving SQL.SQLDA or SQL.SQLDS.

Russ
  • 3,913
  • 19
  • 31
0

This is how you should be doing.

SQL.SQLDS = New DataSet
If SQL.SQLCon.State = ConnectionState.Closed Then
    SQL.SQLCon.open()
    SQL.SQLDA = New SqlDataAdapter(loadSQL, SQL.SQLCon)
    RowsCount = SQL.SQLDS.Tables("booking").Rows.Count
    .
    .
    .
    .
Else
    ' the connection is already open 
    SQL.SQLDA = New SqlDataAdapter(loadSQL, SQL.SQLCon)
    SQL.SQLDA.Fill(SQL.SQLDS, "booking")  'your table name is "booking"

    RowsCount = SQL.SQLDS.Tables("booking").Rows.Count   
    .
    .
    .
    .
End If

Find more here

Small Advice : Please refrain from keep creating new questions with the same piece of code, over and over again. You are creating more and more questions with this same piece of code, again and again, when someone points out the mistake in your code.

  1. Filling DataGridView from DataSet issue
  2. rows count is not a member of integer
  3. NullReferenceException was unhandled
Community
  • 1
  • 1
Saagar Elias Jacky
  • 2,651
  • 2
  • 12
  • 27