[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1355
  • Last Modified:

VB.Net: NullReferenceException was unhandled error

Experts, please help me resolve this error.

Error Message:
"NullReferenceException was unhandled error...Object reference not set to an instance of an object."

Code error occurred during runtime:
dg2.Name = Me.moduletableCB.Text
0
adamchicago
Asked:
adamchicago
  • 9
  • 8
1 Solution
 
thenelsonCommented:
Not knowing what you are trying to do try:
dg2.Name = Nz(Me.moduletableCB.Text, "")
0
 
Stephan_SchrandtCommented:
Either dg2.Name or Me.moduletableCB.Text is nothing. Set a debug point to see which one of those are nothing.
0
 
adamchicagoAuthor Commented:
Thank you very much for your help.

moduletableCB.text is populating accurately...for example "northfoods".  The variable represents both the name of a SQL table (confirmed to exist and be connected to) and a datagridview.

The real problem lies with the variable dg2...I don't know how to handle this one.  A datagridview was created programmatically earlier with the name "northfoods", however "northfoods" is not dim'd as datagridview because the datagridview was created programmatically using the variable "dg".  "dg" is dim'd as NEW datagridview which is why I'm using "dg2" for the new variable.

I think I need to set dg2.name = to moduletableCB.text but I can't figure it out.
0
Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

 
Stephan_SchrandtCommented:
Could you post you code, atm I can not really imagine what you are trying to do.
0
 
adamchicagoAuthor Commented:
This code attempts to refresh an already existing datagridview from data in the SQL table.

Both the table and the gridview share the same name, however the datagridview that is existing and open was created programmatically using the variable "dg" and therefore I cannot just apply the .datasource to the datagridview because its name resides in the variable moduletableCB.text

Thank you very much for your help!!!!!!!!!!!!
'CONNECT THE DATA MODULE TABPAGE-DATAGRIDVIEWS TO THE SQL DATA MODULE TABLES
        Dim dbadp As New SqlDataAdapter("USE " & DBName & ";SELECT * FROM " & "dbo." & moduletableCB.Text, Conn2)
        Dim dTable As New DataTable
        dbadp.Fill(dTable)
        dbadp.Dispose()
        dg2.Name = Me.moduletableCB.Text
        dg2.Location = New Point(20, 20)
        dg2.DataSource = dTable 
        'CLOSE THE SQL CONNECTION
        If Conn.State = ConnectionState.Open Then
            Conn.Close()
            If Conn2.State = ConnectionState.Open Then
                Conn2.Close()
            End If
        End If

Open in new window

0
 
Stephan_SchrandtCommented:
Where did you dim the dg2?
0
 
adamchicagoAuthor Commented:
Oh sorry about that...it is dim'd further up in the code:

"dim dg2 as datagridview"
0
 
adamchicagoAuthor Commented:
and the Conn2 SQL string connection is already opened also.
0
 
adamchicagoAuthor Commented:
So basically at this point the datagridviews are open on the screen for the user to see.  They are then selecting from a combobox one of the datagridviews to updata "moduletableCB" and then clicking the update button which runs this code.
0
 
Stephan_SchrandtCommented:
Do you instantiate it later in your code?
0
 
adamchicagoAuthor Commented:
I'm not sure what you mean by "instantiate" it but was created earlier in the code and is sitting open along with several other datagridviews...the user then uses a combobox that has the names of the datagridviews (and SQL Tables) and selects the datagridview they want to update.
0
 
Stephan_SchrandtCommented:
I think you just forgot the New keyword:

dim dg2 as NEW datagridview


This would explain your nullreference.
0
 
adamchicagoAuthor Commented:
when I add "NEW" nothing happens...I think by using new it wants to create a whole new datagridview, but I really don't know.

Any other ideas?

Thanks again for you help on this...it's appreciated.
0
 
Stephan_SchrandtCommented:
If you want to access properties of a class you have to instantiate it. This is done by the 'new' keyword or by setting a reference to an existing instance. This is what you missed.

If you want to use an existing datagridview with the new variable dg2, then you have to set the reference:

dg2 = existingdatagridview

This is done with the object, not with the name. If you want to populate a new one you have to declare it as new and then add it to form:

Dim dg2 as new datagridview
me.controls.add(dg2)

Your code line 'Dim dg2 as Datagridview' and your comment assume that you want to set the reference to an existing one. Please note that the existing datagridview has to be on the form OR has to be a form/public variable to access it.
0
 
Stephan_SchrandtCommented:
You can also do it with name, but this way:

dg2 = CType(Me.Controls.Find(Me.moduletableCB.Text , True), DataGridView)
0
 
adamchicagoAuthor Commented:
Stephan, thank you so much!  I think you're the only so far who understands what is going on with this app.

I have to follow your second suggestion ("you can also do it with name...") because when I've tried the other method it ends up adding a new datagridview onto the form rather than connecting the existing datagridview.

Regarding the second method I get a blue line warning under the code you posted that says:

"Value of type 1-dimensional array of System.Windows.Forms.Control cannot be converted to system.windows.forms.DataGridView"

This is what I've been running into trying to dim the moduletableCB.text as a datagridview...ugh!

Any ideas?


Thanks again for your help!!!
0
 
Stephan_SchrandtCommented:
Sorry, did forget that the find method returns an array. Use this:

dg2 = CType(Me.Controls.Find(Me.moduletableCB.Text , True)(0), DataGridView)

This will return the 1. control named like the text in moduletableCB.Text.
0
 
adamchicagoAuthor Commented:
Thank you very for your help...I can finally move onto the next VB challenge...your patience and help is greatly appreciated!!!!!!!!!!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 9
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now