Moving from an array of strings to datarow

I have a problem with assigning an array of strings in Datarow. First, I created an object for a string array and put 2 values ​​in an array of 100 (full size). How many values ​​should be placed in the dependencies of the array from another, which I do not show here.

Then I tried to convert to DataRow. But that says. "The reference to the object is not installed in the instance of the object"

DataRow dr = null;
string[] strconcat = new string[100];
dr["concat"] = strconcat[i];

Thanks in advance

Change Actually, I tried to put these string array values ​​in a drop-down menu (ddchooseadeal). Is there any other good way besides this.

 locname = ddchoosealoc.SelectedValue.ToString();
            string[] strdeals = new string[100];
            string[] strconcat = new string[100];
            int i;
            for(i =0; i< dsdeal.Tables[0].Rows.Count; i++)
            {
               strdeals[i] = Convert.ToString( dsdeal.Tables[0].Rows[i]["Title"]);
               strconcat[i] = strdeals[i]+" -- "+ locname;
            }
               DataRow dr = null;
               ddchooseadeal.Items.Clear();
               ListItem li = new ListItem("Choose a Deal");
               ddchooseadeal.Items.Add(li);


               dr["drconcat"] = strconcat[0];
               ListItem item = new ListItem();
               item.Text = NullHandler.NullHandlerForString(strconcat[i], string.Empty);
               ddchoosealoc.Items.Add(item);
+4
source share
6 answers

DataRow DataTable, , DataRow.

, ,

// Declare a DataTable object.
DataTable dt = new DataTable();

// Add some columns to the DataTable
dt.Columns.Add("StringHolder");

// Now suppose , you are having 10 items in your string array
foreach(string str in strArray)
{
    DataRow drow = dt.NewRow() ;   // Here you will get an actual instance of a DataRow
    drow ["StringHolder"] = str;   // Assign values 
    dt.Rows.Add(drow);             // Don't forget to add the row to the DataTable.             
}

, , DataTable, .

+10

DataRow dr = null;, "object reference not set to an instance of an object".

DataTable, DataRow. , , MSDN :

:

+1

100 , . , , . .

, , -. (0 int, false bool ..), (, ) - null.

, dr null.

+1

, :

var x = dr["concat"];

, :

var y = strconcat[i];

datarow , . datatable . datarow.

. msdn: datatable = > http://msdn.microsoft.com/en-us/library/5ycd1034%28VS.80%29.aspx

0

NullPointerException, dr null, dataRow DataTable.NewRow

0

Saurabh, DataTable DataRow.

DataRow, params object[] values. , DataRow , :

// Create the data table
var dataTable = new DataTable("TableName");

// Add the columns you will need
dataTable.Columns.Add("FirstName");
dataTable.Columns.Add("LastName");
dataTable.Columns.Add("Whatever");

// Get your data in string array format
// Will need to be FirstName, LastName, Whatever
string[] data = LoadStringArrayFromCsvOrSomething();

// Add the DataRow using the string array
dataTable.Rows.Add(data);

This works great in conjunction with Microsoft.VisualBasic.FileIO.TextFieldParserand System.Data.SqlClient.SqlBulkCopy. You can transfer data to SQL Server as if nobody was working.

0
source

Source: https://habr.com/ru/post/1781656/


All Articles