此代码在visual studio 2012中给出了以下错误
SqlConnection conn = new SqlConnection(
ConfigurationManager.ConnectionString["erpConnStr"].ConnectionString);
conn.Open();
String validateUserQry = "SELECT * FROM members WHERE username = '" + username.Text + "'";
SqlCommand validateUserComm = new SqlCommand(validateUserQry, conn);
validateUserComm.ExecuteNonQuery();
if (validateUserComm.ExecuteScalar().ToString() == null)
{
errorMsg.Visible = true;
msg.Text = "Username was not Correct ! ";
}
错误是:
对象引用未设置为对象的实例。
我可以说这个错误发生在这一行:
if (validateUserComm.ExecuteScalar().ToString() == null)
为什么?
答案 0 :(得分:3)
丢失ToString()
:
if (validateUserComm.ExecuteScalar() == null)