新硬盘怎么装系统:求助:关于ASP.Net 编程
来源:百度文库 编辑:高考问答 时间:2024/04/29 04:22:32
小弟在用asp.net编写登陆系统页面,在实现登陆验证的后台代码时遇到报错:
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object
Source Error: Line 43:id = (Int32)myCommand.ExecuteScalar();
请问各位大虾们这是什么原因??如何除错??
其具体代码如下:
public int loginxp (String username,String password)
{
SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["SQLConnectionString"]);
SqlCommand myCommand = new SqlCommand("Pr_userlogin", myConnection);
myCommand.CommandType = CommandType.StoredProcedure;
SqlParameter parameterUsername = new SqlParameter("@username", SqlDbType.VarChar);
parameterUsername.Direction = ParameterDirection.Input;
parameterUsername.Value = username;
myCommand.Parameters.Add(parameterUsername);
SqlParameter parameterPassword = new SqlParameter("@password", SqlDbType.VarChar);
parameterPassword.Direction = ParameterDirection.Input;
parameterPassword.Value = password;
myCommand.Parameters.Add(parameterPassword);
Int32 id=0;
myConnection.Open();
id = (Int32)myCommand.ExecuteScalar();
return (int)id;
}
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object
Source Error: Line 43:id = (Int32)myCommand.ExecuteScalar();
请问各位大虾们这是什么原因??如何除错??
其具体代码如下:
public int loginxp (String username,String password)
{
SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["SQLConnectionString"]);
SqlCommand myCommand = new SqlCommand("Pr_userlogin", myConnection);
myCommand.CommandType = CommandType.StoredProcedure;
SqlParameter parameterUsername = new SqlParameter("@username", SqlDbType.VarChar);
parameterUsername.Direction = ParameterDirection.Input;
parameterUsername.Value = username;
myCommand.Parameters.Add(parameterUsername);
SqlParameter parameterPassword = new SqlParameter("@password", SqlDbType.VarChar);
parameterPassword.Direction = ParameterDirection.Input;
parameterPassword.Value = password;
myCommand.Parameters.Add(parameterPassword);
Int32 id=0;
myConnection.Open();
id = (Int32)myCommand.ExecuteScalar();
return (int)id;
}
myCommand.ExecuteScalar(); 返回了null,所以会出现这个错误.