Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/ASP_DOT_NET/Q_21169996.html

Posted on 2004-10-16
30
Medium Priority
?
352 Views
Last Modified: 2011-10-03
why, is it on form load, webform1 throws any exception of "Object reference not set to an instance of an object."

this line is highlighted "If Session("AuthenticateUser").ToString() = "webform3" Then"


and this is the actual code=--------

If Session("AuthenticateUser").ToString() = "webform1" Then
            Button1.Visible = True
        Else
            If Session("AuthenticateUser").ToString() = "webform3" Then
                Button1.Visible = True

            End If
            Button1.Visible = False
        End If
0
Comment
Question by:SirReadAlot
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 16
  • 13
30 Comments
 
LVL 6

Expert Comment

by:ryerras
ID: 12327340
Hmm.... Just 20 points to provide a solution for this......

Anyway,

the exception is because, the Session("AuthenticateUser") was not initialized at the time of using it in your code. Remember always to assign a value to a session variable, before you try to access it. Its like writing a check for the money you dont have in bank, you will get a stick. So before your code stub, make sure you assign a value to the session variable as follows

Session("AuthenticateUser") = "someValue";

Now Session "AuthenticateUser" is a valid session variable, until then Session has no way to figure out what the heck is this  "AuthenticateUser" is.

Good luck
0
 
LVL 2

Expert Comment

by:diablocute
ID: 12327374
I'd get that when I do to .ToString() operation to Session variable that has not been initialized.
My remedy is one of the folowing three.  
Either 1) make sure that Session("AuthenticateUser") has been initialized beforehand.
or 2) Check wether Session("AuthenticateUSer") is not null first before calling .ToString() to it.
or 3) use Convert.ToString  instead of ToString()  
0
 

Author Comment

by:SirReadAlot
ID: 12327379
Ok, initialize it  based on the code above
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:SirReadAlot
ID: 12327413
its going pear shaped,

could u please initialize it, based on this--------

If Session("AuthenticateUser").ToString() = "AdminAdd" Then
            Button1.Visible = True
        Else
            If Session("AuthenticateUser").ToString() = "webform3" Then
                Button1.Visible = True
            End If
            Button1.Visible = False
        End If
0
 
LVL 6

Expert Comment

by:ryerras
ID: 12327421
Ok, to avoid exception, you can do this
put another IF  --- ENDIF, which encloses youe IF blocks and check to see if Session("AuthenticateUSer") is null, then the control wouldnt go into that code block and you wouldnt get exception. But as I said, a Session variable should be intialized before it can be accessed

Good luck
0
 

Author Comment

by:SirReadAlot
ID: 12327468
I would like to intialize it, as this is the best way. i have but the button remains invisible from every angle.
Could u pls initialize by example based on the ffg code


If Session("AuthenticateUser").ToString() = "Webform1" Then
            Button1.Visible = True
        Else
            If Session("AuthenticateUser").ToString() = "webform3" Then
                Button1.Visible = True
            End If
            Button1.Visible = False
        End If
0
 
LVL 6

Expert Comment

by:ryerras
ID: 12327493
Ok, I saw your other question, which actually is the origin for this question. Let me put it in the way I understood

User is coming from WebForm3, ok go to WebForm3.aspx.cs class, in that find where you are actually doing the Reponse.Redirect to WebForm2 (Assuming WebForm2 is your current form, where you are writing this code), before Response.Redirect, put
 Session("AuthenticateUser") = "WebForm3"

I am not sure what you want the "AdminAdd" to signify.  If you mean, you want to check if the user came from some Admin page, go to that page and find the place where it is doing Response.Redirect, and like before place  Session("AuthenticateUser") = "AdminAdd" before the Reponse.Redirect code.

Let me know if you have anymore questions
0
 

Author Comment

by:SirReadAlot
ID: 12327587
its start from the top.

vb.net
1)the destination is addmin.aspx
2)if your coming from webform3.aspx the button is visible (which is good)
3)if your coming from webform3.aspx the button is invisible (which is good)
-----------------------------------------------------------------------------------------------
4)But if i close down my application and build and browse addmin.aspx it throws an exception "Object reference not set to an instance of an object."
5)But after completing steps 1-3,  and build and browse addmin.aspx, the button is invisble.
 
6) i want to prevent step 4 from happening
0
 

Author Comment

by:SirReadAlot
ID: 12327720
EDITED----------------------------------@@@@@@@@@@@@@@@@@@@---------------------------------------------
its start from the top.

vb.net
1)the destination is addmin.aspx
2)if your coming from webform3.aspx the button is visible (which is good)
3)if your coming from webform4.aspx the button is invisible (which is good)
-----------------------------------------------------------------------------------------------
4)But if i close down my application and build and browse addmin.aspx it throws an exception "Object reference not set to an instance of an object."
5)But after completing steps 1-3,  and build and browse addmin.aspx, the button is invisble.
 
6) i want to prevent step 4 from happening
0
 
LVL 6

Expert Comment

by:ryerras
ID: 12327735
Hmm. Dude you are little confusing. As I said, Session("AuthenticateUser") should be set in the webform, from where you are going to the destination. If your destination is Addmin.aspx, and If I want to enable or disable the Button1 based on from where I came, then the
Session("AuthenticateUser") = "AdminAdd" should be set in WebForm1 before you redirect to Addmin.aspx.

If you directly go to Addmin.aspx (not from WebForm1, where you set the Session("AuthenticateUser") ), then you get the previous exception, since Session("AuthenticateUser") is not set before you came to this Addmin.aspx page, to avoid the exception you are getting, check to see if

Session("AuthenticateUser")  is null as follows in the Page_load or wherevr of Addmin.aspx page,

 If Not Nothing(Session("AuthenticateUser") Then
  If Session("AuthenticateUser").ToString() = "AdminAdd" Then
            Button1.Visible = True
        Else
            If Session("AuthenticateUser").ToString() = "webform3" Then
                Button1.Visible = True
            End If
            Button1.Visible = False
        End If

End If

This way, it wouldnt throw error. But the Session("AuthenticateUser") should be set in the origin page as I said before
0
 

Author Comment

by:SirReadAlot
ID: 12327927
the session state has been set since.   and the code also throws an exception.

If Not Nothing(Session("AuthenticateUser") Then           and

If Not Nothing(Session("AuthenticateUser")) Then

0
 
LVL 6

Expert Comment

by:ryerras
ID: 12328031
Try with this syntax

If Session("AuthenticateUser") Is Nothing Then
  If Session("AuthenticateUser").ToString() = "AdminAdd" Then
            Button1.Visible = True
        Else
            If Session("AuthenticateUser").ToString() = "webform3" Then
                Button1.Visible = True
            End If
            Button1.Visible = False
        End If

End If


Just replace --If Not Nothing(Session("AuthenticateUser") Then---  with   --- If Session("AuthenticateUser") Is Nothing Then--
0
 

Author Comment

by:SirReadAlot
ID: 12328076
pls view my codes are they correct??

This is what i have in addmin.aspx

If Session("AuthenticateUser") Is Nothing Then
            If Session("AuthenticateUser").ToString() = "AdminAdd" Then
                Button1.Visible = False
            Else
                If Session("AuthenticateUser").ToString() = "webform3" Then
                    Button1.Visible = True
                End If
                Button1.Visible = False
            End If
        End If
    End Sub
----------------------------------------------------------------
This is what i have in webform3
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Session("AuthenticateUser") = "webform3"
        Response.Redirect("Adminadd.aspx?ID=" & ID)
          End Sub
----------------------------------------------------------------------------------------------------------------
This what i have in webform4
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Session("AuthenticateUser") = "Webform4"
        Response.Redirect("Adminadd.aspx?ID=" & ID)
    End Sub
-------------------------------------------------------------------------------------------------------------------------------

0
 
LVL 6

Expert Comment

by:ryerras
ID: 12328193
Yeah partly correct . But what is the use of Session("AuthenticateUser").ToString() = "AdminAdd" , when you are not setting the
Session("AuthenticateUser") to "AdminAdd" anywhere in Webform3 or WebForm4
0
 

Author Comment

by:SirReadAlot
ID: 12328256
so direct me to the right soln by code example.

in webform3 and 4 how do i use, Session("AuthenticateUser").ToString() = "AdminAdd



0
 

Author Comment

by:SirReadAlot
ID: 12328275
just Session("AuthenticateUser") = "adminadd" to webform3 & 4
0
 
LVL 6

Expert Comment

by:ryerras
ID: 12328324
You dont need to, if you are adding Session("AuthenticateUser") = "Webform4" this to "WebForm4" and

Session("AuthenticateUser") = "webform3" to WebForm3. From your previous comments, I see that you want to disable the button if the user comes from WebForm4 and enable it if he comes from WebForm3. If thats the only thing you want to achieve with this Sessions, what you allready have is enough.

If what I said was correct, all you need do is



If Session("AuthenticateUser") Is Nothing Then
                If Session("AuthenticateUser").ToString() = "webform3" Then
                    Button1.Visible = True
                End If
                Button1.Visible = False
            End If
        End If
    End Sub
----------------------------------------------------------------
This is what i have in webform3
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Session("AuthenticateUser") = "webform3"
        Response.Redirect("Adminadd.aspx?ID=" & ID)
          End Sub
----------------------------------------------------------------------------------------------------------------
This what i have in webform4
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
               Response.Redirect("Adminadd.aspx?ID=" & ID)
    End Sub
-------------------------------------------------------------------------------------------------------------------------------

Remember in WebForm4 you do not need to add session, since all you need is
If came from WebForm3 -->> Enable the button
else Disable it.

THen it should be enough
0
 
LVL 6

Expert Comment

by:ryerras
ID: 12328374
Ok Sorry in Addmin.aspx, Please modify it to

If Session("AuthenticateUser") Is Nothing Then
                If Session("AuthenticateUser").ToString() = "webform3" Then
                    Button1.Visible = True
                else
                Button1.Visible = False
            End If
        End If
0
 

Author Comment

by:SirReadAlot
ID: 12328455
Basically it perfers  this in webform4

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Session("AuthenticateUser") = "Webform4"
        Response.Redirect("Adminadd.aspx?ID=" & ID)
    End Sub

without it would throw an expection.

Am cool with it that way, all i need to do is disable the button when i build and browse AdminAdd.aspx. Right now it errors, does not like --If Session("AuthenticateUser").ToString() = "webform3" Then
which came from---
If Session("AuthenticateUser") Is Nothing Then
            If Session("AuthenticateUser").ToString() = "webform3" Then
                Button1.Visible = True
            Else
                Button1.Visible = False
            End If
        End If
   
0
 
LVL 6

Expert Comment

by:ryerras
ID: 12328523
Whats the exception. Anyway, if the previous code works


If Session("AuthenticateUser") Is Nothing Then
            If Session("AuthenticateUser").ToString() = "AdminAdd" Then
                Button1.Visible = False
            Else
                If Session("AuthenticateUser").ToString() = "webform3" Then
                    Button1.Visible = True
                Else
                Button1.Visible = False
                End If
            End If
        End If
    End Sub
----------------------------------------------------------------
This is what i have in webform3
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Session("AuthenticateUser") = "webform3"
        Response.Redirect("Adminadd.aspx?ID=" & ID)
          End Sub
----------------------------------------------------------------------------------------------------------------
This what i have in webform4
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Session("AuthenticateUser") = "Webform4"
        Response.Redirect("Adminadd.aspx?ID=" & ID)
    End Sub
-------------------------------------------------------------------------------------------------------------------------------


Note  that I changed   -----
          If Session("AuthenticateUser").ToString() = "webform3" Then
                    Button1.Visible = True
                End If
                Button1.Visible = False
            End If

             

To----

               If Session("AuthenticateUser").ToString() = "webform3" Then
                    Button1.Visible = True
                Else
                Button1.Visible = False
                End If



It should work for your scenario. Let me know if it doesnt. If it throws excpetion, please copy paste the exception here. Good luck
0
 

Author Comment

by:SirReadAlot
ID: 12328603
using this ==>   If Session("AuthenticateUser").ToString() = "webform3" Then
            Button1.Visible = True
        Else
            Button1.Visible = False
        End If
works when i move from form to form.

But if i close down the application and start again but building and browsing adminadd.aspx throws this expection====
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 70:
Line 71:
Line 72:         If Session("AuthenticateUser").ToString() = "webform3" Then
Line 73:             Button1.Visible = True
Line 74:         Else

I dont want to see the button when i load the page manually..
0
 
LVL 6

Expert Comment

by:ryerras
ID: 12328673
Ok. My fault.

If Session("AuthenticateUser") Is Nothing Then
   ' Basically this does nothing, if session doesnt exist
   Else
            If Session("AuthenticateUser").ToString() = "AdminAdd" Then
                Button1.Visible = False
            Else
                If Session("AuthenticateUser").ToString() = "webform3" Then
                    Button1.Visible = True
                Else
                Button1.Visible = False
                End If
            End If
End If


I forgot the Else after the first line. Now it should work.. Sorry I am a C# programmer, so wasnt that good with vb.net syntax.

GOod luck
0
 

Author Comment

by:SirReadAlot
ID: 12328727
no errors when i load manually, but the button is visible!!
0
 
LVL 6

Expert Comment

by:ryerras
ID: 12328732
Ok. I thought by default, you make the Button1.Visible = false, ok if not modify the code again to

If Session("AuthenticateUser") Is Nothing Then
   Button1.Visible = False
   Else
            If Session("AuthenticateUser").ToString() = "AdminAdd" Then
                Button1.Visible = False
            Else
                If Session("AuthenticateUser").ToString() = "webform3" Then
                    Button1.Visible = True
                Else
                Button1.Visible = False
                End If
            End If
End If
0
 

Author Comment

by:SirReadAlot
ID: 12328737
some how its receiving the webform3 value--

Session state        Session Key              Type Value
AuthenticateUser   System.String           webform3
0
 

Author Comment

by:SirReadAlot
ID: 12328740
ignore the above
0
 

Author Comment

by:SirReadAlot
ID: 12328774
this works. When i load manually the the button is invisible (good).   However is it logical that after i run webform3, and then build and browse adminadd , it still retains the value from webform3, i.e my button is still visible? has it got to do with viewstate??


Session state        Session Key              Type Value
AuthenticateUser   System.String           webform3
0
 
LVL 6

Accepted Solution

by:
ryerras earned 2000 total points
ID: 12328784
Session is related to a browser window. You have to close the browser and open again, if you recompile to clear the session. By Default asp.net keeps the session for 20 minutes in a browser window if you do not close it. So, as I am saying, Close the browser window, then open another browser after you recompile the code, then browse to Addmin.aspx, and see that the button is invisible
0
 

Author Comment

by:SirReadAlot
ID: 12328822
thats cool.

works well, thanks 4 your patience




i'll b back

SirReadAlot.


 


0
 
LVL 6

Expert Comment

by:ryerras
ID: 12328839
Also, if you want to disable to Button1 for the following case

User comes from WebForm3.aspx --> Addmin.aspx then he leaves Addmin.aspx, and some how come back to
Addmin.aspx

then you need to set Session("AuthenticateUser") ="" before any Response.Redirect statement that might cause the Addmin.aspx to load some other page.

Assuming, you only want to enable Button1, if the path is
WebForm3.aspx --> Addmin.aspx, if not disable

good luck
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question