0

I have a dropdownlist that is populated by an SQL database. I am trying to compare the selected values from the SQL dB and it is not working. Below is what I have so far.

if (dropdownlist.SelectedValue >= "5" && dropdownlist.SelectedValue <= "13")
{
  //do something
}

Thank you in advance for any help.

jsheeran
  • 2,912
  • 2
  • 17
  • 32
Rob
  • 3
  • 1
  • What language is this? And when you say "not working", do you get an error message or an incorrect result? My instinct is that `"5"` and `"13"` should be `5` and `13` but without knowing the language it's impossible to say. – jsheeran Jun 08 '22 at 13:41
  • C#, visual Studio throws a red line under the dropdownlist.SelectedValue >= "5" && dropdownlist.SelectedValue <= "13". I changed the "5" to 5 and same for the 13, and get the same red line. – Rob Jun 08 '22 at 14:14
  • Hovering over the Red line in VS it says, "Operator '<=' cannot be applied to operands of type 'string' and 'int'." – Rob Jun 08 '22 at 14:17
  • That would be where you're going wrong then. Have a read about [converting a string to an int](https://stackoverflow.com/questions/1019793/how-can-i-convert-string-to-int). – jsheeran Jun 08 '22 at 14:20
  • Thank you jsheeran. That did it. I added int ddl = Int32.Parse(dropdownlist.SelectedValue); then referrenced the ddl in the if statement. if (ddl >= 5 && ddl <= 13) Thank you again! – Rob Jun 08 '22 at 14:38

0 Answers0