Make datatable data available to other functions (novice)

  • Thread starter Thread starter DubSport
  • Start date Start date
D

DubSport

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
 
DubSport said:
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).
 

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

Back
Top