Click here to Skip to main content
15,881,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a ListBoxFor where i need to retrieve all the selected values. The selected values are in a string array of the form :
string commaDelimited = "mihr20, emp1004, fbc567,mihr01";.
Now when i attempt to separate the individual items between the commas i get type-casting exception 'Cannot convert Char to int'. I need to extract the values to :

mihr20 emp1004 fbc567 mihr01

In the controller i have :

C#
request.EmployeeNumber = new string[] { Convert.ToString(formcollection["EmployeeNumber"]) };
 string[] employees =request.EmployeeNumber.Split(new char[] { ',' });
if (ModelState.IsValid)
                {
                    foreach (string emp in employees)
                    {
					...save the selected employees
					}
					
					}



And the model as
C#
[Required]
       [Display(Name = "Employee ")]
       public string[] EmployeeNumber { get; set; }
       public Employee Employee { get; set; }
       public String DisplayName { get; set; }
       public IEnumerable<SelectListItem> employees { get; set; }


What I have tried:

I tried to as below but in the database it saves a single value System.String[] instead of all the values

C#
var employeees = request.EmployeeNumber.ToString();
 string[] employees =employeees.Split(new char[] { ',' });
Posted
Updated 2-Apr-22 13:30pm

This looks like the same issue as in your previous question at How do I resolve error 'cannot implicitly convert type 'string' to string[][^]. You need to understand the difference between a string, which is a single string of characters, and a string[], which is an array of string objects.
As to your problem here, you have not explained which line of code produces the error message above.
 
Share this answer
 
Seem to be some other reason for this error because I got the same error with the following code:

C#
foreach (string filePath in DirectoryBrowser.SelectedPath)
{
    //.....do something with the path
}


filePath is declared as a string and DirectoryBrowser.SelectedPath returns a string, so I don't see how the char datatype is involved.
 
Share this answer
 
Comments
Dave Kreskowiak 3-Apr-22 0:53am    
Don't post your questions as a solution to an old question. Go to the "Quick answers" menu and click "Ask a question".

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900