0

好的,其他错误现在已修复,此时我的其他页面需要更新其代码:

错误:对象引用未设置为对象的实例。

 public partial class Controls_GetUserScraps : System.Web.UI.UserControl

{

DataBaseClass dbClass = new DataBaseClass();
public DataTable dt;

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {

        GetUserScraps(int.Parse(Session["UserId"].ToString()));
    }
}
public void GetUserScraps(int Id)
{
    string getUserScraps = "SELECT u.Id as UserId,u.FirstName,,u.LastName,u.ImageName,s.FromId,s.ToId,s.Message,s.SendDate,s.ID as ScrapId FROM [User] as u, Scrap as s WHERE u.Id=s.FromId AND s.ToId='" + Request.QueryString["Id"].ToString() + "'";
    dt = dbClass.ConnectDataBaseReturnDT(getUserScraps);
    if (dt.Rows.Count > 0)
    {
        GridViewUserScraps.DataSource = dt;
        GridViewUserScraps.DataBind();
    }
}

我将 Session ID 替换为 UserId 并删除了错误,现在它显示了另一个错误。

错误:对象引用未设置为对象的实例。 在此处输入图像描述

string getUserScraps = "SELECT u.Id as       UserId,u.FirstName,,u.LastName,u.ImageName,s.FromId,s.ToId,s.Message,s.SendDate,s.ID as ScrapId FROM [User] as u, Scrap as s WHERE u.Id=s.FromId AND s.ToId='" +   Request.QueryString["Id"].ToString() + "'";

添加您的修复程序创建了这个新错误: 在此处输入图像描述

4

1 回答 1

0

我认为最简单的方法是让该private bool UserAuthenticate方法采用一个out参数,您可以使用该参数从数据库中返回实际的用户名。

private bool UserAuthenticate(string UserName, string Password, out string actualUserName) 
{ 
    actualUserName = string.Empty;

    bool boolReturnValue = false; 
    //-------------------------------- 
    //Check UserID From Config File 
    if (UserName == "User" && Password == "Pass") 
    { 
        boolReturnValue = true; 
        return boolReturnValue; 
    }     
    else 
    { 
        //-------------------------------- 
        dt = new DataTable(); 
        string chkUser = "Select * FROM [User] where Email='" + UserName + "' AND Password='" + Password + "'"; 
        dt = dbClass.ConnectDataBaseReturnDT(chkUser); 
        if (dt.Rows.Count > 0) 
        { 
            //TODO: grab the actual user name from the row and assign it to actualUserName. For example:
            actualUserName = dt.Rows[0]["FullName"];

            boolReturnValue = true; 
            Session["UserId"] = dt.Rows[0]["Id"].ToString(); 
            string updateLastLogin = "Update [User] SET LastLogin='" + System.DateTime.Now.ToString() + "' where Id='" + Session["UserId"].ToString() + "'"; 
            dbClass.ConnectDataBaseToInsert(updateLastLogin); 
        } 
        return boolReturnValue; 
    } 
} 

之后,您将需要在OnAuthenticate方法中执行以下操作:

string actualUserName;
Authenticated = UserAuthenticate(ctlLogin.UserName, ctlLogin.Password, out actualUserName);

重定向现在是:

Response.Redirect("Home.aspx/" + actualUserName);
于 2012-10-13T01:37:31.210 回答