Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moving from string array into datarow

Tags:

c#

asp.net

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);
like image 702
challengeAccepted Avatar asked Dec 22 '10 13:12

challengeAccepted


1 Answers

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.

like image 51
TalentTuner Avatar answered Sep 26 '22 02:09

TalentTuner