hi all,

I have to maintain a Session variable in the Login page, which is used to keep login information for user.

It is not required for user to go for login and access all pages. I mean that if user has an account then he/she can get login otherwise it is not necessary to go for login, they still can access all pages.

I store "no" string in the session variable in the login page, if user does not get logged.

The problem is that when I want to retrieve string from this variable in other pages, and user did not get logged, then I always get error that 'Session variable is not created'. How I can solve this problem?

Please reply as soon as possible.

Thanks.

Recommended Answers

All 9 Replies

Can u provide code for u r problem,it will be more easy to understand u r query.

In the Login Page I have done:-

protected void Page_Load(object sender, EventArgs e)
    {
        Session["login"] = "no";
    }
    protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
    {
        if (myObj.UserLogin(Login1.UserName, Login1.Password) == "exists")
        {
            Response.Redirect("HomePage.aspx");
            Session["login"] = Login1.UserName;
        }
        else
            Response.Write("Please verify UserName or Password");           
    }

And tried to get Sessin value in other pages.
by:- string s=Session["login"].ToString();

but I get error that Session variable is not created.

HI,
I have a doubt here,how the application knows whether the user has account or not,i guess from your code here that,u r using login page to enter that info.when r u getting the error session variable not created,when the user has logged in with the existing username or password.

Hi,
I think i found u r answer,u are redirecting the page even before the session variable is created. so place the session first and then redirect the page.

hi

thanks for giving time in my problem.

Actually my problem is that how I can maintain login information accessiable in all the pages of the website.

Is there any other solution for it rather then using Session variable.

If I use Session variable in login page, then if user access other page of the website directly then there error will be occure that Session variable is not created.

Please solve my problem, I got tired with this work.

thanks

I guess cookies can solve u r problem but i have no idea on using cookies in my application,search msdn for further info.hope it works for u

Hi, I have found the best way to pass user information between pages is with Server.Transfer("~/page.aspx", true)

what you do is first create the string you want to pass between pages

ON SOME MOUSE CLICK OR EVENT:

dim isvalidated as string
dim username as string
isvalidated = "Yes"
username = "eric"

context.items.add("isvalid", isvalidated)
context.items.add("usern", username)
server.transfer("~/page.aspx", true)

---

once it goes to the new page you can "retrieve" these values

dim isvalid as string
dim usern as string


ON PAGELOAD FOR NEXT PAGE
isvalid = context.items("isvalid")
usern = context.items("usern")

what i usually do is validate at this point...

if isvalid = "" then
server.transfer("~/notauthorized.aspx")
else

DO WHATEVER YOU WANT

...

end if

Dear Colleagues:

I want to separate interface design from its code (in my asp.net school project). So that , I'm trying to use a code-behind using vb.net. For example in my login page I provide the user with username and password input, then handle the user input from lgoin.vb file. But I don't know how to get the values of textbox that are placed in login.aspx file.

How Can access these username and password textbox values in the login.vb code to check the user input. To give u some idea about my code, just see the following simple code:

************************
login.aspx
************************

<%@ page Inherits ="class1" scf="lgoin.vb"%>

<html>
<head></head>
<body>
<form id="form1" runat="server">

<asp:TextBox ID="txtuname" runat="server"/>
<asp:TextBox ID="txtpass" runat="server"/>
<asp:Button ID="btnlog" runat="server" Text="Button" OnClick ="button_click" />
</form>
</body>
</html>
************************
login.vb
************************
Imports Microsoft.VisualBasic
Imports system.Web
Imports system.Web.UI
Imports system.Web.UI.Control
Imports system.Web.UI.HtmlControls

Public Class Class1
Inherits Page

Sub button_click(ByVal s As Object, ByVal e As EventArgs)
'based on the the user input(username and passward)
' I want to redirect the users to different pages
' if txtpass.text= "x" and txtuname="something" then

'Response.Redirect("x.aspx")
'else
'Response.Redirect("y.aspx")
End Sub
End Class
************************
As u have seen from the code I commented some lines on login.vb file. This is because I can't access the txtuname and txtpass textboxes from lgoin.vb.


Please help me how can I do that


Regards,

Ben

This depends on a lot of things. How are you validating users in the first place? Using a database, using the web.config file? If you are using a database, it would be something like the following:

Dim Myconn As SqlConnection = New SqlConnection("server=localhost; database=dbaseName; Trusted_Connection=Yes")
Dim DBselect As String
DBselect = "select whatever from table_namewhere some condition"
Dim Mycommand As SqlCommand = New SqlCommand(DBselect, Myconn)
'Feed Parameter to database get the access level
Mycommand.Parameters.Add("@param", SqlDbType.VarChar).Value = txtUName.value

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.