Last post Mar 28, 2018 08:56 PM by AaronR___
Mar 27, 2018 10:25 PM|AaronR___|LINK
I want to populate a datatable in one subroutine, but use that data in another subroutine within the same class. I'm wanting to avoid creating duplicate code in multiple sub-routines that fill the table with the same data.
My problem, is that the data only seems to be available within the function where the subroutine was actually filled. When I try to refer to the data in another sub, it tells me there's no data in the table. So I've had to copy/paste the code that fills
the table from one function to another. It works, but I hope there's a cleaner way to do it.
Mar 28, 2018 09:28 AM|PatriceSc|LINK
Hard to tell without seeing some code. Are you 100% sure you called the sub that populate the DataTable before consuming it ? It could be also that you mistakenly populate a local variable or a misunterstanding about how the web works (trying to populate
the DataTable on a request and trying to consume it during another http request).
I would likely create a function. It makes easier to write code that ensure it is populated rather than having a sub that populates a class level DataTable and then calling another one that does use this DataTable.
Mar 28, 2018 04:14 PM|AaronR___|LINK
I can describe a bit more about the form. It's an internal webform that will be hosted on our intranet. The first step is the user enters a studentid.
Sub Routine A:
The aspx.vb code then verifies that the ID is valid, and if so, it populates a datatable with student data, that will be used to auto-populate many fields on the form. If a student is enrolled in multiple schools however, there will be two different "sets"
of data that the user has to chose from. If the code detects two sets of data, it populates a dropdown list, from which the user will select the school.
Sub routine B:
After the user selects a school, the fields should be populated based on whichever school they select.
So yes, I am trying to consume the data in the table in a different request than when it was populated. But it sounds like you're saying .Net doesn't store the data in that datatable for the user's session?
Mar 28, 2018 04:45 PM|AaronR___|LINK
I made a function that populates the table. So I guess that'll work. Just trying to learn more about the .Net platform as I go. So is it the .net garbage collector that would clear out the value of the table?
Mar 28, 2018 05:30 PM|PatriceSc|LINK
Seeing your DataTable declaration and a bit of code for your two subs could help.
It seems for now it could be a confusion about the last point. The web is stateless by design that is when you ask a page to a web server :
- it checks how this request should be processed (serving a static file etc...)
- it may end up in creating a "page" (webform) or a "controller" object to process this request. The goal is to render HTML markup that is sent to the browser and this object is DESTROYED when the http request ends...
In short it will create a NEW object for each http request. So if this is what you are doing you can't get a DataTable during a first http request and reuse it during the next http request. Or do you run both subs during the SAME http request ?
Mar 28, 2018 08:56 PM|AaronR___|LINK
Yes, there are two requests involved, because first we have to establish what the options are, then we need to wait for user import to chose which option they are selecting. Having a separate function that populates the datatable avoides the issue of duplicate
code. Though in a large scale app, I imagine you'd want to avoid hitting the database twice, but this is just a rinky-dink internal app that will only be used by a couple of people.
I found this page which discusses methods to keep the value of variables "alive" across http requests. I imagine I could create a "session" datatable to accomplish this, but what I'm doing now works. Thanks!