大家帮忙看下那里错了?
private void Button1_Click(object sender, System.EventArgs e)
{
string strConn="server=localhost;uid=sa;pwd=;database=ASPNET";
string strCmd="select count(*) from users where UserName like '"+UserName.Text+"'";
SqlConnection myConnection=new SqlConnection();
myConnection.ConnectionString=strConn;
SqlCommand myCommand=new SqlCommand(strCmd,myConnection);
myCommand.Connection.Open();
int flag=(int)myCommand.ExecuteScalar();
myCommand.Connection.Close();
if (flag==0)
{
Response.Write("可以注册");
}
else
{
Response.Write("不可以注册");
} }
private void Button1_Click(object sender, System.EventArgs e)
{
string strConn="server=localhost;uid=sa;pwd=;database=ASPNET";
string strCmd="select count(*) from users where UserName like '"+UserName.Text+"'";
SqlConnection myConnection=new SqlConnection();
myConnection.ConnectionString=strConn;
SqlCommand myCommand=new SqlCommand(strCmd,myConnection);
myCommand.Connection.Open();
int flag=(int)myCommand.ExecuteScalar();
myCommand.Connection.Close();
if (flag==0)
{
Response.Write("可以注册");
}
else
{
Response.Write("不可以注册");
} }
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。
源错误:
行 70: int flag=(int)myCommand.ExecuteScalar();
用DATAREADER试一下
if(DataReader.Reader())
{
Response.Write("不可以注册");
}
请检查你strConn内设置是否与你的数据库匹配。
我试下先,可是这个int flag=(int)myCommand.ExecuteScalar();我在别的程序力好用
拿过来就不可以了阿,为什么呢?
我试下先,可是这个int flag=(int)myCommand.ExecuteScalar();我在别的程序力好用
拿过来就不可以了阿,为什么呢?
strConn="server=localhost;uid=sa;pwd=;database=ASPNET";
这里面的数据库名称也分大小写?
int flag =(int)myCommand.ExecuteScalar();
}
建议用这样的语句。
不过试了下。你的语句居然可以通过检查。
其他代码都很正常。