string str = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("test.mdb");
OleDbConnection con = new OleDbConnection(str);
string sql = "select * from Users where name='123'"+" and psw='222'";
OleDbCommand cmd = new OleDbCommand(sql, con);
con.Open();
int num =(int)cmd.ExecuteScalar();
con.Close();
“/test”应用程序中的服务器错误。
--------------------------------------------------------------------------------未将对象引用设置到对象的实例。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误:
行 33: OleDbCommand cmd = new OleDbCommand(sql, con);
行 34: con.Open();
行 35: int num =(int)cmd.ExecuteScalar();行 36: con.Close();
行 37: if (num > 0)
源文件: d:\My Documents\Visual Studio 2005\WebSites\test\zhuce.aspx.cs 行: 35 堆栈跟踪:
[NullReferenceException: 未将对象引用设置到对象的实例。]
_Default.tijiao_Click(Object sender, EventArgs e) in d:\My Documents\Visual Studio 2005\WebSites\test\zhuce.aspx.cs:35
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +75
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +98
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +4919
OleDbConnection con = new OleDbConnection(str);
string sql = "select * from Users where name='123'"+" and psw='222'";
OleDbCommand cmd = new OleDbCommand(sql, con);
con.Open();
int num =(int)cmd.ExecuteScalar();
con.Close();
“/test”应用程序中的服务器错误。
--------------------------------------------------------------------------------未将对象引用设置到对象的实例。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误:
行 33: OleDbCommand cmd = new OleDbCommand(sql, con);
行 34: con.Open();
行 35: int num =(int)cmd.ExecuteScalar();行 36: con.Close();
行 37: if (num > 0)
源文件: d:\My Documents\Visual Studio 2005\WebSites\test\zhuce.aspx.cs 行: 35 堆栈跟踪:
[NullReferenceException: 未将对象引用设置到对象的实例。]
_Default.tijiao_Click(Object sender, EventArgs e) in d:\My Documents\Visual Studio 2005\WebSites\test\zhuce.aspx.cs:35
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +75
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +98
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +4919
解决方案 »
- 今天自学看书遇到的问题,请高手们进来帮助!!
- 求解:大哥们帮帮忙,弹框问题
- 发送一串16进制给服务器,服务器会反馈同样的一串16进制
- 在C#程序中,怎么使用sql语句向数据库的一列中插入空值,但是该列也可能有值,这样的语句应该怎么写。
- C#实现 基于UDP的接收多个端口发来的数据??
- C# WinForm中怎么设置窗体不可以拉大和拖小,是设置窗体的什么属性啊?
- 高手请进,调试问题。(很难)
- 世界上最难的SQL语句,搜虎的面试题,谁会啊。。。。!!!!!!!!!!!!!!!!!!!!!!!!
- winform中datagridvoew添加列的问题
- 简单而容易忽视的问题:new SqlParameter(ParameterName, xxx); 中的ParameterName需要带@吗?
- 随机函数问题。 求帮忙。。
- 什么是代码折叠 为什么要折叠代码
这里返回的是一个数据集合,不是一个int应该用ExecuteDataReader,获取改用OleDbDataAdapter来填充DataSet或者DataTable
在查询分析器里先执行一下sql,看是否有值
要使用Server.MapPath("~/pathxxx/test.mdb");这样相当网站根目录的形式,
你原来是使用当前默认的目录,这样页面的目录不同,test.mdb相对位置也不同。
你确定你的查询SQL
select * from Users where name='123'"+" and psw='222'"这个能查到数据吗?如果没有查询数据 cmd.ExecuteScalar() (返回执行结果第一行第一列的数据)这个返回的肯定是NULL
将NULL强转成INT 肯定出错!祝你好运!
{
//你执行的代码
}
catch(System.NullReferenceException e)
{
//专抓空引用异常
}