Home > DeveloperSection > Forums > How to get logged user id c#
Ankit Singh

Total Post:341

Posted on    December-17-2013 12:38 AM

 C# C# 

 1 Reply(s)
 838  View(s)
Rate this:

I have created a simple login page but I got problem with getting logged in user ID

public partial class LoginwithEncryption : System.Web.UI.Page


    protected void btnSubmit_Click(object sender, EventArgs e)


        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conStr"].ConnectionString);


        SqlCommand cmd = new SqlCommand(

            "select * from dbo.UserInfo where Login =@Login and Password=@Password", con);

        cmd.Parameters.AddWithValue("@Login", txtUserName.Text);

        cmd.Parameters.AddWithValue("@Password", txtPWD.Text);

        SqlDataAdapter da = new SqlDataAdapter(cmd);

        DataTable dt = new DataTable();


        if (dt.Rows.Count > 0)






            ClientScript.RegisterStartupScript(Page.GetType(), "validation",

                "<script language='javascript'>alert('Invalid UserName and Password')</script>");




How can I get ID of user after login (I'm able to login) ? I tried few methods but it won't work?

Pravesh Singh

Total Post:411

Posted on    December-17-2013 1:21 AM

Hi Ankit,

Based on the info you have provided all you have to do is just extract the row data from the datatable containing the logged in user.

For example:

 //Extract data

 User objUser = new User();

 objUser.Id = int.parse(dt.Rows[0]["ID"].ToString());

 objUser.Login = dt.Rows[0]["Login"].ToString();

 objUser.Password = dt.Rows[0]["Password"].ToString();

 objUser.Type= int.parse(dt.Rows[0]["Password"].ToString());

Don't want to miss updates? Please click the below button!

Follow MindStick