简体   繁体   中英

How to Get UserID from Custom Table

i would like to be able to get the ID of the Current user logged in on my Site.

The thing is that i'm not trying to get the Guid, but The User_ID, that is a field that i have on a custom table of mine called Users. On that table i have User_ID that is the PK and UserId that is a FK from the table aspnet_Users.

The reason i want this field is because i have a table Purchase that every time an User LOGGED IN presses the button saying (Buy), a new saleId is incremented and the User_ID that bought it. On my table Users the User_ID is of int type that starts at 1 and also increments every time a newUser registers on the site.

So it's easier to check an user by ID of (1,2,3) that an unique Identifier with 30 characters

What i have in mind is something like this:

protected void Button1_Click(object sender, EventArgs e)        
{
    string InsertSql = "INSERT INTO [Purchase] (User_ID) VALUES (@User_ID)";

            using (Connection)
            {

                Connection.Open();
                SqlCommand com = new SqlCommand(InsertSql, Connection);

                com.Paramaters.AddWithValue("@User_ID",  ????);

                com.ExecuteNonQuery();
                cn.Close(); 

             }
} 

Where is ???? i need to get somehow the User_ID of the current User logged in that is on my custom table Users.

Thanks for the help

After Log-in into your application you should maintain the userid into session variable ..so that later on you can use that directly.

Example:-

    //while user logged-in,push userid into session variable:-

    Session["userid"] = 12;

    //And later on in oyur page you can use it like :-

   com.Paramaters.AddWithValue("@User_ID",  (int)Session["userid"]);

Assuming as you say that using is logged in

Use User.Identity.Name .

You check if user is authenticated using:

User.Identity.IsAuthenticated

If will give you the name of the current logged user in your asp.net application.

com.Paramaters.AddWithValue("@User_ID",  User.Identity.Name);

If you are using Membership then

Gets the name if authenticated.

if (User.Identity.IsAuthenticated)
    Label1.Text = HttpContext.Current.User.Identity.Name;
else
    Label1.Text = "No user identity available.";

If you want to get userid then run a select query like

Select userid from tablename where username = 'pass here username';

by running this query you will get userid and use that userid when you need.

Parametrized Query:

com.Paramaters.AddWithValue("@User_ID",  HttpContext.Current.User.Identity.Name);

and if you are not using Membership then at the time of login get userid from your username and use any where you want to.

Note: Make sure that you have unique username otherwise it will give you multiple userid .

Logic 2: Use the ProviderUserKey member of the MembershipUser object, like this:

MembershipUser user = Membership.GetUser();
string userid = user.ProviderUserKey.ToString();

Hope it works..

If I understood right, you have a custom table Users , with PK User_ID and a column UserId that is a FK to the table aspnet_Users.

You already know the UserName of the logged in user, so why not simply do something like:

string InsertSql = @"
    INSERT INTO [Purchase] (User_ID)
    SELECT U.User_ID FROM Users U
    INNER JOIN aspnet_Users AU ON U.UserId = AU.UserId
    WHERE AU.LoweredUserName = LCASE(@UserName)
     ";

Your @UserName parameter is then the name of the current logged on user that you already know ( HttpContext.Current.User.Name ), and you don't need to know the User_ID.

i finally did it, i kinda moved on to other stuff that i needed to do on my Site, so i just figured it out today.

I just wanna say thanks for all the replies and leave the solution to my own problem. Basically what i was doing wrong was the Select command. I'm not very good at SQL, so i didn't know were to place it xD, but now i know.

protected void Button1_Click(object sender, EventArgs e)

{

    MembershipUser usr =  Membership.GetUser(HttpContext.Current.User.Identity.Name);
    Guid usrid = (Guid)usr.ProviderUserKey;

    String product= DropDownList1.SelectedValue.ToString();

    string InsertSql= "INSERT INTO [Purchase] (Product_ID, User_ID) VALUES (@Product_ID,(SELECT User_ID from [Users] where UserId = @UserId))";


        using (Connection)
        {
            Connection.Open();
            SqlCommand com = new SqlCommand(InsertSql, Connection);

            com.Parameters.AddWithValue("@Product_ID", product);
            com.Parameters.AddWithValue("@UserId", usrid);

            com.ExecuteNonQuery();
            Connection.Close();

        }

    Response.Redirect("Purchase.aspx");
} 

Once again, thanks for all the patience and help Take Care

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM