//// userName must not be null
if (null == userName)
{
return false;
}
//// passWord must not be null
if (null == passWord)
{
return false;
}
try
{
//// connect to your SQL Server.
conn = DB connection string;
string sqlUserName;
string sqlfirstname;
//// Create SqlCommand to select passwor and username fields from UserData table given supplied userName passWord.
sqlUserName = "SELECT UserName,Password FROM [your table name] ";
sqlUserName += " WHERE (UserName ='" + userName + "')";
sqlUserName += " AND (Password ='" + passWord + "')";
SqlCommand com = new SqlCommand(sqlUserName, conn);
//// Create SqlCommand to select firstname and lastname fields from UserData table.
sqlfirstname = "SELECT FirstName,LastName FROM [your table name] ";
sqlfirstname += " WHERE (UserName ='" + userName + "')";
SqlCommand command2 = new SqlCommand(sqlfirstname, conn);
currentName = (string)com.ExecuteScalar();
firstname = (string)command2.ExecuteScalar();
//// Cleanup command and connection objects.
conn.Dispose();
conn.Close();
}
catch (Exception ex)
{
// Add error handling here for debugging.
// This error message should not be sent back to the caller.
System.Diagnostics.Trace.WriteLine("[ValidateUser] Exception " + ex.Message);
}
if (currentName != null)
{
Session["UserAuthentication"] = userName;
Session["firstname"] = firstname;
//// Session.Timeout is set in web.config file.
return true;
}
else
{
Session["UserAuthentication"] = string.Empty;
return false;
}
}
then add this line of code to .aspx file you want to display the firstname. for example master.aspx
agiles
Member
2 Points
1 Post
Re: Display user name in the header when logged in
Mar 28, 2011 11:01 AM|LINK
when you validate the user, you can catch firstname in session then you can used it whatever you want it.
in code behind file where the login validation, something like this:
internal bool ValidateUser(string userName, string passWord)
{
SqlConnection conn;
string currentName = null;
string firstname = null;
//// userName must not be null
if (null == userName)
{
return false;
}
//// passWord must not be null
if (null == passWord)
{
return false;
}
try
{
//// connect to your SQL Server.
conn = DB connection string;
string sqlUserName;
string sqlfirstname;
//// Create SqlCommand to select passwor and username fields from UserData table given supplied userName passWord.
sqlUserName = "SELECT UserName,Password FROM [your table name] ";
sqlUserName += " WHERE (UserName ='" + userName + "')";
sqlUserName += " AND (Password ='" + passWord + "')";
SqlCommand com = new SqlCommand(sqlUserName, conn);
//// Create SqlCommand to select firstname and lastname fields from UserData table.
sqlfirstname = "SELECT FirstName,LastName FROM [your table name] ";
sqlfirstname += " WHERE (UserName ='" + userName + "')";
SqlCommand command2 = new SqlCommand(sqlfirstname, conn);
currentName = (string)com.ExecuteScalar();
firstname = (string)command2.ExecuteScalar();
//// Cleanup command and connection objects.
conn.Dispose();
conn.Close();
}
catch (Exception ex)
{
// Add error handling here for debugging.
// This error message should not be sent back to the caller.
System.Diagnostics.Trace.WriteLine("[ValidateUser] Exception " + ex.Message);
}
if (currentName != null)
{
Session["UserAuthentication"] = userName;
Session["firstname"] = firstname;
//// Session.Timeout is set in web.config file.
return true;
}
else
{
Session["UserAuthentication"] = string.Empty;
return false;
}
}
then add this line of code to .aspx file you want to display the firstname. for example master.aspx
<div class="link" id="divid" style="float: right">
<asp:LoginName id="LoginName1" runat="server" />
</div>
and then add this line of code in the .cs file ex:(master.cs) under page_load.
protected void Page_Load(object sender, EventArgs e)
{
////this can come from anywhere like session, database
string firstname = (string)(Session["firstname"]);
LoginName1.FormatString = "Sign in as" + " - " + firstname; ////output: Sign in as - username
}
IS this helpful for you???????