C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Using Sessions in Login page and how to maintain the state in all pages of application


Posted Date: 30 Aug 2008      Total Responses: 4

Posted By: MSB       Member Level: Bronze     Points: 1


Hello members,

I am new to .net and this is my first post. I have made a login page for a small website whihc I am making. I am doing the website in C#.net. Sqlserver 2005. I am getting confused about how to use sessions in login page and how to maintain the state in all other pages of my website.

Please help me with some sample code.. that would be greatly appreciated. I got struck basically in how to carry the username and password to all the pages to maintain state.

Please help me out by givnig some sample code. My login page has this code,



protected void Button1_Click(object sender, EventArgs e)
{


SqlConnection myconn = new SqlConnection();
SqlCommand mycmd = new SqlCommand();
SqlDataReader myDataReader;


try
{
myconn.ConnectionString = ConfigurationManager.ConnectionStrings["con_str"].ConnectionString;
myconn.Open();



string myTxtsql = "select username,password from student where username = " + " '" + TextBox1.Text + "' " + " and password= " + " '" + TextBox2.Text + "' ";

mycmd.CommandText = myTxtsql;




mycmd.Connection = myconn;

myDataReader = mycmd.ExecuteReader();

Session["username"] = TextBox1.Text;





int i = 0;

while (myDataReader.Read())
{
i = i + 1;
}
if (i == 1)
{
Response.Redirect("welcome.aspx");
}
else
{
Label3.Text = "Invalied details";
}

myDataReader.Close();
}

catch(Exception err)
{
Response.Write(err.Message.ToString());
Console.WriteLine(err.Message.ToString());
Trace.Warn(err.Message.ToString());
}
finally
{
mycmd.Dispose();
myconn.Close();
}

}


Hoping for an early and a positive reply..

Regards

MSB..






Responses

Author: Pradeep Kumar Chaudhary    30 Aug 2008Member Level: GoldRating:     Points: 2
All data in a server in uniquely identified is called a Primary key.
When a user is logged in successfully then the logged username or ID is stored in the session like:
Session["UserName"]="<loggedinusername>"
Session["UserId"]="<UserId>"
The logged in UserID or UserName will be used to retreive the value corresponding to the data.
When the User Login successfully, a session will be created to maintain these things.
I think you got your answer, If u have any query then u can post them.

Regards,
Pradeep


Author: Barani    30 Aug 2008Member Level: SilverRating:     Points: 2
d


Author: MSB    30 Aug 2008Member Level: BronzeRating:     Points: 1
Hi....

I found answer for my question...

we need to use Session.IsNewSession




Author: bill    31 Aug 2008Member Level: GoldRating:     Points: 2
hi,
Session["username"] = TextBox1.Text;
Session["pass word"] = TextBox2.Text;
the both session can be carried out to all pages .
the session time out by default is twenty minutes.you can also set the
the time.


Post Reply
You must Sign In to post a response.
Next : How to display Page Header in DrillDown crystal report
Previous : Validation in TextBox in ASP.NET
Return to Discussion Forum
Post New Message
Category: ASP.NET

Related Messages



dotNet Slackers   BizTalk Adaptors    Web Design

web conferencing services

Contact Us    Privacy Policy    Terms Of Use