用户代码未处理 System.NullReferenceExceptionMessage="未将对象引用设置到对象的实例。"

tulu315060844 2010-04-10 01:29:46
private bool checkname(string username, string idcard)
{
bool bl = false;
SqlConnection sqlconnection = new SqlConnection(Program.GetConnectionString());
SqlCommand dbcommand = new SqlCommand();
dbcommand.Connection = sqlconnection;
dbcommand.CommandType = System.Data.CommandType.Text;
dbcommand.CommandText = "select userid from resume where name='" + username + "'and idcard='" + idcard + "';";
dbcommand.Connection.Open();
int n = (int)dbcommand.ExecuteScalar();
if (n > 0)
{
userid = n;
bl = true;
}
dbcommand.Connection.Close();
return bl;
}

用户代码未处理 System.NullReferenceException
Message="未将对象引用设置到对象的实例。"

在 System.Windows.Forms.Control.OnValidated(EventArgs e)
在 System.Windows.Forms.Control.PerformControlValidation(Boolean bulkValidation)
在 System.Windows.Forms.ContainerControl.ValidateThroughAncestor(Control ancestorControl, Boolean preventFocusChangeOnError)


错误在这一句
int n = (int)dbcommand.ExecuteScalar();
...全文
356 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
tulu315060844 2010-04-10
  • 打赏
  • 举报
回复
谁帮帮忙啊
tulu315060844 2010-04-10
  • 打赏
  • 举报
回复
我的问题和这个差不多,但他没把最终解决方法说出来
http://topic.csdn.net/u/20090523/09/b51b6f4c-ddc3-49c2-9c1a-6ef957449f4f.html
tulu315060844 2010-04-10
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 ojlovecd 的回复:]
你确定在这句报的错?
[/Quote]
我写的判断用户名和身份证号是否注册,运行时不报错,但写完用户名和身份证号再写别的就弹出错误
定位在int n = (int)dbcommand.ExecuteScalar();这一句
我姓区不姓区 2010-04-10
  • 打赏
  • 举报
回复
你确定在这句报的错?
tulu315060844 2010-04-10
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 mayonglong 的回复:]
dbcommand.CommandText = "select userid from resume where name='" + username + "'and idcard='" + idcard + "';";
是不是多一个分号~
[/Quote]

分号无关紧要吧
tulu315060844 2010-04-10
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 conankid 的回复:]
那出错的时候会中断在那个语句上,你把dbcommand.ExecuteScalar();选中,右键-》。。。监视。。。(具体不太记得)
[/Quote]
这个是干嘛的
刚接触C#,还不清楚
mayonglong 2010-04-10
  • 打赏
  • 举报
回复
dbcommand.CommandText = "select userid from resume where name='" + username + "'and idcard='" + idcard + "';";
是不是多一个分号~
ConanKid 2010-04-10
  • 打赏
  • 举报
回复
那出错的时候会中断在那个语句上,你把dbcommand.ExecuteScalar();选中,右键-》。。。监视。。。(具体不太记得)
tulu315060844 2010-04-10
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 conankid 的回复:]
1。你的userid列是否为整型?这个应该不是问题所在
2。你那条SQL语句是否有符合的记录?如果没有dbcommand.ExecuteScalar();返回的应该是null,所以这个时候进行转换就出现了这种错误。
[/Quote]

userid是整型,sql也有相符合的记录
但总是报这错
ConanKid 2010-04-10
  • 打赏
  • 举报
回复
1。你的userid列是否为整型?这个应该不是问题所在
2。你那条SQL语句是否有符合的记录?如果没有dbcommand.ExecuteScalar();返回的应该是null,所以这个时候进行转换就出现了这种错误。

111,098

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧