1
string[] weight = txtProductOptn1.Text.Split(',');
var Weight1 = Convert.ToInt32(weight);

Getting an error when i am converting the string array to int or decimal as

Unable to cast object of type 'System.String[]' to type 'System.IConvertible'.

Please find the answer.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Haritha
  • 11
  • 1
  • 1
  • 4

3 Answers3

6

Try this, weight is an array. Assuming you want to convert first element of array.

string[] weight = txtProductOptn1.Text.Split(',');
var Weight1 = Convert.ToInt32(weight[0]);
Satpal
  • 132,252
  • 13
  • 159
  • 168
4

You cannot convert a string[] to an int. Perhaps you want to convert every single string to an int:

IEnumerable<int> ints = weight.Select(int.Parse);

Note that this will fail if one of the strings is not parsable to int because it has an invalid format.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
1

You can't convert an array of string (string[]) to an integer (int). Using System.Linq, you can try:

int[] convertedWeight = txtProductOptn1.Text.Split(',')
                                            .Select(x => Convert.ToInt32(x))
                                            .ToArray();

to convert each element to an integer.

I found a similar post here : Convert string to int array using LINQ

Timothy G.
  • 6,335
  • 7
  • 30
  • 46
VinceAnity
  • 56
  • 5