0

I know there have been articles on here under this title but I cannot relate them to my error. I am trying to cobble together a mock query page as part of an assignment for Uni and stumbled upon this script that I have modded to suit. There are two forms, one that runs a set query to a datagrid - this works but on another page script below there is this NullReferenceException. The code is exactly the same as the working form so what's wrong ?

Public Class Adding_Information

'Dim SQL As New SQLControl
Private SQL As New SQLControl
Public Property DGV_SetQueries As Object



Private Sub btnCustomerFind_Click(sender As Object, e As EventArgs) Handles btnCustomerFind.Click
    If txtCustomerName.Text <> "" Then

        If SQL.HasConnection = True Then
            Dim dgv_QueryData As Object = Nothing
            dgv_QueryData.DataSource = SQL.SQLDataset.Tables(0)
            'Create a query variable
            Dim query As String = "Select
                              Customer.Title,
                              Customer.Firstname,
                              Customer.Surname,
                              Customer.Email,
                              Customer.MobileNo,
                              Customer.Postcode,
                              Customer.Regno
                            From
                              Customer
                            Where
                              Customer.Surname = 'Fletcher'
                            Order By
                              Customer.Firstname"

            SQL.RunQuery(query)

            If SQL.SQLDataset.Tables.Count > 0 Then

                'ensure that there is only one table
                DataGridView1.DataSource = SQL.SQLDataset.Tables(0)

            End If

        End If

    End If

End Sub

Private Sub DGV_SetQueries_CellContentClick(sender As Object, e As DataGridViewCellEventArgs)

End Sub

Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick

End Sub
End Class

The DataGridView is called datagridview1 and hasconnection is a connection check to the SQL server. Has anyone spotted anything?

Tyler Benzing
  • 1,116
  • 11
  • 25
  • On which line do you get the error? There are too many `As Object` definitions for my taste - surely they are some distinct type, but my guess is that in `SQL.SQLDataset.Tables(0)` either `SQLDataset` or `Tables(0)` is nothing since there is no code there showing anything happening to the `SQLControl` object (whatever *that* is) before pieces of it are referenced. – Ňɏssa Pøngjǣrdenlarp Apr 15 '16 at 19:14
  • If SQL.HasConnection = True Then
    Dim dgv_QueryData As Object = Nothing
    >>>> dgv_QueryData.DataSource = SQL.SQLDataset.Tables(0)
    'Create a query variable
    Dim query As String = "Select
    The sqlcontrol is a class form that has the functions has connection and
    – Andrew Bowles Apr 15 '16 at 19:20
  • ...like I said there is no code there to do anything with/to `SQL` before it is first referenced, so one or both of `SQLDataset` and `Tables(0)` is Nothing – Ňɏssa Pøngjǣrdenlarp Apr 15 '16 at 19:30
  • Ok so I am thick here , but the same exact code with a different SQL query WORKS on another form within this very solution ! so what would you add to this to get it to work ?? – Andrew Bowles Apr 15 '16 at 19:50
  • I cant tell you why code I cant see does work, and I have no idea what `SQLControl` is or what it does (looks like a SQL "helper"0. I susoect if you remove the line `dgv_QueryData.DataSource = SQL.SQLDataset.Tables(0)` at the top all will be well. After the query is executed there is code to check of there is a table etc and assign the DS – Ňɏssa Pøngjǣrdenlarp Apr 15 '16 at 20:05

1 Answers1

2

Dunno if this should be an answer or a comment but... the error is painfully obvious.

    If SQL.HasConnection = True Then
        Dim dgv_QueryData As Object = Nothing
        dgv_QueryData.DataSource = SQL.SQLDataset.Tables(0)

You're initializing 'dgv_QueryData' to 'Nothing' and then immediatly using it, trying to access its 'DataSource' property... you must initialize it to something, either another variable or a 'New '...

Josh Part
  • 2,154
  • 12
  • 15