2010-04-27 43 views
0

我碰到下面的錯誤的實例:C#:對象引用未設置到對象

Object reference not set to an instance of an object

這是我的C夏普代碼:

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

非常感謝,

Vincent

+0

是tableMDF空? – 2010-04-27 08:12:03

回答

2

tableMDF爲空。你需要找出爲什麼dsAcces.Tables["dsPrinterMDF"]在頂部返回null。

+0

這是一張空桌子,也許是這個問題? – Vinzcent 2010-04-27 08:13:43

+0

這是可能的,但我希望該設置包含零行的表格對象。你確定你的表名是否正確? – Paolo 2010-04-27 08:43:56

+0

我發現了這個錯誤。這是一個錯字。 This:DataTable tableMDF = dsAcces.Tables [「dsPrinterMDF」]; - > DataTable tableMDF = dsMDF.Tables [「dsPrinterMDF」]; – Vinzcent 2010-04-27 09:22:49

0

似乎dsAcces.Tables [「dsPrinterMDF」]返回null而不是表。也許這張表沒有找到,或者你有一個錯字。

0

看起來像tableMDF爲空,檢查是否有表dsPrinterMDF

0
dsAcces.Tables["dsPrinterMDF"] 

看起來是空的,所以確保它提供了一個DataTable。我猜標識符dsPrinterMDF不存在(例如具有不同的大小寫)

0

這裏是我的所有代碼:

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

@Vinzcent - 爲了將來的參考,這不是一個傳統的論壇 - 你可以編輯你的問題(和你的答案),所以你需要做的就是將上述問題放在原始問題中。 – Murph 2010-04-27 08:21:13

相關問題