469,951 Members | 2,744 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,951 developers. It's quick & easy.

Make datatable data available to other functions (novice)

I have created a datatable in a function, and it is populated with
data. I want to call a new function from an ASP button, and write out
some of the values in the datatable, using:
string RowValue;
RowValue = dt.Rows[0][1].ToString();
Response.Write(RowValue);

Now when I run that code from within the same function that the
datatable was populated from, it works.

When I try to run that code from a seperate function, it says that
"There is no row at position 0.".

How do I make my datatable data available to this other function?

Thanks,
....Jamie

Apr 22 '07 #1
2 3202
"DubSport" <ja**************@cmgl.cawrote in message
news:11*********************@q75g2000hsh.googlegro ups.com...
>I have created a datatable in a function, and it is populated with
data. I want to call a new function from an ASP button, and write out
some of the values in the datatable, using:
string RowValue;
RowValue = dt.Rows[0][1].ToString();
Response.Write(RowValue);

Now when I run that code from within the same function that the
datatable was populated from, it works.

When I try to run that code from a seperate function, it says that
"There is no row at position 0.".

How do I make my datatable data available to this other function?
I assume that you are storing the table in a class variable. This would
work in a Windows Form, but it doesn't work in a Web Form. In Web Forms, the
Class is always destroyed when the server sends the response back to the
client, and then it is created again when the client sends a new request to
the server (such as when you click on a button). Therefore, everything that
you placed in class variables from a first function call is lost when you
execute the click of the button -- hence the "no row at position 0", because
the datatable is empty.
If you need to preserve something between page postbacks (such as your
datatable) you need to write yourself the code that will save it the first
time around, and then recover it when you need it again. There are various
mechanisms, collectively called "state management", each one with its own
strengths and weaknesses: ViewState, Session State, Application State,
Cache, Cookies...

One way in which you can solve your problem is to use the ViewState. At
the end of the function that populates your datatable, do the following:
DataSet ds = new DataSet();
ds.Tables.Add(dt);
ViewState["MyTable"] = ds;

In the function where you need the values (such as the Click of a
button), do the following:
DataSet ds = ViewState["MyTable"] as DataSet;
dt = ds.Tables[0];

Notice that I have encapsulated your datatable inside a dataset. I do
that because the ViewState needs to be serialized, and the dataset is
serializable while the isolated datatable is not (at least in version 1 of
the framework, I believe that this was fixed in version 2 but I have not
verified it).

Apr 22 '07 #2
You nailed it man, Thanks I really appreciate it!

Apr 22 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Chris Ericoli | last post: by
2 posts views Thread by Terry Olsen | last post: by
6 posts views Thread by scottyman | last post: by
10 posts views Thread by D. Shane Fowlkes | last post: by
7 posts views Thread by Steven Bethard | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.