【问题标题】:Object reference not set to an instance of an object (don't know what I should do)对象引用未设置为对象的实例(不知道该怎么办)
【发布时间】:2012-06-06 16:48:23
【问题描述】:

这是错误:

异常详细信息:System.NullReferenceException:没有对象引用 设置为对象的实例。

它停在这里:con.Open();

这里是代码:

SqlConnection con = new SqlConnection(DBHelper.connection);
    SqlCommand com = new SqlCommand();
    con = com.Connection;
    con.Open();
    com.CommandType = CommandType.Text;
    com.CommandText = "select catname,catdescription,photo from category where catid=" + catselectddl.SelectedValue ;
     SqlDataReader dr= com.ExecuteReader();
     DataTable dt = new DataTable();
     dt.Load(dr);
     DataRow drr;
     drr=dt.Rows[0];
     con.Close();

错误:

Line 19:         SqlCommand com = new SqlCommand();
Line 20:         con = com.Connection;
Line 21:         con.Open(); // here the error
Line 22:         com.CommandType = CommandType.Text;
Line 23:         com.CommandText = "select catname,catdescription,photo from category where catid=" + catselectddl.SelectedValue 

【问题讨论】:

  • 确保您的查询返回任何结果
  • 我确定是返回结果

标签: c# asp.net sql


【解决方案1】:

第三行是错误的。应该是

  com.Connection = con;

【讨论】:

    【解决方案2】:

    你需要改变这一行(com.Connectionnull 在这一点上):

    con = com.Connection;
    

    对此:

    com.Connection = con;
    

    【讨论】:

      【解决方案3】:

      您以错误的顺序分配连接。您应该将在第一行创建的连接分配给 SqlCommand,而不是将 SqlCommand 的连接(尚未创建)分配给您之前创建的 SqlConnection 变量 con。

      SqlConnection con = new SqlConnection(DBHelper.connection);
      con.Open();
      SqlCommand com = new SqlCommand();
      com.Connection = con
      

      您还应该检查您的连接状态,以确保它在执行您的命令之前成功打开。

      【讨论】:

        【解决方案4】:

        试试这个:

        SqlConnection con = new SqlConnection(DBHelper.connection);
        SqlCommand com = con.CreateCommand();
        con.Open();
        com.CommandType = CommandType.Text;
        com.CommandText = "select catname,catdescription,photo from category where catid=" + catselectddl.SelectedValue ;
         SqlDataReader dr= com.ExecuteReader();
         DataTable dt = new DataTable();
         dt.Load(dr);
         DataRow drr;
         drr=dt.Rows[0];
         con.Close();
        

        您实际上是在尝试从命令创建连接 - 需要为命令分配连接,反之亦然。

        我还建议使用我喜欢的“使用”语法,它还负责处理命令和连接。

        using (SqlConnection con = new SqlConnection(DBHelper.connection))
        {
          using(SqlCommand com = con.CreateCommand())
          {
           con.Open();
           com.CommandType = CommandType.Text;
           com.CommandText = "select catname,catdescription,photo from category where catid=" + catselectddl.SelectedValue ;
           SqlDataReader dr= com.ExecuteReader();
           DataTable dt = new DataTable();
           dt.Load(dr);
           DataRow drr;
           drr=dt.Rows[0];
           }
         }
        

        【讨论】:

          【解决方案5】:

          "select catname,catdescription,photo from category where catid=" + catselectddl.SelectedValue

          附注:
          这种类型的 SQL 脚本,如果变成一种习惯,将会打开SQL-Injection 的大门;而且我认为没有开发人员喜欢这种类型的缺陷......

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2014-07-31
            • 2019-03-20
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多