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 

解决方案 »

  1.   

    string sql = "select * from Users where name='123'"+" and psw='222'"; 
    这里返回的是一个数据集合,不是一个int应该用ExecuteDataReader,获取改用OleDbDataAdapter来填充DataSet或者DataTable
      

  2.   

    你确定取出值了吗,先判断取出来的是否为空
    在查询分析器里先执行一下sql,看是否有值
      

  3.   

    string sql = "select count(*) from Users where name='123'"+" and psw='222'";
      

  4.   

    string str = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("test.mdb"); Server.MapPath("test.mdb"); //这里有问题,test.mdb放在那里?
    要使用Server.MapPath("~/pathxxx/test.mdb");这样相当网站根目录的形式,
    你原来是使用当前默认的目录,这样页面的目录不同,test.mdb相对位置也不同。
      

  5.   

    行 35:        int num =(int)cmd.ExecuteScalar();行 36:        con.Close(); 
    你确定你的查询SQL
    select * from Users where name='123'"+" and psw='222'"这个能查到数据吗?如果没有查询数据 cmd.ExecuteScalar() (返回执行结果第一行第一列的数据)这个返回的肯定是NULL
    将NULL强转成INT 肯定出错!祝你好运!
      

  6.   

    try
    {
     //你执行的代码
    }
    catch(System.NullReferenceException e)
    {
     //专抓空引用异常
    }