c# Operator >= cannot be applied

Skythrust

Member
Jul 9, 2019
133
7
Hi there,

I am trying to make an application which is checking a number in a MySQL database. If this value is above 25 there will be a follow up action. I really can't find the solution to fix this code below.

Any advise?

C#:
MySqlConnection conn = new MySqlConnection();
            conn.ConnectionString = @"ConnString";
            MySqlCommand cmd = new MySqlCommand("SELECT Value FROM Test WHERE `Name` = 'Temperature'", conn);
            conn.Open();

            string Value;
            MySqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                Value = reader.GetString("Value");

                if(Value >= "25")
                {

                }
                MessageBox.Show(Value);
 

Damien

Don't need glasses if you can C#
Feb 26, 2012
434
647
Code:
int.TryParse(reader.GetString("Value"), out int Value);

Code:
int Value = Convert.ToInt32(test);

Code:
int Value = reader.GetInt32("Value");
 

JayC

Well-Known Member
Aug 8, 2013
5,505
1,401
Code:
int.TryParse(reader.GetString("Value"), out int Value);

Code:
int Value = Convert.ToInt32(test);

Code:
int Value = reader.GetInt32("Value");
Code:
while (reader.Read())
{
    if(int.TryParse(reader.GetString("Value"), out int val))
    {
        if(val > 25){

        }
    }
}
;)
 

JayC

Well-Known Member
Aug 8, 2013
5,505
1,401
It depends mostly on if the value will always be an integer, if so then I recommend using the "GetInt32" method.
I agree with this but I'm also wondering why the connection is using a while read function if your only expecting one row in return? You can say if(reader.Read()) and then use the reader object on the first row returned
 

Skythrust

Member
Jul 9, 2019
133
7
I agree with this but I'm also wondering why the connection is using a while read function if your only expecting one row in return? You can say if(reader.Read()) and then use the reader object on the first row returned

Thanks Jay! For now it is just object, in the future the query would be longer with more objects.
Post automatically merged:

Thanks Jay! For now it is just object, in the future the query would be longer with more objects.

I have tried this code what you had post, but he is trying to parse the value, but I would like to use this value for a follow up thing. Like now in a messagebox, but it will be trigger a mail action.

C#:
if (int.TryParse(reader.GetString("Value"), out int val))
                {
                    if (val >= 25)
                    {
                        MessageBox.Show("Value is higher than 25");
                    }
                    else if (val <= 23)
                    {
                        MessageBox.Show("Value lower than 23");
                    }
                    MessageBox.Show("Value is neutral");
                }
 
Last edited:

Users who are viewing this thread

Top