Session problems with LOGIN/Validating

P

Patrick.O.Ige

I have a problem with the code below:-
When i use a username for example dog for the first time it works but later
when i use cat for example it keeps showing
Hello:- Dog..
It keeps DOG in the session for long.

But i want to get the USERNAME from the datbase and display it
so it catches only the username from the DB only..

I think 'm not placing the "Session["name"] = username.Text; "
CAN ANYBODY ADVICE WHERE I SHOULD PUT THE "SESSION"

Help if u can..PLease..look at the code properly
Thx

void Login_Click(Object sender, EventArgs e)
{

SqlDataReader dr;

SqlConnection cn = new SqlConnection("Data Source=(local);Initial
Catalog=Northwind;Integrated Security=SSPI;");

cn.Open();

SqlCommand cmdQuestion = new SqlCommand("SELECT Password FROM Users WHERE
username = '" + username.Text + "'", cn);

dr=cmdQuestion.ExecuteReader();





if(dr.Read())

if(dr["Password"].ToString() == Password.Text)


// CookieAuthentication.RedirectFromLoginPage(Email.Text,
false);
Response.Redirect("testlabel.aspx");

else
Msg.Text = "Invalid password.";


else
Msg.Text = "Email address not found.";


Session["name"] = username.Text;
cn.Close();
}
 
P

Peter Bromberg [C# MVP]

if(dr["Password"].ToString() == Password.Text)
Session["name"] = username.Text;
// every time a new user is successfully matched, it is at that point
// that you want to store their entered username in Session.
 
P

Patrick Ige

Thx for the advice..
but when i do:-it says error:-"Invalid attempt to read when no data is
present."
Can you help me with the code!!
Thx


if(dr["Password"].ToString() == Password.Text) {
Session["name"] = username.Text; }
// CookieAuthentication.RedirectFromLoginPage(Email.Text, false);
Response.Redirect("testlabel.aspx");
// else Msg.Text = "Invalid password.";

if(dr.Read()) {
//else
}
Msg.Text = "Email address not found.";

//Session["name"] = username.Text;
//Session["name"] = dr["username.Text"].ToString();
//Session.Terminate();
cn.Close();
 
P

Patrick Ige

Hi Peter..
Well I did this(And its WORKING !!!! ):-
Thx mo!!U are the best in the FORUM!!

if(dr.Read()) if(dr["Password"].ToString() == Password.Text)
{
Session["name"] = username.Text;
// CookieAuthentication.RedirectFromLoginPage(Email.Text, false);
Response.Redirect("testlabel.aspx");
}
else
Msg.Text = "Invalid password.";
else
Msg.Text = "Email address not found.";
cn.Close();
 

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

Top