Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot convert type "char" to "string" in Foreach loop

Tags:

asp.net

I have a hidden field that gets populated with a javascript array of ID's. When I try to iterate the hidden field(called "hidExhibitsIDs") it gives me an error(in the title).

this is my loop:

foreach(string exhibit in hidExhibitsIDs.Value)
        {
            comLinkExhibitToTask.Parameters.AddWithValue("@ExhibitID", exhibit);
        }

when I hover over the .value it says it is "string". But when I change the "string exhibit" to "int exhibit" it works, but gives me an internal error(not important right now).

like image 459
user1084319 Avatar asked Dec 01 '25 02:12

user1084319


1 Answers

You need to convert string to string array to using in for loop to get strings not characters as your loop suggests. Assuming comma is delimiter character in the hidden field, hidden field value will be converted to string array by split.

foreach(string exhibit in hidExhibitsIDs.Value.Split(','))
{
     comLinkExhibitToTask.Parameters.AddWithValue("@ExhibitID", exhibit);
}
like image 157
Adil Avatar answered Dec 02 '25 17:12

Adil