Click here to Skip to main content
15,885,910 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hey

how to save a userid in session
like i have a user table in this table there is a column userid,name..
and there is another table which is documnet in this columns are docid,docname,file,userid
when usersignup the his.her info is save in user table like this
userid username
1 abc

userid is primary key
then when this same user upload documnet like
docid doname file userid
1 def math.docx null

then i want userid in document like this
docid docname file userid

1 def math.docx 1


then how done this and how save userid in session

this is the lgin code and in this i save username in session

C#
protected void Btn_Login_Click(object sender, EventArgs e)
  {
      try

      {

          int users = aa.SignIn(txt_username.Value, txt_pass.Value);



         if (users == 1)

          {
              //Session["UserID"] = aa.SignIn(txt_username.Value, txt_pass.Value);

              Session["UserID"] = txt_username.Value;

              Session["UserTypeID"] = users;

              Response.Redirect("AdminOp.aspx");

          }

          else if (users == 2)

          {
              //Session["UserID"] = aa.SignIn(txt_username.Value, txt_pass.Value);


              Session["UserID"] = txt_username.Value;

              Session["UserTypeID"] = users;

              Response.Redirect("leave.aspx");

          }


      }

      catch

      {

          Label8.Text= "Incorrect User Name or Password";

      }
Posted

C#
Int user_id;
if (Session["UserID"] != null)
       {
           //Retrieving UserName from Session
           user_id =  int.Parse(Session["UserID"]);
       }
       else
       {
        //Do Something else
       }

Now assign the user_id to column of that contains documents
 
Share this answer
 
v3
Comments
Diya Ayesa 3-Oct-13 6:53am    
where i place this i also use if use ....
Diya Ayesa 3-Oct-13 7:03am    
when i try this
like this
int user_id;

if (users == 1)

{
if (Session["UserID"] != null)


{
user_id = Session["UserID"];

//Session["UserID"] = aa.SignIn(txt_username.Value, txt_pass.Value);

Session["UserID"] = txt_username.Value;

Session["UserTypeID"] = users;

Response.Redirect("AdminOp.aspx");

}


then it show me error
Cannot implicitly convert type 'object' to 'int'. An explicit conversion exists (are you missing a cast?)
Hey there,

Here is a suggestion:
You can use your
C#
SignIn(txt_username.Value, txt_pass.Value)

to return the User record from Table in a DataTable if the login is successful.
This way you,ll be able to get both UserTypeID and UserID related to the current user.
Hence, you can save these two fields in the Session object, suppose dt is the returned DataTable e.g.
C#
Session["UserID"] = dt.Rows[0]["UserID"].ToString();


and when you want to use this UserID at the time of saving document records, you can do this to get the UserID from Session.
C#
if(Session["UserID"] != null)
{
    long UserID = Convert.ToInt64(Session["UserID"].ToString());
}


Hope it helps.

Azee...
 
Share this answer
 
Comments
Diya Ayesa 3-Oct-13 6:54am    
i use in if like this ...
if (users == 1)

{
//Session["UserID"] = aa.SignIn(txt_username.Value, txt_pass.Value);

Session["UserID"] = txt_username.Value;

Session["UserTypeID"] = users;

Response.Redirect("AdminOp.aspx");

}

as i above mentioed so how i use this if(session["userid"]!=null)
Azee 3-Oct-13 6:58am    
Use it when you need the UserID to insert in the documents table. Get the UserID as stated above and pass it to the procedure that is inserting document records.

Note: I used long, because I supposed UserID DataType is Bigint, if its Int then you can use Int here as well.
Diya Ayesa 3-Oct-13 7:08am    
when i use like this it shows me error


if(Session["UserID"]!=null)
{
long userid = Convert.ToUInt64(Session["UserID"].ToString());

if (users == 1)
{


ERROR
" Cannot implicitly convert type 'ulong' to 'long'. An explicit conversion exists (are you missing a cast?) "
Diya Ayesa 3-Oct-13 7:27am    
k i try this one


int user_id;
if (Session["UserID"] != null)
{
user_id = Convert.ToInt32(Session["UserID"].ToString());


if (users == 1)
{


//Session["UserID"] = aa.SignIn(txt_username.Value, txt_pass.Value);

Session["Login2"] = txt_username.Value;

Session["UserTypeID"] = users;

Response.Redirect("AdminOp.aspx");

}
}

else if (users == 2)



but in table userid show 0

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