Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 Bad practice maybe...

Author  Topic 

jhermiz

3564 Posts

Posted - 2004-10-06 : 15:08:18
Not being a web developer and just getting into it has been quite a change. One thing I am having problems with is retaining information. I want to ask the question is it bad to do something like this:


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here

strLoginUser = Session("Login")
intClientID = Session("ClientID")
If Not IsPostBack Then
'bind the grid
'the first time IsPostBack=False (on the initial load)
'because of this we default to open issues
Call SetDefaults()
End If
End Sub


Meaning say strLoginUser was a string of the class and intClientID
was an integer of the class. This page load event fires every time
this page loads, meaning a user may leave this page (go to another
tab) and come back at a later time. When they come back to this tab
(page) this event fires again and I store the user name and client
identifier via the session variables. Is this a bad idea ? I have
noticed at times that the variables are empty after the session
timeout of twenty minutes hits...so then my page fails with this
huge error message at the top:


An Error Occurred: System.Data.SqlClient.SqlException:
Procedure 'select_action_items_by_type' expects
parameter '@ResponsiblePerson', which was not supplied. at
System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior
cmdBehavior, RunBehavior runBehavior, Boolean returnStream) at
System.Data.SqlClient.SqlCommand.ExecuteReader() at ims.jakah.com._default.BindActionItemsGrid(Int32 intType, Int32
intSortField, String strPerson) in \\Jakah-iis-2
\ims\default.aspx.vb:line 223


And I know why it errors out because it calls this function 'BindActionItemsGrid' which passes the user name as a
stored procedure parameter. The parameter ends up not getting a value so Im wondering how to handle this. Is this normal ? Because
i have used apps in the past that take me back to the login page if
my session has timed out. But how do I check for this and redirect
back to the login page if my procedure errors out ?

Or should I not be doing this this way ?

Thanks,

MichaelP
Jedi Yak

2489 Posts

Posted - 2004-10-06 : 16:31:56
I usually populate such things at time of login, and never re-assign the variables unless they change.

In the Render method of my page (the Base Class that the pages inherit from) I have something like this:


If Session.Keys.Count = 0 Then
'Something is fubar'd
'Make the user re-login
System.Web.Security.FormsAuthentication.SignOut()
Session.Clear()
Session.Abandon()

Response.Redirect("Login.aspx?ReturnURL=" & Server.UrlEncode(Request.Url.PathAndQuery))
End If


Ideally, the forms authentication and session timeouts would be the same, but they are not, so you have to do this sort of thing.

Michael

<Yoda>Use the Search page you must. Find the answer you will.</Yoda>
Go to Top of Page

MichaelP
Jedi Yak

2489 Posts

Posted - 2004-10-06 : 16:32:58
Jon,
I am available for consulting :)

Michael

<Yoda>Use the Search page you must. Find the answer you will.</Yoda>
Go to Top of Page

jhermiz

3564 Posts

Posted - 2004-10-06 : 16:41:45
hahahah we need one...where are you at ?
Go to Top of Page

MichaelP
Jedi Yak

2489 Posts

Posted - 2004-10-06 : 16:45:31
Mobile, AL

<Yoda>Use the Search page you must. Find the answer you will.</Yoda>
Go to Top of Page

jhermiz

3564 Posts

Posted - 2004-10-06 : 16:46:05
:(
Go to Top of Page

jhermiz

3564 Posts

Posted - 2004-10-06 : 16:53:09
quote:
Originally posted by MichaelP

I usually populate such things at time of login, and never re-assign the variables unless they change.

In the Render method of my page (the Base Class that the pages inherit from) I have something like this:


If Session.Keys.Count = 0 Then
'Something is fubar'd
'Make the user re-login
System.Web.Security.FormsAuthentication.SignOut()
Session.Clear()
Session.Abandon()

Response.Redirect("Login.aspx?ReturnURL=" & Server.UrlEncode(Request.Url.PathAndQuery))
End If


Ideally, the forms authentication and session timeouts would be the same, but they are not, so you have to do this sort of thing.

Michael

<Yoda>Use the Search page you must. Find the answer you will.</Yoda>



Right that is what I do, I set the session data at login Michael,
The code I posted grabs that session data.

The problem is the session loses some of this data so I want to go back to the login page.

Where should I check for that? Directly before that stored procedure passes parameters ?
Go to Top of Page

MichaelP
Jedi Yak

2489 Posts

Posted - 2004-10-06 : 17:17:41
I'd do it in the same place as above. If the count is NOT zero, check to make sure you have everything you need before you start to render the page. if you don't want to check for everything, then you need to do the validation at the function call where you pass in the UserID etc.

Michael

<Yoda>Use the Search page you must. Find the answer you will.</Yoda>
Go to Top of Page

jhermiz

3564 Posts

Posted - 2004-10-06 : 18:16:21
Hi Michael

So this:

If Session.Keys.Count = 0 Then
'Something is fubar'd
'Make the user re-login
System.Web.Security.FormsAuthentication.SignOut()
Session.Clear()
Session.Abandon()

Response.Redirect("Login.aspx?ReturnURL=" & Server.UrlEncode(Request.Url.PathAndQuery))
End If

Goes in my default.aspx page the render event ?

THanks
Go to Top of Page

MichaelP
Jedi Yak

2489 Posts

Posted - 2004-10-06 : 19:31:33
Ideally, all pages should inherit from a base class, and the posted code should go into the Render() method of the Base class.


Public Class BasePage : Inherits System.Web.UI.Page


Public Class _Default : Inherits BasePage

<Yoda>Use the Search page you must. Find the answer you will.</Yoda>
Go to Top of Page

jhermiz

3564 Posts

Posted - 2004-10-06 : 20:56:27
Hmm
so whats my BasePage here ?

Go to Top of Page
   

- Advertisement -