繁体   English   中英

C#:对象引用未设置为对象的实例

[英]C#: Object reference not set to an instance of an object

我收到以下错误:

你调用的对象是空的

这是我的C Sharp代码:

      DataTable tableAcces = dsAcces.Tables["dsPrinterAcces"];
      DataTable tableMDF = dsAcces.Tables["dsPrinterMDF"];
      DataRow newrow = null;


      foreach(DataRow dr in tableAcces.Rows)
       {
           newrow = tableMDF.NewRow();

        newrow["PRINTER_ID"] = dr["PRINTER_ID"];
        newrow["MERK"] = dr["MERK"];
        newrow["MODEL"] = dr["MODEL"];
        newrow["LOKAAL_ID"] = dr["LOKAAL_ID"];

        tableMDF.Rows.Add(newrow);
      }

      daMDF.Update(dsMDF, "dsPrinterMDF");
      lblSucces.Text = "Gelukt. De tabel printers is overgezet.";
    }

在这一行中,他引发了错误:

newrow = tableMDF.NewRow();

非常感谢,

文森特

tableMDF为null。 您需要找出为什么dsAcces.Tables["dsPrinterMDF"]在顶部返回null的原因。

似乎dsAcces.Tables [“ dsPrinterMDF”]返回null而不是表。 也许找不到此表,或者您有错字。

看起来tableMDF为null,请检查是否存在表dsPrinterMDF

dsAcces.Tables["dsPrinterMDF"] 

似乎为空,因此请确保它提供了一个DataTable。 我猜标识符dsPrinterMDF不存在(例如,大小写不同)

这是我所有的代码:

  ng connStringAcces = ConfigurationManager.ConnectionStrings["RandAppAcces"].ToString();
  connAcces = new OleDbConnection(connStringAcces);


  daAcces = new OleDbDataAdapter();


  string sqlSelectAcces = "SELECT * FROM tblPrinter";
  OleDbCommand cmdAcces = new OleDbCommand(sqlSelectAcces, connAcces);
  daAcces.SelectCommand = cmdAcces;

  string connStringMDF = ConfigurationManager.ConnectionStrings["RandAppMDF"].ToString();
  connMDF = new SqlConnection(connStringMDF);

  daMDF = new SqlDataAdapter();

  string sqlSelectMDF = "SELECT * FROM tblPrinter";
  SqlCommand cmdMDF = new SqlCommand(sqlSelectMDF, connMDF);
  daMDF.SelectCommand = cmdMDF;

  string sqlInsertMDF = @"INSERT INTO tblPrinter(PRINTER_ID, MERK, MODEL, LOKAAL_ID)" +
    @"VALUES (@PRINTER_ID, @MERK, @MODEL, @LOKAAL_ID)";

  cmdMDF = new SqlCommand(sqlInsertMDF, connMDF);

  cmdMDF.Parameters.Add("@PRINTER_ID", SqlDbType.Int, 0, "PRINTER_ID");
  cmdMDF.Parameters.Add("@MERK", SqlDbType.NVarChar, 100, "MERK");
  cmdMDF.Parameters.Add("@MODEL", SqlDbType.NVarChar, 100, "MODEL");
  cmdMDF.Parameters.Add("@LOKAAL_ID", SqlDbType.Int, 0, "LOKAAL_ID");

   daMDF.InsertCommand = cmdMDF;

  dsMDF = new DataSet();
  dsAcces = new DataSet();

  daMDF.Fill(dsMDF, "dsPrinterMDF");
  daAcces.Fill(dsAcces, "dsPrinterAcces");

  DataTable tableAcces = dsAcces.Tables["dsPrinterAcces"];
  DataTable tableMDF = dsAcces.Tables["dsPrinterMDF"];
  DataRow newrow = null;


  foreach(DataRow dr in tableAcces.Rows)
   {
       newrow = tableMDF.NewRow();

    newrow["PRINTER_ID"] = dr["PRINTER_ID"];
    newrow["MERK"] = dr["MERK"];
    newrow["MODEL"] = dr["MODEL"];
    newrow["LOKAAL_ID"] = dr["LOKAAL_ID"];

    tableMDF.Rows.Add(newrow);
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM