Question Accessing DataSets throughout project

davebeginner

New member
Joined
Dec 3, 2011
Messages
2
Programming Experience
Beginner
I'm just starting to explore VB.NET datasets but I'm stuck on a (probably) trivial problem.
I have declared a dataset (let's call it 'Fred') as public in a VARs module.
In the main form in a sub routine called at the start of the project I connect to an Excel spreadsheet and read it into the 'Fred' dataset into one table (called 'Table')
I then bind the dataset table to a datagridview and I can see the spreadsheet. Which is very nice :)

Having loaded the dataset I want to be able to get some values so I created a button and used the following code:

msg = Fred.Tables("Table").Rows(0).Item("Name")

I think this should get the value in the first row and in the column called 'Name' in the table called 'Table'

But I get an error when I click the button 'Object reference not set to an instance of an object' which I think means I need to use the 'New' keyword...

So I added to the button code:

Dim Fred = New DataSet

But I get the same error.

Now I have to admit that I've never really got my head around how to use the 'New' keyword and would have hoped to be able to get data from a dataset through my entire project - especially as I've declared it as 'Public'. Surely if I use 'New' all over the place then I create new datasets - when I've already got one :-(

I would therefore appreciate some help to the specific problem of how to access a dataset throughout my whole project. I've searched around but can't find an explanation and get quite confused when I see the word 'instantiated'
 
No, it doesn't mean that you need to use the New keyword. It means that you are expecting a particular reference to contain an object and it doesn't. One possible solution may be to use the New keyword to create an object and then assign that object to the reference before using it, but that only makes sense if you want a new object. If you want to use an existing object then creating a new one is obviously not a good solution.

So, the first step is to work out what reference is null. This is your code:
msg = Fred.Tables("Table").Rows(0).Item("Name")
What references are you using there? Well, first there's 'Fred'. If it's Nothing then you can't get its Tables property, so that is the first thing to check. Assuming 'Fred' is not Nothing, the next reference is Fred.Tables("Table"). If there's no table named "Table" then that will return Nothing, so you obviously can't get the Rows property if there's no DataTable. That's the next thing to check. I'll let you determine what other references you need to test, if those are not Nothing. In general, any time you use a dot to access a property or method, you will get a NullReferenceException if the thing before the dot is Nothing.
 
...But the code works in another sub....

Thank-you for your reply.
To test whether the code returned null I copied it back into the sub routine where I initialise and load the dataset.
In this case I get an answer from the dataset without error.
It is only when I take this line of code into another sub routine that it causes the error.
 
I direct you to my first post.
So, the first step is to work out what reference is null.
Have you tested each reference in the line where the error occurs? If not then you have done nothing towards that first step. How code behaves elsewhere in the app is irrelevant to that.
 

Latest posts

Back
Top