get datagrid.datasource gives error

V

vips

Page_Load

datagrid1.datasource=dataset1

//I am filling the datagrid and it works fine when page is displayed

end

---------------

button 1_click

dim ds as dataset

ds=datagrid1.datasource

//gives error

end



when I try to get the datasource from a datagrid (i.e. a dataset) it gives
error

"Object reference not set to an instance of an object."

is it due to postback event the datagrid's datasource is not accessible ??



vips
 
G

Grant Merwitz

try casting

Not sure of the the VB code but in C#

DataSet ds = new DataSet();
ds = (dataset)datagrid1.datasource;
 
K

Karl Seguin

That's right. The datasource isn't maintained in viewstate as a raw
dataset..instead only the information necessary to the datagrid (rows,
columns, ...) are maintained. If you need the dataset again you'll need
to either recreate it or store it somewhere...

KArl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
 
V

vips

I am getting datagrid.datasource as nothing !!



Grant Merwitz said:
try casting

Not sure of the the VB code but in C#

DataSet ds = new DataSet();
ds = (dataset)datagrid1.datasource;
 
V

vips

u mean I need to keep the dataset it in viewstate ??
but the framework itself keeps it in viewstate ...cant i access it some how
??
 
K

Karl Seguin

I mean, you can keep it wherever you want...viewstate, session,
cache..whatever appropriate for your situation (don't know enough to tell
you).

The framework doesn't keep the dataset in the viewstate. It keeps the
controls which make up the datagrid (which were build using the dataset) but
not the dataset itself...

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
 
Joined
May 11, 2006
Messages
1
Reaction score
0
Hi You can get the dataSource from view state

Here is the code to do it

if(!Page.IsPostBack)
{
if(this.ViewState["DataSource"] == null)
this.ViewState["DataSource"] = dtTable;
}

And while retireving the datasource use following code

DataTable dtTable = new DataTable();
if(this.ViewState["DataSource"] != null)
dtTable = ((DataTable)this.ViewState["DataSource"]);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top