Search code examples
c#datatable

Unable to cast object of type 'MS.Internal.NamedObject' to type 'System.Data.DataRowView'


I have this code to retrieve data from a datagrid and then store then in another datatable. But I am getting the exception as: "Unable to cast object of type 'MS.Internal.NamedObject' to type 'System.Data.DataRowView'." This exception comes after "dt.Rows.Add(dr.ItemArray)"

Please do let me know if there are any faults, or any additional information is needed.

Code:

 DataTable dt = new DataTable();
         DataColumn c1 = new DataColumn("IsActive", typeof(bool));
         dt.Columns.Add(c1);

         DataColumn c2 = new DataColumn("DataGridTextBox_QCList1", typeof(string));
         c2.MaxLength = 500;
         dt.Columns.Add(c2);

         DataColumn c3 = new DataColumn("DataGridTextBox_QCSummary", typeof(string));
         c3.MaxLength = 500;
         dt.Columns.Add(c3);

         DataColumn c4 = new DataColumn("DataGridComboxBox_Control", typeof(string));
         c4.MaxLength = 500;
         dt.Columns.Add(c4);  

     foreach (DataRowView d in dtgQCNumbers.Items)
     {
    DataRow dr = d.Row;
    dt.Rows.Add(dr.ItemArray);
    dt.AcceptChanges();
     }
     foreach (DataRow row in dt.Rows)
     {
    qcId = row["QC_ID"].ToString();
    zipFolderPath = baseFolderPath;
    patternFiles = Directory.GetFiles(zipFolderPath, "*.zip");

    logMessage = "Unzipping file from path" + zipFolderPath + " \n file name:" + patternFiles[0];
    Common.LogMessage(logMessage);

    UnZipReleasePatch(zipFolderPath, patternFiles.First());
    //deploy release patch.
    logMessage = "Deploying files" + zipFolderPath;
    Common.LogMessage(logMessage);
    DeployFiles();
}

Solution

  • Thanks to AVD, got my answer as follows:

    foreach (DataRow d in _ds.Tables[0].Rows)
             {
                 if (d["DataGridComboxBox_Control"].ToString() == "AVS_DB")
                 {
                     qcId = d["DataGridTextBox_QCList1"].ToString();
                     zipFolderPath = baseFolderPath;
                     patternFiles = Directory.GetFiles(zipFolderPath, "*.zip");
    
                     logMessage = "Unzipping file from path" + zipFolderPath + " \n file name:" + patternFiles[0];
                     Common.LogMessage(logMessage);
    
                     UnZipReleasePatch(zipFolderPath, patternFiles.First());
                     logMessage = "Deploying files" + zipFolderPath;
                     Common.LogMessage(logMessage);
                     DeployFiles();
                 }
             }