0

I'm working on a Windows Form project and am having issues with one of the DataGridView objects in one of the forms. This is what I have so far:

Private Sub CustomerUsageForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Me.Tbl_usageTableAdapter1.Fill(Me.ClarityWaterDBMAINDataSet1.tbl_usage)

    Dim con As New OleDb.OleDbConnection
    Dim dbString As String
    Dim ds As New DataSet
    Dim da As OleDb.OleDbDataAdapter
    Dim sql As String

    dbString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\ClarityWaterDBMAIN.accdb"

    con.ConnectionString = dbString
    con.Open()

    sql = "SELECT * FROM tbl_usage"
    da = New OleDb.OleDbDataAdapter(sql, con)
    da.Fill(ds, "tbl_usage")
    con.Close()

Then when the form is activated this code is run:

    Private Sub CustomerUsageForm_Activated(sender As Object, e As EventArgs) Handles MyBase.Activated
    Dim GridInitializedBoolean As Boolean = False

    'Initialize the grid's binding
    If Not GridInitializedBoolean Then
        'bind and format the grid
        DataGridView1.DataSource = BindingSource1

        SetUpGridColumns()

        GridInitializedBoolean = True
    End If
End Sub

Private Sub SetUpGridColumns()
    'sets up the columns for the data grid view
    Try
        With DataGridView1
            .Columns!usage_id.Visible = False
            .Columns!reading_date.HeaderText = "Reading Date"
            .Columns!reading_cf.HeaderText = "Amount Used"
            .Columns!account_id.HeaderText = "Account ID"
        End With
    Catch ex As Exception
        MessageBox.Show("Error Setting up the grid." & ex.Message)
    End Try
End Sub

When I load the form there is a NullReferenceException thrown from the SetUpGridColumns() method. I'm not sure why this is happening, I have this form set up exactly like another form that works perfectly:

 Private Sub InventoryForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    'TODO: This line of code loads data into the 'ClarityWaterDBMAINDataSet.tbl_customers' table. You can move, or remove it, as needed.
    Me.Tbl_customersTableAdapter.Fill(Me.ClarityWaterDBMAINDataSet.tbl_customers)

    Dim con As New OleDb.OleDbConnection
    Dim dbString As String
    Dim ds As New DataSet
    Dim da As OleDb.OleDbDataAdapter
    Dim sql As String

    dbString = "Provider=Microsoft.ACE.OLEDB.12.0;DataSource=|DataDirectory|\ClarityWaterDBMAIN.accdb"

    con.ConnectionString = dbString
    con.Open()

    sql = "SELECT * FROM tbl_customers"
    da = New OleDb.OleDbDataAdapter(sql, con)
    da.Fill(ds, "tbl_customers")

    con.Close()
    AccountNumberTextBox.Focus()


    CustomerUsageForm.CustomerUsageForm_Load(sender, e)
    CustomerUsageForm.Hide()
End Sub

Then this is the event on the button click event

Private Sub SearchButton_Click(sender As Object, e As EventArgs) Handles SearchButton.Click
    'retrieve the customer search information
    Dim GridInitializedBoolean As Boolean = False

    'initialize the grids binding
    If Not GridInitializedBoolean Then
        'bind and format the grid
        CompanyIDLookupDataGridView.DataSource = CompanyLookupBindingSource

        SetUpGridColumns()

        GridInitializedBoolean = True
    End If

    Try
        CompanyLookupBindingSource.Filter = BuildSearchString()
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Sub

I sorry for such a long post. I'm honestly not sure what else to try. Can anyone help explain why I'm having these issues?

EDIT: What I mean by set up exactly the same way is that both forms have the same types of objects, the Load events are coded the same, and the events that initialize the grid are the same.

If I comment out the first .Columns! line from the SetUpGridColumns() method, the error is thrown on the next line.

tGraham12
  • 1
  • 2
  • If the NRE is happening in the `SetUpGridColumns`, make sure the column names are correct. I don't recognize the use of `!` in `.Columns!usage_id.Visible = False`, but I haven't done WinForms in ages. – Tim Nov 12 '14 at 20:40
  • 1
    possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Tim Nov 12 '14 at 21:04
  • move the form load code to form shown and see if you get different results; but I would get rid of the bang (!) operators. you should know that `GridInitializedBoolean` doesnt do anything - as a local var it always will be false – Ňɏssa Pøngjǣrdenlarp Nov 12 '14 at 22:36
  • That's what I thought about the GridInitializedBoolean, I mostly included it as it was in the sample code that I based this on. – tGraham12 Nov 12 '14 at 22:43
  • Working backwards, if the DGV is `Nothing` in the Columns setup -- check using a Breakpoint (F9) and hold the mouse over it -- then most likely the constructor (`Sub New`) did not complete, and you are dealing with a "silent exception" (see the What is... VB answer). Another trick would be to change the config to AnyCPU and run it - it may well crash elsewhere telling you the problem. Or post the form's `Sub New` – Ňɏssa Pøngjǣrdenlarp Nov 12 '14 at 22:47
  • If **thats** not the problem, it may be the form activate code. There is no reason to defer that to later if the form has a DS when it loads. Move the logic (2 lines of code) to form_load. Its rather goofy to rely on an event to fire to perform a *required* setup step like add columns to the DGV – Ňɏssa Pøngjǣrdenlarp Nov 12 '14 at 22:59

1 Answers1

0

From what I can see you seem to have an extra character in your with statement for your columns... Also you said...

"I'm not sure why this is happening, I have this form set up exactly like another form that works perfectly"

If this is the case, where is this code at, I would be curious to see the code for this form.

   'You have a ! that shouldn't be referenced for your columns...
   With DataGridView1
        .Columns!usage_id.Visible = False
        .Columns!reading_date.HeaderText = "Reading Date"
        .Columns!reading_cf.HeaderText = "Amount Used"
        .Columns!account_id.HeaderText = "Account ID"
   End With

This is how it should look...

    With DataGridView1
        .Columns(usage_id).Visible = False
        'And so on for the rest of your columns...
    End With
Trevor
  • 7,777
  • 6
  • 31
  • 50
  • I'll take a look at changing the ! for parenthesis, though the SetUpGridColumns() method works exactly the same in the other form that is not throwing the error – tGraham12 Nov 12 '14 at 21:44
  • The form that is working is included in the original post, the InventoryForm_Load() and Searchbutton_Click() events are both in the same form. – tGraham12 Nov 12 '14 at 22:15
  • The exception is a NullReferenceException that is thrown when the SetupGridColumns() method (The one you included in this answer) is called – tGraham12 Nov 12 '14 at 22:49