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.