Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cannot access the DataSet object from a DataGrid ItemCommand event

Status
Not open for further replies.

CosmoNut

Programmer
Nov 19, 2004
5
US
Hello,

I have a DataGrid bound to a DataSet, with one column being a ButtonColumn. When the user clicks on the button, the code goes to the ItemCommand event handler where I try to use the index position of the button on the grid to access data from the DataSet fields.

The code is the following:

Private Sub ReserveClicked(ByVal sender As System.Object, ByVal e As DataGridCommandEventArgs) Handles dgApptList.ItemCommand

'This is where I get the row index of the button clicked

Dim GridRow As Integer = e.Item.ItemIndex

'Then I try to assign a field value from the undelying bound DataSet (ApptGrdDS) to a string

Dim GrDS As String = ApptGrdDS.Tables("Appointment").Rows(GridRow).Item("Slot").ToString

But when I run it I get:

"Object reference not set to an instance of an object" on the Dim GrDS... line.

Can I not access the DataSet object in the ReserveClicked ItemCommand event handler procedure? Is there another way it needs to be referenced? Any ideas?

Much appreciated
 
I don't think Datasets and Datagrids work that way. I think that once you bind the grid the dataset gets closed. Look at for a great series on datagrids and that will give you the information you need to set the value.

Hope everyone is having a great day!

Thanks - Jennifer
 
this may work if your dataset was declared and populated as a pUblic variable that can be used throughout the page...
 
You are correct Jennifer, at this point in the code the DataSet is somewhere near Jupiter and it cannot be referenced.

The solution is to place hidden columns in the DataGrid which are bound to the key information needed to build the SQL statement for managing the required record(s). Then, when the button is clicked, all the necessary information is available by referencing the DataGrid Cell contents.

Thanks for the link, it gave me what I needed and now everything works.
 
Great job - glad that I could help. Checkai is correct you could have set it up in a public variable but I wouldn't recommend it.

Hope everyone is having a great day!

Thanks - Jennifer
 
Actually, I had the DataSet dimmed as Friend, so if it were possible to reference it, I should have been able to do so. Maybe Friend is not friendly enough?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top