Click here to Skip to main content
15,892,797 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My Question may be very simple but i am not able to solve this problem lets say i am retrieving user details in a query i.e
C#
SqlCommand comand = new SqlCommand("Select Userid,UserName,Password FROM Users WHERE UserName=@Uname and Password = @Pass", con);
        comand.Parameters.AddWithValue("@Uname", this.txtUsername.Text);
        comand.Parameters.AddWithValue("@pass", this.txtPassword.Text);
  con.Open();
       SqlDataAdapter adp = new  SqlDataAdapter(comand);
        DataTable t = new DataTable();
        adp.Fill(t);

I want to get Userid separately to further use in a Query or i may store USerid of the user who has loged in into Session How i can do this??
Posted
Updated 16-Feb-14 3:54am
v2

Store it in the session, it's pretty simple:
C#
Session["UserId"] = myUserId;

And then
C#
if (Session["UserId"] == null)
   {
   ... redirect to login page
   }
else
   {
   string userId = (string) Session["UserId"];
   ...
   }


But please, don't do that! Never store passwords in clear text - it is a major security risk. There is some information on how to do it here: Password Storage: How to do it.[^]
 
Share this answer
 
C#
SqlCommand comand = new SqlCommand("Select Userid,UserName,Password FROM Users WHERE UserName=@Uname and Password = @Pass", con);
        comand.Parameters.AddWithValue("@Uname", this.txtUsername.Text);
        comand.Parameters.AddWithValue("@pass", this.txtPassword.Text);
  con.Open();
       SqlDataAdapter adp = new  SqlDataAdapter(comand);
        DataTable t = new DataTable();
        adp.Fill(t);

string userId;
foreach(DataRow dr in t.Rows)
{
userId = dr["Userid"].ToString();
}

and then store it in the session, i.e
C#
Session["userId"] = userId;

Hope this is pretty clear to you :)

-KR
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900