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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DataTable Problem

Status
Not open for further replies.

Erics44

Programmer
Oct 7, 2004
133
GB
Hi
I have some code that works for one datatable and datagrid on one page and doesnt for another.

All I want to do is set a DataTable as the datasource of a datagrid and like I said it works in one instance but not another. Example of the code

Dim dt As DataTable
dt = DataGrid1.DataSource
Dim dr As DataRow
For Each dr In dt.Rows
lblTest.Text = dr(0).ToString
Next

Anyone have any ideas what I could be missing
Thanks
 
Not understanding what you are doing. Do you want to take the datasorce of a datagrid and assign it to another?
Jim
 
The datasource of a DataGrid and assign it to a DataTable
 
Ok, so is there an error you are getting in the code you are showing? You say it works in one but not the other. Is there an error here, or no rows??
 
in the above example it looks at the line

For Each dr In dt.Rows

and gives the error

Object reference not set to an instance of an object
 
Does datagrid1's datasource exist?
Try :

For each dr in datagrid1.datasource
Next

 
Hi
Sorry, I get the same error, datagrid1 is definately populated with data though
Thanks
 
what is the datasource of the grid. Is it a dataset or a datatable in code?
 
it was taken from a dataset

The page where the same code works fine the datasource of the datagrid was also taken from a dataset
 
like jim said, try this

try

catch

response.write(dt.Rows.count)
end try

Known is handfull, Unknown is worldfull
 
I don't know how that would work at all. I receive errors if I use your code with a datagrid who's datasource is a dataset:
Try this:
Code:
Dim dt As DataTable = YourDataSet.Tables(0)
Dim dr As DataRow

For Each dr In dt.Rows
   lblTest.Text = dr(0).ToString
Next
I suspect the datasource of the other grid is already a datatable, not a dataset.

Jim
 
and one more thing, what exactly are you trying to do? maybe you are looking for the DataGrid's ItemDataBound event???

Known is handfull, Unknown is worldfull
 
Hi
The datasource of the other datagrid definately comes from a dataset and the exact same code definately works. In fact it works in more than one other instance.

To answer your question VBKris, I use the datatable in a class module which then exports the table to an excel spreadsheet.

Thanks a lot for your time
 
can i suggest a workaround? but before that need to know whether pagination is there in the grid? and if it is there how exactly should the data come in the excel file???

Known is handfull, Unknown is worldfull
 
Can you set the DataTable = an actual DataTable and not the source of a datagrid? If so, do you still have the problem that way?
 
VBKris - I dont know what Pagination means do you just mean the allow paging property? If so I have tried it both on and off and I get the same result. The Table is exported to excel using a file stream and a stream writer and dont really want to change this part because it works for so many other instances.

Ralph - Do you mean just build a new datatable? I have done that and used it in my export to excel class and it works. I still have the problem of how I am going to assign the datasource of my datagrid to a datatable. I think it is a problem with the datagrid because I cant look through the rows in the datagrid datasource.

For Each dr In DataGrid1.DataSource - errors

Thanks for you help
 
Hi All
I have rebuilt the SQL and assigned the information to a dataset and then the dataset to a datatable (so it is in line with the class) and then exported it and it works. I long winded way and I am still baffled why in this instance I cant assign the datasource of a datagrid to a datatable and in plenty of other instances I can.

Thanks for your time
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top