0

I am trying to copy values in string array into a DataRow, it throws an error:

Cannot implicitly convert from string to string[]

The code:

DataRow dr = null;
ddcontent[i] = strfinalstartweek[i] + " - " + strfinalendweek[i] + "-- $" + openingbid;
// ddcontent is the string array
for (int i = 0; i < 12; i++)
{
    dr.ItemArray = ddcontent[i];
    ListItem item = new ListItem();
    item.Text = NullHandler.NullHandlerForString(dr["OpeningBid"], string.Empty);
    ddweek.Items.Add(item);
}

What do you guys think is wrong in here.. tried lot of ways trying.

Thanks in advance!!

8
  • 4
    Needs more context - What type is ddcontent? What type is dr? Commented Dec 29, 2010 at 15:25
  • The exception usually contains a reference to the line in your source code that is causing the problem. It would be of great help if you could highlight the line that generates the error. Commented Dec 29, 2010 at 15:25
  • On which line do you get the error. The stackTrace will help. Commented Dec 29, 2010 at 15:26
  • Can you add the types for the different items? It seems to me from the error message that dr is of type String[][]. Commented Dec 29, 2010 at 15:26
  • I'm going to guess that ddcontent[i] is returning a string and trying to assign a value to dr.ItemArray, which is a string array. Not enough context though. Commented Dec 29, 2010 at 15:27

3 Answers 3

2
dr.ItemArray = ddcontent[i];

ItemArray is of type object[] so that line won't work. A string can't be converted to an array of objects. Based on the text of the error, however, I wonder if there is another type mismatch in the code as well. Can you show which line contains the error?

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

7 Comments

dr.ItemArray = ddcontent[i]; this line contains the error. Thank you
@Ram: Chances are (and this is based on the limited code we're seeing, so you'll want to confirm the logic yourself), what you probably want to do is: dr.ItemArray[i] = ddcontent[i];.
i am sorry David, I am trying to save the string array values into the datarow. Then, show it in the dropdown. This is the aim. When i changed it to dr.ItemArray[i] = ddcontent[i];there is no error but has an exception "Object reference not set to an instance of an object".
@Ram: Oops, I missed the fact that the DataRow is null here. It's not initialized and therefore has no ItemArray property. Why are you using a DataRow? It's being declared here, and you attempt to populate it here, but what is its intended use? And what is its data structure that you're trying to populate? Why not just create the ListItems from the string array?
@David: List Items? New concept that i should see.. I am new for asp.net. Learning it.. I will see what List Item does.
|
0

Is ddcontent an array of strings (the data rows)? If you want to chop each row up to fit into a data row, you have to use the string.Split method

Comments

0

Based on the information provided it seems that ddcontent[i] is a string and you are trying to assign it to a property - ItemArray - that expects an a array. To fix this you need to either:

Move this line outside your loop and assign the ddcontent array dirctly to the ItemArray. But note that you ddcontent has to have the SAME number of items as columns in your DataRow.

   dr.ItemArray = ddcontent;
   for (int i = 0; i < 12; i++)
   {
      ListItem item = new ListItem();
      item.Text = NullHandler.NullHandlerForString(dr["OpeningBid"], string.Empty);
      ddweek.Items.Add(item);
   }

Or in the loop assign the items using the Item indexer

   for (int i = 0; i < 12; i++)
   {
      dr.Item[i] = ddcontent[i];
      ListItem item = new ListItem();
      item.Text = NullHandler.NullHandlerForString(dr["OpeningBid"], string.Empty);
      ddweek.Items.Add(item);
   }

Also, I am assuming that you are creating a new DataRow instance somewhere in your code after DataRow dr = null and before using the dr i.e. there is a line like this somewhere:

dr = dt.NewRow(); 

where dt is your datatable.

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.