public static bool AdminLogin(Admin ad)
{
using (DataBase db = new DataBase())
{
SqlCommand com = new SqlCommand();
com.Connection = db.Connection;
com.CommandType = CommandType.StoredProcedure;
com.CommandText = "dbo.hs_AdminLogin";
com.Parameters.Add("@aName",SqlDbType.VarChar,100).Value = ad.aName;
com.Parameters.Add("@aPwd", SqlDbType.VarChar, 100).Value = ad.aPwd;
int count = Convert.ToInt32(com.ExecuteScalar());
return count > 0 ? true : false;
}
}
报这个错:
ExecuteScalar: Connection property has not been initialized.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.InvalidOperationException: ExecuteScalar: Connection property has not been initialized.Source Error:
Line 92: com.Parameters.Add("@aName",SqlDbType.VarChar,100).Value = ad.aName;
Line 93: com.Parameters.Add("@aPwd", SqlDbType.VarChar, 100).Value = ad.aPwd;
Line 94: int count = Convert.ToInt32(com.ExecuteScalar());
Line 95: return count > 0 ? true : false;
Line 96: }
{
using (DataBase db = new DataBase())
{
SqlCommand com = new SqlCommand();
com.Connection = db.Connection;
com.CommandType = CommandType.StoredProcedure;
com.CommandText = "dbo.hs_AdminLogin";
com.Parameters.Add("@aName",SqlDbType.VarChar,100).Value = ad.aName;
com.Parameters.Add("@aPwd", SqlDbType.VarChar, 100).Value = ad.aPwd;
int count = Convert.ToInt32(com.ExecuteScalar());
return count > 0 ? true : false;
}
}
报这个错:
ExecuteScalar: Connection property has not been initialized.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.InvalidOperationException: ExecuteScalar: Connection property has not been initialized.Source Error:
Line 92: com.Parameters.Add("@aName",SqlDbType.VarChar,100).Value = ad.aName;
Line 93: com.Parameters.Add("@aPwd", SqlDbType.VarChar, 100).Value = ad.aPwd;
Line 94: int count = Convert.ToInt32(com.ExecuteScalar());
Line 95: return count > 0 ? true : false;
Line 96: }
说的很清楚啊是不是没设置connectionString?
int count = Convert.ToInt32(com.ExecuteScalar());
------
int count=int.parse(com.ExecuteScalar().ToString());
你设置传回个count,肯定不会报错,你不要这样写,感觉写的很乱
看看Sqlconnection对象OPEN没有啊