5

I have a problem with assigning string array into Datarow. Firstly, i have created the object for string array and put 2 values in the array out of 100(whole size). How many values should be placed in the array dependds on a different, which i am not showing here, though.

Then i tried converting into DataRow. But it says. "object reference not set to an instance of an object"

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

Thanks in advance

Edit-- Actually i was trying put these string array values into dropdown (ddchooseadeal). Is there any other good way other than 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);
0

6 Answers 6

11

Your DataRow is not a part of any DataTable which is actually right, that's why you can't instantiate a direct DataRow object.

So that's the theory part, to solve your problem

// 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.             
}

So by following the above steps, you will have a DataTable populated with rows.

Sign up to request clarification or add additional context in comments.

Comments

1

Your string array here has 100 elements, all null. So if you assign one of the elements into your data row, you are assigning null. Not a string.

If you are creating an array, the elements will remain uninitialised until you populate it with something. Value types will have their default value (0 for int, false for bool, etc.) while reference types (like string) default to null.

Also, dr is set to null in your example.

Comments

1

You are attempting to add values to a variable that is null DataRow dr = null; which is why you are getting the "object reference not set to an instance of an object" error.

You need to create a new datarow using your DataTable object, then adding the values to that DataRow. Without seeing more of the code it would be hard to offer much more help, but the following article from MSDN will get you started:

How to: Add Rows to a DataTable

Comments

0

check with your debugger if your desired column exists:

var x = dr["concat"];

and check if your desired value exists in the string array with:

var y = strconcat[i];

your datarow above is initialized with null so the error message is absolutely plausible. you have to design a datatable with the columns you want. after that get a new row from that table and save the values from your string array to the datarow.

see msdn: how to add rows to a datatable => http://msdn.microsoft.com/en-us/library/5ycd1034%28VS.80%29.aspx

Comments

0

It throws NullPointerException because dr is null, you have to create dataRow with DataTable.NewRow Method

Comments

0

Building off Saurabh's answer, you can build a DataTable and it's columns and add each DataRow.

But there is a constructor for the DataRow that takes a params object[] values. So you can automatically add a whole string array to a DataRow, provided that the order of the elements matches the order of the columns specified:

// 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 the Microsoft.VisualBasic.FileIO.TextFieldParser and the System.Data.SqlClient.SqlBulkCopy. You can throw data into SQL Server like its nobody's business.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.