private void btnsubmit_Click(object sender, System.EventArgs e)
{
SqlConnection con=db.dbcon();
con.Open();
SqlCommand logincmd=new SqlCommand("select userid from tuser where username='"+this.tbxusername.Text+"' and userpwd='"+this.tbxpwd.Text+"'",con);
int userid=(int)logincmd.ExecuteScalar();
if (userid>0)
{
Response.Write("成功");
}
else
{
Response.Write("失败");
}
con.Close();
}
我的程序代码如上所示。tuser表的userid列为int型。这样执行程序时会出现如下错误代码:异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。
但将SqlCommand logincmd=new SqlCommand("select userid from tuser where username='"+this.tbxusername.Text+"' and userpwd='"+this.tbxpwd.Text+"'",con);代码改换为SqlCommand logincmd=new SqlCommand("select count(userid) from tuser where username='"+this.tbxusername.Text+"' and userpwd='"+this.tbxpwd.Text+"'",con);
就能正常运行,谁能告诉我?先谢谢了
{
SqlConnection con=db.dbcon();
con.Open();
SqlCommand logincmd=new SqlCommand("select userid from tuser where username='"+this.tbxusername.Text+"' and userpwd='"+this.tbxpwd.Text+"'",con);
int userid=(int)logincmd.ExecuteScalar();
if (userid>0)
{
Response.Write("成功");
}
else
{
Response.Write("失败");
}
con.Close();
}
我的程序代码如上所示。tuser表的userid列为int型。这样执行程序时会出现如下错误代码:异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。
但将SqlCommand logincmd=new SqlCommand("select userid from tuser where username='"+this.tbxusername.Text+"' and userpwd='"+this.tbxpwd.Text+"'",con);代码改换为SqlCommand logincmd=new SqlCommand("select count(userid) from tuser where username='"+this.tbxusername.Text+"' and userpwd='"+this.tbxpwd.Text+"'",con);
就能正常运行,谁能告诉我?先谢谢了
解决方案 »
- 现在C# winform 开发界面是不是都用WPF?
- 有关Winform上添加一排按钮 上面有删除 添加 保存 放大缩小等按钮的问题
- 3D碰撞检测里“碰撞”是什么意思
- urlrewrite中通过RewritePath重写,如何判断Request.HttpMethod是GET还是POST?
- 请问用C#如何管理远程的计算机和iis?
- 为什么VS2003中调用自定义的类时,相关的方法属性之类的不能自动的列举出来?
- 请教高手如何用程序(VB或C#)提取CAXA二维图纸标题栏和明细表信息
- (10分) 用System.Data.Oledb 没问题,但用System.Data.OracleClinet 报错,请高手指点
- ComboBox 数据绑定问题?
- richTextBox1.AppendText(builder.ToString())无法执行
- 怎样用myR获得下一个数据集记录
- 如何让用户不能更改TextBox的值能能查看
而select count(userid)查不到数据时返回的是0,此时(int)logincmd.ExecuteScalar();可以正常运行
object obj = logincmd.ExecuteScalar();
if (obj != DBNull.Value)
{
int userid=(int)obj;
}
else
{
Response.Write("失败");
}