开发者

Cannot implicitly convert from string to string[]

I am trying to c开发者_如何学Pythonopy 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!!


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?


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜