C# - null异常

时间:2014-09-25 16:54:24

标签: c# asp.net system.data

任何人都可以帮我解决这个例外吗?

  

异常详细信息:System.NullReferenceException:未将对象引用设置为对象的实例。

     

第43行:uNameExists = username.Text;

代码:

protected void UserExists(string uNameExists, string eNameExists)
{
            uNameExists = username.Text;
            eNameExists = email.Text;

            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString); //creates sql connection object  to the database via connection string 
            conn.Open(); //Open Connection

            string checkUser = "select count (*) from users where UserName='" + uNameExists + "'"; //UserName query
            string checkEmail = "select count (*) from users where Email='" + eNameExists + "'"; //email query

            SqlCommand commUser = new SqlCommand(checkUser, conn);  //Command that allows connection
            SqlCommand commEmail = new SqlCommand(checkEmail, conn);  //Command that allows connection

            int tempUser = Convert.ToInt32(commUser.ExecuteScalar().ToString());
            int tempEmail = Convert.ToInt32(commUser.ExecuteScalar().ToString());

            int intUserName;
            int.TryParse(uNameExists, out intUserName);
            int intEmailName;
            int.TryParse(eNameExists, out intEmailName);

            SqlDataReader sdrUser = commUser.ExecuteReader();
            SqlDataReader sdrEmail = commEmail.ExecuteReader();


            if (tempUser != 0 | tempEmail != 0 )
            {
                Response.Write("User name or password already exists. Please enter a new one.");
                return;

            }
            conn.Close(); //Close Connection 
    }

0 个答案:

没有答案