0

I am using a foreach loop to retrieve data from database and it includes both fields with text and numbers. I need to convert the fields with numbers from string to int to be able to sort the number fields properly.

I tried the following from another thread but it is not working for me:

 // Displays the value as string  
Convert.ToInt32(item.property);

// Throws an exception error "The best overloaded method match for 'int.TryParse(string out int' has some invalid arguments)"
Int32.TryParse(item.property, out PropertyInt);

Followed following:

How can I convert String to Int?

But this is not working for me in a foreach loop.

Here is my code:

@foreach (var item in db.Query(collection))
{
<tr>
<td>item.name</td>
<td>@Convert.ToInt32(item.value);</td>
<td>Int32.TryParse(item.value, out ItemValueInt);</td>
</tr>
}
Yiyi You
  • 16,875
  • 1
  • 10
  • 22
9944990
  • 444
  • 1
  • 5
  • 16
  • 6
    In what way is this "not working"? Are you getting an error? An unexpected result? Something else? Please provide a [mcve] demonstrating the problem and clarify the specific problem you are observing. – David May 25 '22 at 14:53
  • Using `int.TryParse` will do the conversion and let you know whether what you are trying to convert is an integer or not (the other two will throw exceptions if the string doesn't represent an integer). But as @David says, we really can't help you if you don't show us what your code looks like. – Flydog57 May 25 '22 at 15:01
  • Please also post the context (the foreach loop) for clarity – Wilko van der Veen May 25 '22 at 15:04
  • @David I added some more information and both examples I am trying in my foreach loop for item.value column. – 9944990 May 25 '22 at 15:17
  • 1
    @9944990: Can you add information about how this is failing? What is the specific problem that you are observing? – David May 25 '22 at 15:18
  • Generally speaking this is just bad practice. You shouldn't burden your View/Page with such code. Just retrieve whatever you need on the server side part of this code (probably some GET method) and pass to the View/Page the data in the form and order expected. – Steve May 25 '22 at 15:20
  • @David I get this error: "The best overloaded method match for 'int.TryParse(string out int' has some invalid arguments)" – 9944990 May 25 '22 at 15:21
  • @Steve It is a small project which uses Razor Pages Single Page approach. – 9944990 May 25 '22 at 15:22
  • @9944990: And what are the types for `item.value` and for `ItemValueInt`? Once again, please provide a [mcve] which demonstrates the problem. – David May 25 '22 at 15:23
  • Alright then the line is _Int32.TryParse(item.value, out int ItemValueInt)_ but this will just print if the Int32.TryParse is able to convert the value not the value converted – Steve May 25 '22 at 15:23
  • 1
    @9944990: Also, you're showing two different pieces of code, indicating that both of them represent your attempt. Which one is the actual attempt that is throwing the error? In the second code sample you also seem to have forgotten to prepend Razor code with `@`, so are those statements even executing? So far all you're showing us is text that probably kind of looks like the code you're using and a comment with an incomplete error message. Take some time to update the question with the specific code you're using and the specific error you're seeing. – David May 25 '22 at 15:26
  • The value is in ItemValueInt if conversion succeeded – Klaus Gütter May 25 '22 at 17:09

1 Answers1

0

Try to use TryParse to check if the string can be converted to int,and if true, show the ItemValueInt,here is a demo:

@{ int ItemValueInt;}
@if (Int32.TryParse(item.value, out ItemValueInt))
{
    <td>@ItemValueInt</td>
}
Yiyi You
  • 16,875
  • 1
  • 10
  • 22
  • It is throwing exception error: "The best overloaded method match for 'int.Parse(string)' has some invalid arguments" – 9944990 May 27 '22 at 11:59