Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- MembershipUser user = Membership.GetUser();
- string id = user.ProviderUserKey.ToString();
- SqlConnection connection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Connection"].ConnectionString);
- try
- {
- using (connection)
- {
- using (SqlCommand con_get = new SqlCommand("SELECT firstname FROM x WHERE userId='" + id + "'", connection)) //Executes SQL command
- {
- try
- {
- connection.Open(); //Opens the database connection
- using (SqlDataReader reader = con_get.ExecuteReader())
- {
- if (reader != null)
- {
- while (reader.Read())
- {
- Label label1 = (Label)sender;
- label1.Text = reader["x"].ToString();
- }
- }
- }
- }
- catch
- {
- }
- finally
- {
- connection.Close(); //Closes the database connection
- }
- }
- }
- }
- catch
- {
- }
- protected void Page_Load(object sender, EventArgs e)
- {
- if(!Page.IsPostBack)
- {
- var welcomeLabel = Page.Master.FindControl("lblWelcome") as Label;
- SetName(welcomeLabel);
- }
- }
- protected void SetName(Label welcomeLabel)
- {
- //this is the type of thing you'd probably wanna do in the Global.asax on session start
- if (Session["userName"] == null || !Login()) return; //session variable is empty and the login attempt failed, give up
- var usersName = Session["userName"].ToString();
- welcomeLabel.Text = usersName;
- }
- protected bool Login()
- {
- const string query = "SELECT Name FROM Users WHERE Password = @Password AND UserName = @UserName";
- using (var conn = new SqlConnection(connectionString))
- {
- using (var comm = new SqlCommand(query, conn))
- {
- comm.CommandType = CommandType.Text;
- comm.Parameters.Add(new SqlParameter("@Password", password)); //or get this from a control or wherever
- comm.Parameters.Add(new SqlParameter("@UserName", userName)); //or get this from a control or wherever
- conn.Open();
- var name = (string)comm.ExecuteScalar();
- if (!string.IsNullOrEmpty(name))
- {
- Session["userName"] = name;
- return true;
- }
- //"Login information is wrong or user doesn't exist.
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement