DataRow constructor inaccessible when writing DataSet extension?
Asked Answered
W

2

6

I am trying to write a couple of extensions to convert UniDataSets and UniRecords to DataSet and DataRow but I get the following error when I try to compile.

'System.Data.DataRow.DataRow(System.Data.DataRowBuilder)' is inaccessible due to its protection level

Is there any way to fix this or should I abandon this approach and come at it a different way?

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Data;
    using IBMU2.UODOTNET;

    namespace Extentions
    {
        public static class UniDataExtentions
        {
            public static System.Data.DataSet ImportUniDataSet(this System.Data.DataSet dataSet, IBMU2.UODOTNET.UniDataSet uniDataSet)
            {
                foreach (UniRecord uniRecord in uniDataSet)
                {
                    DataRow dataRow = new DataRow();
                    dataRow.ImportUniRecord(uniRecord);
                    dataSet.Tables[0].ImportRow(dataRow);
                }

                return dataSet;
            }

            public static void ImportUniRecord(this System.Data.DataRow dataRow, IBMU2.UODOTNET.UniRecord uniRecord)
            {
                int fieldCount = uniRecord.Record.Dcount();

                // ADD COLUMS
                dataRow.Table.Columns.AddRange(new DataColumn[fieldCount]);

                // ADD ROW
                for (int x = 1; x < fieldCount; x++)
                {
                    string stringValue = uniRecord.Record.Extract(x).StringValue;
                    dataRow[x] = stringValue;
                }
            }
        }
    }
Wiedmann answered 22/4, 2013 at 19:54 Comment(2)
I don't see where you're trying to use that constructor anywhere in this code? What line is the error occurring on?Teahouse
Line 17: DataRow dataRow = new DataRow();Wiedmann
S
18

It doesn't matter whether it's in an extension method, or any method. The DataRow constructor is not publicly accessible. You need to use the DataTable.NewRow() method to create a new DataRow.

It will use the schema information from the data table to create a row that matches it. If you just tried to use the constructor on it's own the object would have no idea what schema should be used.

Schnabel answered 22/4, 2013 at 20:0 Comment(0)
T
0

I tried a simpler approach, however it is for multiple rows and can be applied to a single row as well:

//Declare a variable for multiple rows 
DataRow[] rows = null;

//get some data in a DataTable named table

//Select specific data from DataTable named table
rows = table.Select("column = 'ColumnValue'");

//Read the value in a variable from the row
string ColumnValue = rows[0]["column"].ToString();

hope this helps...

Twelfth answered 24/1, 2014 at 17:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.