【问题标题】:save username but shows name in c#保存用户名但在 c# 中显示名称
【发布时间】:2012-08-30 06:47:18
【问题描述】:

我正在使用 c#,在 vs 2008 框架 3.5 中,我需要保存用户名以及何时保存 用户登录,它显示他/她的名字和姓氏,此时可以保存它,并且 这就是我想要的,但需要向用户展示他/她的用户名而不是他/她的用户名 姓。这是我的代码,在此先感谢。

//code in the login
ClPersona login = new ClPersona();
bool isAuthenticated = login.sqlLogin1((txtUsuario.Text), (txtPassword.Text));
if (isAuthenticated)
{
Session["sesionicontrol"] = login.NombreUsuario;
Response.Redirect("../MENU/menu1.aspx");
}

//code in the form where shows the username but I want the first and last name

public partial class menu2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
lblNombreUsuario.Text = (string)Session["sesionicontrol"]; 
if (!IsPostBack)


//ClPersona class

public Boolean sqlLogin1(string nombreUsuario, string password)
{
string stSql = "select * from usuarios where usuario='" + nombreUsuario + "' and
pass='" + password + "'and id_rol='1'";
Bd miBd = new Bd();
DataTable dt = miBd.sqlSelect(stSql);
DataSet ds = new DataSet();
ds.Tables.Add(dt);
//return ds;
if (dt.Rows.Count > 0)
{
return true;
}
else
{
return false;
}
}

【问题讨论】:

  • login 对象是否提供对属性的访问以检索可以存储在会话中的名字和姓氏?
  • @mellamokb 不,问题是我需要将用户名保存在我的数据库中,但用户必须看到他/她的名字和姓氏而不是他/她的用户名,提前谢谢跨度>
  • 请显示 sqlLogin1 方法和 ClPersona 类

标签: c# session session-variables


【解决方案1】:

修改两个部分

第一个

if (isAuthenticated)
{
Session["YouObject"] = login;//<--

Session["sesionicontrol"] = login.NombreUsuario;
Response.Redirect("../MENU/menu1.aspx");
} 

第二

protected void Page_Load(object sender, EventArgs e)
{
   if(Session["YouObject"] != null)
   { 
     ClPersona obj = (ClPersona )Session["YouObject"];
     lblNombreUsuario.Text = string.Format("{0}-{1}", obj.FirstName, obj.LastName ) 


   }

      lblNombreUsuario.Text = (string)Session["sesionicontrol"];
     ....
}

【讨论】:

    猜你喜欢
    • 2020-03-29
    • 2023-04-04
    • 1970-01-01
    • 2022-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-19
    • 1970-01-01
    相关资源
    最近更新 更多