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'
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'