为什么会出现这种情况:System.NullReferenceException:对象引用未设置为对象的实例

时间:2015-05-12 10:45:56

标签: c# asp.net visual-studio-2013

我在这里阅读了其他答案但没有任何效果。问题是如果用户输入了错误的密码,我会收到此错误(用户名很好):

System.NullReferenceException:未将对象引用设置为对象的实例。

堆栈追踪:

[NullReferenceException: Object reference not set to an instance of an object.]
   Login.btnLogin_Click(Object sender, EventArgs e) in c:\Users\Michelle\Desktop\COMF510_65300_HS_task_2\Login.aspx.cs:31
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9628614
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +103
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724

login.aspx.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;

public partial class Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnLogin_Click(object sender, EventArgs e)
    {
        SqlConnection myDB = new SqlConnection(ConfigurationManager.ConnectionStrings["LoginConnectionString"].ConnectionString);
        myDB.Open();
        string checkUser = "select count (*) from users where username = '"+txtUserName.Text+"'";
        SqlCommand com = new SqlCommand(checkUser, myDB);
        int temp = Convert.ToInt32(com.ExecuteScalar().ToString());
        myDB.Close();

        if (temp == 1)
        {
            myDB.Open();
            string checkPassWord = "select password from users where password = '" + txtPassword.Text + "'";
            SqlCommand passCom = new SqlCommand(checkPassWord, myDB);
            string pass = passCom.ExecuteScalar().ToString().Replace(" ","");
            if(pass == txtPassword.Text)
            {
                Session["New"] = txtUserName.Text;
                Response.Redirect("EmpWelcome.aspx");
            }
            else
            {
                Response.Write("Incorrect details!  Please try again.");// if password is incorrect
            }
        }
        else
        {
            Response.Write("Incorrect details!  Please try again."); // if username is incorrect
        }
    }
}

源错误:

第31行:string pass = passCom.ExecuteScalar()。ToString()。Replace(“”,“”);

我确信这对于专家来说很容易解决,但我在C#中很新。提前谢谢。

1 个答案:

答案 0 :(得分:1)

com.ExecuteScalar()为空(未找到结果)

尝试以下代码(并且也不记得按using构造配置db对象)。

protected void btnLogin_Click(object sender, EventArgs e)
{
    using (SqlConnection myDB = new SqlConnection(ConfigurationManager.ConnectionStrings["LoginConnectionString"].ConnectionString))
    {
        myDB.Open();
        string passwordObject = "select password from users where username = '" + txtUserName.Text + "'";
        using (SqlCommand com = new SqlCommand(passwordObject, myDB))
        {
            var res = com.ExecuteScalar();
            if (res != null)
            {
                string checkPassWord = passwordObject as string;
                if (txtPassword.Text == checkPassWord)
                {
                    Session["New"] = txtUserName.Text;
                    Response.Redirect("EmpWelcome.aspx");
                }
                else
                {
                    Response.Write("Incorrect details!  Please try again.");// if password is incorrect
                }
            }
            else
            {
                Response.Write("Incorrect details!  Please try again."); // if username is incorrect
            }
        }
    }
}