Declaring variables in Aspx.vb file

I have class level variable declared  as   Private dsdd as dataset

In one of the methods I am populating the dsdd.

When I try to access the same variable in another method , it is throwing  "object reference not set"
vensaliAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kamal KhaleefaInformation Security SpecialistCommented:
you need to instantiate the object


in your case to access the variable you need to declare it as shared

otherwise you need to fill it and call it from the same method


Thanks
vensaliAuthor Commented:
Is it different from declaring variables in vb.net. in vb.net, Class level variable can be accessed from any method  inside the same class.  how to get the same result in asp.net.  shared variable is the only way?
 
(infact, I declared  dsdd  as  Private Shared dsDD As DataSet  and it worked.)
Kamal KhaleefaInformation Security SpecialistCommented:
shared declaration will work
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Kamal KhaleefaInformation Security SpecialistCommented:
or you can use a session in asp.net

where you can call the session in all pages
Snarf0001Commented:
Shared declaration is very dangerous in this case.  Shared means it's the same underlying variable being accessed by ALL instances of the class.
So if you have two webrequests coming in at the same time, they're going to overwrite and read each other's data.

Can you post the code for your class?
vensaliAuthor Commented:
Private Shared dsDD As DataSet
Private objclinical As UCTrn_Clinical

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 
       objclinical = New UCTrn_Clinical


End sub

Protected Sub cmddetails_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmddetails.Click

 dsDD = objclinical.Load(txtipno.Text.Trim)

End sub


Protected Sub cmdSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdSubmit.Click
   
    saverecords()

End sub


Protected Sub saverecords()
 Dim dt1 As DataTable

  dt1 = dsDD.Tables(2)    **   If I don't declare dsdd as shared, I am getting the ""object reference not set"  error here

End Sub
Snarf0001Commented:
What's in "UCTrn_Clinical"?  I'm assuming it's a user control, but what do you have in there?
I can see why it wasn't working.  You're only loading the dataset on cmddetails_Click,
But when that's done, the page posts back, and on the next request (firing cmdSubmit_Click), it's a completely new request, new instance of the class, which means you don't have the dataset anymore.

I'm assuming you're trying to save changes done from inside UCTrn_Clinical, but need more detail.

Shared is definitely not the way to go.  The only reason it's working now, is because the server has an application level variable of a single dataset. So it's being kept across all requests by any user.
vensaliAuthor Commented:
UCTrn_Clinical is UI centric class in class library.  It just has method  which passes the data between UI and data centric class.  The method in UCTrn_clinical gets the  parameter from UI and passes to  datacentric class which executes the sp and loads the data to dataset.  this data is passed back to UI thro UCTrn_clinical.

Actually  " the page posts back, and on the next request (firing cmdSubmit_Click), it's a completely new request, new instance of the class, which means you don't have the dataset anymore."  clarified my doubt.  But then what is the best method to adopt in the above scenario.  

In Windows UI application, this was working, as I presume everything  was considered as part of single request and hence class level variable was available for all the methods in the class of windows form.
Snarf0001Commented:
For the windows app, yes, completely correct.  It's all there on your system locally, so it's not the same request / response mechanism.
Right now for saving records, the way to get it to work, would be to load the data set again inside cmdSubmit_Click.
But as I said, I'm kind of assuming the user is modifying those records on the actual page somewhere, hence the point of saving them.  In which case reloading the dataset isn't going to get you anywhere.

Its that piece of how / where the user is modifying, and why you're saving that I'm missing.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
vensaliAuthor Commented:
Actually  for a selected Ipno, I am getting a blank dataset  in Get_details method from the database and after user enters the data in user controls, and submits it,  I am populating the dataset  as below
 Dim dt1 As DataTable
  dt1 = dsDD.Tables(2)

 Dim newrow1 As DataRow = dt1.NewRow()
 newrow1.BeginEdit()
        newrow1("DS_IP_NO") = txtipno.Text.Trim
        newrow1("DS_HOSP_NO") = txthospno.Text.Trim
        newrow1("DS_REFDOC") = txtrefdoc.Text.Trim
 newrow1.EndEdit()
        dt1.Rows.InsertAt(newrow1, 0)

and calling a save method to save the data to the database

 blnresult = objclinical.Save(dt1)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.