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

Each Field in a Datareader 1

Status
Not open for further replies.

jl280328

Programmer
Jun 15, 2006
97
US
I have a datareader names SQLReader. Is it Possible to say something like this in C#:
Code:
foreach (FieldName description in SQLReader)
{
  Session[description] = SQLReader[description].ToString()
}

I am returning only a single row in the datareader and want to loop through each field and set the Session variable name to the fieldname then set the value to the value of the row returned.
 
dont know the correct C# construct, but in VB.NET:

while ObjRdr.Read
For intI = 0 To ObjRdr.FieldCount - 1
Session(ObjRdr.GetName(intI)) = ObjRdr(intI)
Next

end while



got this from the C# converter tool:

while (ObjRdr.Read) {
for (int intI = 0; intI <= ObjRdr.FieldCount - 1; intI++) {
Session(ObjRdr.GetName(intI)) = ObjRdr(intI);
}
}


Known is handfull, Unknown is worldfull
 
If you are setting the session variable name dynamically (i.e for each field that is returned from a query), how do you know what the name is to retrieve the values? Do you loop through all the session variables?


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
No I just know what they are from the names that I give them in my SQL statement. They aren't used a whole lot I guess but there are more than a couple of them so I was wondering if I should be storing these values a different way rather than Session variables, is there a more efficient way to do this keeping in mind that I need these values to survive through multiple PostBacks on the page?
 
>> is there a more efficient way to do this keeping in mind that I need these values to survive through multiple PostBacks on the page?

in this case the session method is the best bet (cross postback cross pages). if its just a single page you could use ViewState object...


Known is handfull, Unknown is worldfull
 
Does ViewState improve performance over Session? Thanks for all of your help.
 
That really depends. If you have a large page and you have viewstate enabled for all controls, it can become bloated, and possibly corrupt if you add or insert viewstate info incorrectly. I would stick with sessions, as long as you are not stored large amounts of data, for many users, you should be fine.
 
Sessions are your best bet if you need to use them across multiple pages.

However, the point I was getting at, was how you store them, not where you store them. You say that you will know the names of each session variable when you come to retrieve them, so when setting them you can explicity say what they are called. Expanding on that, they all seem to be a collection of items that are used for one purpose. So, why not create a class for these objects?

The class could contain a method for retrieving the data and a property for each item which could be set once the "retrieve" method is called. However, rather than storing each item seperately, store the whole class in a sessin variable. You can then retrieve the class from the session (whenever needed) and use each property to get the details.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top