大虾,我通过一个ACCESS数据库表来验证用户登录,但出现了如题的错误建了一个odb.cs的类,里面的链接语句是:
 public static OleDbConnection con()
    {
        OleDbConnection con = new OleDbConnection(ConfigurationManager.ConnectionStrings["db1"].ConnectionString);
        return con;
    }
login.aspx.cs文件中的按钮事件:
protected void Button1_Click(object sender, EventArgs e)
    {
            string auid = uid.Text.ToString();
            int buid = Convert.ToInt32(odb.src("select count(*) from [admin] where [uid]='" + auid + "'"));            string apwd = pwd.Text.ToString();
            int bpwd = Convert.ToInt32(odb.src("select count(*) from [admin] where [pwd]='" + apwd + "' and uid='" + auid + "'"));//上面已经验证过用户名,为何这里还要验证下呢?            if (buid > 0 && bpwd > 0)
            {
                Session["qx"] = odb.src("select qx from [admin] where [uid]='" + auid + "'");
                Session["uid"] = odb.src("select uid from [admin] where [uid]='" + auid + "'");
                Response.Redirect("admin.aspx");
            }
            else
            {
                if (buid <= 0)
                    uid.Text = "用户名错误";
                if (bpwd <= 0)
                    uid.Text = uid.Text + "&密码错误";            }
        
       }捕获的异常信息:
用户代码未处理 System.NullReferenceException
  Message="未将对象引用设置到对象的实例。"
  Source="App_Code.4vmkr6nf"
  StackTrace:
       在 odb.con() 位置 e:\学学的东西\Csharp作业存放\WebSite1\App_Code\odb.cs:行号 40
       在 odb.src(String que) 位置 e:\学学的东西\Csharp作业存放\WebSite1\App_Code\odb.cs:行号 78
       在 admin_login.Button1_Click(Object sender, EventArgs e) 位置 e:\学学的东西\Csharp作业存放\WebSite1\admin\login.aspx.cs:行号 21
       在 System.Web.UI.WebControls.Button.OnClick(EventArgs e)
       在 System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
       在 System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
       在 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
       在 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
       在 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

解决方案 »

  1.   

    先判断是不是为null啊
    关键代码没贴出来
      

  2.   

    odb.cs这里面的错.发出来看看.在 odb.con() 位置 e:\学学的东西\Csharp作业存放\WebSite1\App_Code\odb.cs:行号 40 
    在 odb.src(String que) 位置 e:\学学的东西\Csharp作业存放\WebSite1\App_Code\odb.cs:行号 78 40行和78行好好查检一下
      

  3.   

    这个其实是我现在在啃的一个留言本,
    oldb.cs类
    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Data.OleDb;/// <summary>
    /// odb 的摘要说明
    /// </summary>
    public class odb
    {
        public string name;
        public string email;
        public string qq;
        public string msn;
        public string url;
        public string title;
        public string concent;
        public string face;
        public string ip;
        public string pwd;
        public string uid;
        public string pwda;//这个公共变量在哪里用到地??
        public DateTime dtt;
    public odb()
    {
    //
    // TODO: 在此处添加构造函数逻辑
    //
    }
        public static OleDbConnection con()
        {
            OleDbConnection con = new OleDbConnection(ConfigurationManager.ConnectionStrings["db1"].ConnectionString);
            return con;
        }
        public static bool insert(string que)
        {
          OleDbConnection con = odb.con();
          con.Open();
          OleDbCommand cmd = new OleDbCommand(que,con);
          int count = Convert.ToInt32(cmd.ExecuteNonQuery());
          if (count > 0)
              return true;
          else
              return false;
         
        }
        public static DataTable ds(string que)//返回一个装载SQL数据的表,啥意思啊???
        {
            OleDbConnection con = odb.con();
            OleDbDataAdapter oda = new OleDbDataAdapter();
            oda.SelectCommand = new OleDbCommand(que,con);
            DataSet ds = new DataSet();
            oda.Fill(ds,"serven7");
            return ds.Tables["serven7"];
        }
        public static bool img(string que)
        {
            OleDbConnection con = odb.con();
            con.Open();
            OleDbCommand cmd = new OleDbCommand(que,con);
            if (cmd.ExecuteScalar().ToString() != "")
                return true;
            else
                return false;
            con.Close();    }
        public static string src(string que)//同上额??
        {
            OleDbConnection con = odb.con();
            con.Open();
            OleDbCommand cmd = new OleDbCommand(que,con);
            return cmd.ExecuteScalar().ToString();    }
        public static int num(string mm)
        {
            return Convert.ToInt32(odb.src("select ["+mm+"] from [config]"));
        }
    }
      

  4.   

    odb.cs这里面的错.发出来看看. 在 odb.con() 位置 e:\学学的东西\Csharp作业存放\WebSite1\App_Code\odb.cs:行号 40 
    在 odb.src(String que) 位置 e:\学学的东西\Csharp作业存放\WebSite1\App_Code\odb.cs:行号 78 40行和78行好好查检一下 
      

  5.   

    设个断点自己跟一下..看哪里为null了.
      

  6.   

    调试一下,肯定有空值了,或者用try catch捕获一下异常
      

  7.   

    刚刚我看了原版的留言本程序,
    public static string src(string que)
        { 
            OleDbConnection con = odb.con(); 
            con.Open(); 
            OleDbCommand cmd = new OleDbCommand(que,con); 
            return cmd.ExecuteScalar().ToString();     } 
    里面的que其实是一个sql语句,他没有写在oldb.cs类中,定义在了其他aspx.cs页中
      string que = "INSERT INTO [read] ([lid], [name], [title], [concent], [dtt]) VALUES ('" + lid + "','" + name + "','" + title + "','" + concent + "','" + dtt + "')";而小菜我这个sql语句还没做!!!
    是不是由这个sql语句引起的啊???
      

  8.   

    首先检查下连接的数据库名、服务器名以及密码等是否(在ConfigurationManager.ConnectionStrings中)和电脑中的数据库系统相同,大多数都是这样然后检查SQL语句是否正确
      

  9.   

    这个没取到吧,检查下web.config
    ConfigurationManager.ConnectionStrings["db1"]