locked
Get Single Value From SQL Database [C#] RRS feed

  • Question

  • User-1012551284 posted

    I am currently making a membership system, and I already made the login system. When I log in, it sets the user id from the database as a session named UserId to indicate that the user are logged in.

    My problem is, when I need to make a welcome message, and in that message the users name will appear. How can I get the name only from the database without overkilling it with a ASP.NET Repeater or the SQLDataReader function in the backend?

    Extra info, I am working in Web Forms.

    English is not my primary language, but I hope you understand anyway. Any help is appreciated!

    Thanks in advance, Jens Kvist

    Wednesday, May 28, 2014 7:33 AM

Answers

  • User-821857111 posted

    In your code behind (Page_Load), you can do this:

    var connect = <your connection string>;
    var sql = "SELECT UserName From Users WHERE UserId = @0";
    using (var conn = new SqlConnection(connect))
    using (var cmd = new SqlCommand(sql, conn)){
        cmd.Parameters.AddWithValue("@0", Session["UserId"]);
        conn.Open();
        Label1.Text = cmd.ExecuteScalar();
    }

    That's about as lean as it can be, and assumes that you want the username to be assigned to a label.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 28, 2014 8:22 AM
  • User-760709272 posted

    You could create a "User" or a "UserInfo" class that just has some basic essential data like their ID, name, username and email address and store that object in the session as well

    Session["User"] = userObject;

    Then if you need to show their name somewhere you just get it from that object

    User userObject = (User)Session["User"];

    LiteralWelcome.Text = "Hello " + userObject.Name

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 28, 2014 8:27 AM

All replies

  • User-821857111 posted

    In your code behind (Page_Load), you can do this:

    var connect = <your connection string>;
    var sql = "SELECT UserName From Users WHERE UserId = @0";
    using (var conn = new SqlConnection(connect))
    using (var cmd = new SqlCommand(sql, conn)){
        cmd.Parameters.AddWithValue("@0", Session["UserId"]);
        conn.Open();
        Label1.Text = cmd.ExecuteScalar();
    }

    That's about as lean as it can be, and assumes that you want the username to be assigned to a label.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 28, 2014 8:22 AM
  • User-760709272 posted

    You could create a "User" or a "UserInfo" class that just has some basic essential data like their ID, name, username and email address and store that object in the session as well

    Session["User"] = userObject;

    Then if you need to show their name somewhere you just get it from that object

    User userObject = (User)Session["User"];

    LiteralWelcome.Text = "Hello " + userObject.Name

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 28, 2014 8:27 AM
  • User-1012551284 posted

    Thank you, that helped alot :)

    Wednesday, May 28, 2014 9:30 AM
  • User-1012551284 posted

    Thanks for your answer, I got it to work :)

    Wednesday, May 28, 2014 9:31 AM