Vinzcent
Vinzcent

Reputation: 1448

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

I get the following error:

Object reference not set to an instance of an object

This is my C Sharp code:

      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.";
    }

In this line, he throws the error:

newrow = tableMDF.NewRow();

Thanks a lot,

Vincent

Upvotes: 0

Views: 3158

Answers (5)

Vinzcent
Vinzcent

Reputation: 1448

Here is all my code:

  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);
  }

Upvotes: 0

Foxfire
Foxfire

Reputation: 5755

dsAcces.Tables["dsPrinterMDF"] 

is seemingly null, so make sure this delivers a DataTable. I'd guess that the identifier dsPrinterMDF doesn't exist (e.g. has a different capitalization)

Upvotes: 0

Andrew Bezzub
Andrew Bezzub

Reputation: 16032

It looks like tableMDF is null, check if there is a table dsPrinterMDF.

Upvotes: 0

Jan-Patrick Ahnen
Jan-Patrick Ahnen

Reputation: 1460

It seems that dsAcces.Tables["dsPrinterMDF"] returns null and not a table. Maybe this table is not found or you have a typo in it.

Upvotes: 0

Paolo
Paolo

Reputation: 22638

tableMDF is null then. You need to find out why dsAcces.Tables["dsPrinterMDF"] is returning null at the top.

Upvotes: 2

Related Questions