problem with shared sub

B

Ben

Hi,

i don't use the heavy login control of asp.net but only a simple login
/password in order to identify the users.
In directory App_Code, i put a file (check.vb) with this content:

Public Class check
Public Shared Sub beh()
Dim ok As HttpCookie
ok = HttpContext.Current.Request.Cookies("ok")
If Not ok.Value = "y" Then
If ok.Value = Nothing Then
Dim jv As String
jv = "<script language='javascript'>" _
& " alert(First log in please');" _
& " window.location.href='log.aspx';" _
& "</script>"
HttpContext.Current.Response.Write(jv)
End If
End Sub
End Class

In every page of the application, i put this:
-----------------------------------------

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load
check.beh()
....
End class

The log.aspx file contains this:
-----------------------------

Protected Sub Submit1_ServerClick(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Submit1.ServerClick
Dim beh, pw As String
Dim ok As New HttpCookie("ok")

beh = Text1.Value
pw = Password1.Value
If pw = "a" And beh = "a" Then
ok.Value = "y"
Response.Cookies.Add(ok)
Response.Redirect("menu.aspx", False)
Else
Page.ClientScript.RegisterClientScriptBlock(Me.GetType(),
"myscript", _
" alert(wrong login /pw);", True)
End If
End Sub

No error appears when logging, but if i try to start e.g. file menu.aspx by
typing it in the browser (http://myserver/myapp/menu.aspx), i get the error:
"Object reference not set to an instance of an object" at line: check.vb

Remark: if i put the entire code from check.vb in file menu.aspx, i get the
javascript alert and i'm redirected to log.aspx, with other words, it works.

So, why doesn't the code work when put into a shared sub but works when put
directly into the file?
Thanks for help.
Ben
 
C

Coskun Sunali [MVP]

Hi Ben,

You must check if the following line really returns a value. This is all
that comes to my mind by reading your code.

ok = HttpContext.Current.Request.Cookies("ok")

in this case, "ok" can be "nothing", I suppose.
 
B

Ben

Hi, thanks for replying.
First, there is an error in my code: the line If ok.Value = Nothing Then
must be deleted.
If an user logs, the cookie sent has value "Y". If an user doesn't log but
tries to access any page directly, there is no cookie sent, so no value for
that cookie, and indeed, the line ok =
HttpContext.Current.Request.Cookies("ok") doesn't return any value.
How to prevent that error in case the cookie returns no value? I also tried
with: If ok.value = Nothing then ... but same error.
Thanks
 
C

Coskun Sunali [MVP]

Hi Ben,

I suppose you can try the following:

--- if ok = Nothing Then --- so I just removed the ".Value"

I say I suppose because I am not a VB.NET syntax developer. I am just trying
to show the idea behind the error.

--
Coskun Sunali
Microsoft MVP - ASP.NET
http://sunali.com
http://propeople.dk
 
B

Ben

With this, i get following error:
BC30452: This operator = is not defined for types System.Web.HttpCookie and
System.Web.HttpCookie.
 
P

Patrice

Hello,

In your page you are declaring ok as *new* httpcookie. So if the cookie
doesn't exist you'll still have an object. In your shared sub you get the
cookie but doesn't make anyhting special in case it would be missing. This
message always mean that you are working with an object and that this object
is "nothing" (non initialized).

As a side note this is something ASP.NET could do for you (you do have a
simple login/password pair in ASP.NET, plus even if you don't want this UI
you should be able to reuse the authentication mechanism using
http://msdn.microsoft.com/en-us/library/ka5ffkce.aspx).
 
M

Mich

Thanks,
you say:"In your shared sub you get the cookie but doesn't make anyhting
special in case it would be missing".
That's my problem: what special thing can i do? I tried "If ok.Value =
Nothing Then", and also "If ok = Nothing Then" but doesn't work. Is there
something i can do in the shared procedure or not?
If not, i will try your suggestion.

_____________________________________
Onze website: http://home.scarlet.be/heibloem
 
P

Patrice

So it could be something such as shown below (untested). you need to make
sure that you have actually a non nothing object (ok variable) before
statring to work with it. This is likely what was missing from the previous
code...

' Here if the cookie doesn't exists, the "ok" variable is
nothing
If Ok is nothing then
HttpContext.Current.Response.Write(jv)
Exit Sub
End If
If Ok.Value is nothing then
HttpContext.Current.Response.Write(jv)
Exit Sub
end if
If ok.Value<>"y" Then
HttpContext.Current.Response.Write(jv)
Exit Sub
End If
 

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

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,699
Latest member
AnneRosen

Latest Threads

Top