Resolved How to solve this Error: CS0019 Operator '==' cannot be applied to operands of type 'method group' and 'bool'?

Omer Butt

Active member
Joined
Sep 6, 2021
Messages
26
Programming Experience
Beginner
Please Tell me if the Line 13 `if (Txt_AddUser_UserName_TextChanged(sender, e) == true)` is now a write way then how to write it.

Add User and Check for User Exist:
//This part to add a user in database as a signup in Line 13 in the condition of if I tried to create a logic if Txt_AddUser_UserName_TextChanged(object sender, EventArgs e) this is true only than it sign up otherwise throws an error. Butt In this Line I came across this error, CS0019    Operator '==' cannot be applied to operands of type 'method group' and 'bool'
private void Btn_SignUp_Click(object sender, EventArgs e)
        {
            String Role = Combo_UserRole.Text;
            String Name = Txt_AddUser_Name.Text;
            String DOB = DatePicker_AddUser_DOB.Text;
            Int64 Mobile = Int64.Parse(Txt_AddUser_MobileNo.Text);
            String Email = Txt_AddUser_Email.Text;
            String UserName = Txt_AddUser_UserName.Text;
            String Password = Txt_AddUser_Password.Text;
            try
            {
                if (Txt_AddUser_UserName_TextChanged(sender, e) == true) //Error is in this Line 
                {
                    Query = "insert into Users_Table (User_Role,User_Name,DOB,User_Mobile,User_Email,User_Username,User_Password) values('" + Role + "','" + Name + "','" + DOB + "','" + Mobile + "','" + Email + "','" + UserName + "','" + Password + "')";
                    SqlCommand cmd = new SqlCommand(Query, Conn.Connect);
                    Conn.OpenConnection();
                    int t = cmd.ExecuteNonQuery();
                    Conn.CloseConnection();
                    if (t > 0)
                    {
                        MessageBox.Show("Data Inserted Successfully!", "Success! Data Inserted", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        MessageBox.Show("Data is not Inserted! Try enter the data correctly", "Error! Data Not Inserted", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            catch (Exception msg)
            {
                MessageBox.Show(msg.Message);
            }
        }

public void Txt_AddUser_UserName_TextChanged(object sender, EventArgs e)
        {
           
            Query2 = "select * from Users_Table where User_Username='" + Txt_AddUser_UserName.Text + "'";

            SqlDataAdapter QryCmd = new SqlDataAdapter(Query2, Conn.Connect);
            DataTable dt = new DataTable();
            QryCmd.Fill(dt);
            if (dt.Rows.Count == 0)
            {
                PictureBox_Username_tick_cross.ImageLocation = @"C:\Users\faroo\OneDrive\Desktop\Graphin8-POS\Images\yes.png"; //Tick Image to show Username is Available
            }
            else
            {
                PictureBox_Username_tick_cross.ImageLocation = @"C:\Users\faroo\OneDrive\Desktop\Graphin8-POS\Images\no.png"; // Cross Image to Show Username already taken and is in the Database
            }
        }
 
Solution
What I Sound like...
It's kinda for me to tell you what you sound like because I'm the one who's reading what you're typing. What you intend it to sound like and what it actually sounds like aren't necessarily the same thing, as evidenced by your first three posts. Thankfully, you did provide a decent description in your fourth post and now you've gone and repeated what I said was my understanding of what you wanted. I have already provided you with the solution. First you wouldn't provide the explanation and now you won't stop providing it. I have shown you how to get a value of type bool that indicates whether the user name entered is new or not. You can then use that value to make decisions. I've shown you how to do...

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
4,130
Location
Sydney, Australia
Programming Experience
10+
Maybe if you were actually explain what you're trying to achieve there, rather than expecting us to work it out ourselves from code that doesn't actually achieve it. Is it too much to expect that you actually explain your problem, rather than writing as few words as possible?

The line you highlighted makes no sense. Txt_AddUser_UserName_TextChanged is, by the look of it, an event handler, which handles the TextChanged event of a TextBox named Txt_AddUser_UserName. It's return type is void. Why exactly would you expect that to be equal to true or to anything else for that matter?
 

Omer Butt

Active member
Joined
Sep 6, 2021
Messages
26
Programming Experience
Beginner
Maybe if you were actually explain what you're trying to achieve there, rather than expecting us to work it out ourselves from code that doesn't actually achieve it. Is it too much to expect that you actually explain your problem, rather than writing as few words as possible?

The line you highlighted makes no sense. Txt_AddUser_UserName_TextChanged is, by the look of it, an event handler, which handles the TextChanged event of a TextBox named Txt_AddUser_UserName. It's return type is void. Why exactly would you expect that to be equal to true or to anything else for that matter?
BTW I explained in comment part of the code, but as asked, Here I wanted to Signup a user only when in this Txt_AddUser_UserName_TextChanged this Event in line 36 is true then the Btn_SignUp_Click Event executes otherwise throws an error.
 

Omer Butt

Active member
Joined
Sep 6, 2021
Messages
26
Programming Experience
Beginner
BTW I explained in comment part of the code, but as asked, Here I wanted to Signup a user only when in this Txt_AddUser_UserName_TextChanged this Event in line 36 is true then the Btn_SignUp_Click Event executes otherwise throws an error.
Maybe if you were actually explain what you're trying to achieve there, rather than expecting us to work it out ourselves from code that doesn't actually achieve it. Is it too much to expect that you actually explain your problem, rather than writing as few words as possible?

The line you highlighted makes no sense. Txt_AddUser_UserName_TextChanged is, by the look of it, an event handler, which handles the TextChanged event of a TextBox named Txt_AddUser_UserName. It's return type is void. Why exactly would you expect that to be equal to true or to anything else for that matter?
I accept that I am doing it wrong that why I am on the forum to seek help in getting the idea on how to do that, What I am doing wrong as being a student and in learning stage I seek help of professionals to improve my skills.
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
4,130
Location
Sydney, Australia
Programming Experience
10+
BTW I explained in comment part of the code, but as asked, Here I wanted to Signup a user only when in this Txt_AddUser_UserName_TextChanged this Event in line 36 is true then the Btn_SignUp_Click Event executes otherwise throws an error.
Firstly, don't explain your problem in code comments. Code comments are for commenting the code. Explain your problem in the post.

Secondly, that isn't an explanation anyway because, as I have already explained, this:
Txt_AddUser_UserName_TextChanged this Event in line 36 is true
is nonsense. That's like saying that you only want to lie down if your leg is true. Please explain what you're actually trying to achieve. Are you saying, without actually saying, that you want to perform an action only if the user has entered text into a particular TextBox? If so, say that. If not, say what you are trying to do.
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
4,130
Location
Sydney, Australia
Programming Experience
10+
I accept that I am doing it wrong that why I am on the forum to seek help in getting the idea on how to do that, What I am doing wrong as being a student and in learning stage I seek help of professionals to improve my skills.
The issue is not that you're doing it wrong so much as you're doing it wrong and not making any attempt to explain what it is that you're doing. We shouldn't have to work out what problem we 're trying to solve as well as solve it. You should be doing everything in your power to provide a FULL and CLEAR explanation of the problem, not dumping your code and assuming that we will immediately see what the purpose is and the correct way to achieve that purpose. That code is so nonsensical that there's no indication of what it is supposed to actually do, so how can we tell you how to do it? If you'd like us to volunteer our time to help you, the least you can do is take the time to ensure we can do that without undue effort. We're here because we want to help but we generally don't want to be taken advantage of and that's what it feels like when people try to get away with doing as little as possible to help us help them. All I'm asking for is for you to explain your problem, which isn't really something that we should have to ask for.
 

Omer Butt

Active member
Joined
Sep 6, 2021
Messages
26
Programming Experience
Beginner
The issue is not that you're doing it wrong so much as you're doing it wrong and not making any attempt to explain what it is that you're doing. We shouldn't have to work out what problem we 're trying to solve as well as solve it. You should be doing everything in your power to provide a FULL and CLEAR explanation of the problem, not dumping your code and assuming that we will immediately see what the purpose is and the correct way to achieve that purpose. That code is so nonsensical that there's no indication of what it is supposed to actually do, so how can we tell you how to do it? If you'd like us to volunteer our time to help you, the least you can do is take the time to ensure we can do that without undue effort. We're here because we want to help but we generally don't want to be taken advantage of and that's what it feels like when people try to get away with doing as little as possible to help us help them. All I'm asking for is for you to explain your problem, which isn't really something that we should have to ask for.
Ok I Got it I explain the scenario, There is a Form named Add User, and that add user form is connected to a database, Where after filling all the field comprises of combo date time picker and textboxes, When the user click on a signup button named Btn_SignUp It need to first see if the username textbox named Txt_AddUser_UserName is checked if the Username already exist in the database. If it is already in the database It will not signup and shows an error if it is a new username that a user typed in Txt_AddUser_UserName only then the user will be succesfully signed up. for that vary reason I created a TextChanged Event in which I shown Tick or Cross Image as a validation kind of thing that shows if the User already exists or not in the database. Then for the Btn_SignUp I created a click event Btn_SignUp_Click where I tried to say if the Txt_AddUser_UserName_TextChanged is true then run the signup code.

problem.jpg

problem1.jpg


Below is a Part where is just a simple it will insert a data in to database.

C#:
private void Btn_SignUp_Click(object sender, EventArgs e)
        {
            String Role = Combo_UserRole.Text;
            String Name = Txt_AddUser_Name.Text;
            String DOB = DatePicker_AddUser_DOB.Text;
            Int64 Mobile = Int64.Parse(Txt_AddUser_MobileNo.Text);
            String Email = Txt_AddUser_Email.Text;
            String UserName = Txt_AddUser_UserName.Text;
            String Password = Txt_AddUser_Password.Text;
            try
            {
              
                    Query = "insert into Users_Table (User_Role,User_Name,DOB,User_Mobile,User_Email,User_Username,User_Password) values('" + Role + "','" + Name + "','" + DOB + "','" + Mobile + "','" + Email + "','" + UserName + "','" + Password + "')";
                    SqlCommand cmd = new SqlCommand(Query, Conn.Connect);
                    Conn.OpenConnection();
                    int t = cmd.ExecuteNonQuery();
                    Conn.CloseConnection();
                    if (t > 0)
                    {
                        MessageBox.Show("Data Inserted Successfully!", "Success! Data Inserted", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        MessageBox.Show("Data is not Inserted! Try enter the data correctly", "Error! Data Not Inserted", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
              
            }
            catch (Exception msg)
            {
                MessageBox.Show(msg.Message);
            }
        }

Then this part below is for checking if the username is already in the database or not and on that basis it will show a tick name yes.png and cross names no.pnd in a picture box right beside the Username textbox

C#:
public void Txt_AddUser_UserName_TextChanged(object sender, EventArgs e)
        {
        
            Query2 = "select * from Users_Table where User_Username='" + Txt_AddUser_UserName.Text + "'";

            SqlDataAdapter QryCmd = new SqlDataAdapter(Query2, Conn.Connect);
            DataTable dt = new DataTable();
            QryCmd.Fill(dt);
            if (dt.Rows.Count == 0)
            {
                PictureBox_Username_tick_cross.ImageLocation = @"C:\Users\faroo\OneDrive\Desktop\Graphin8-POS\Images\yes.png"; //Tick Image to show Username is Available
            }
            else
            {
                PictureBox_Username_tick_cross.ImageLocation = @"C:\Users\faroo\OneDrive\Desktop\Graphin8-POS\Images\no.png"; // Cross Image to Show Username already taken and is in the Database
            }
        }

Now I wanted the above sign button click event Btn_SignUp_Click to be only run when the Username Textbox TextChanged Event says the username is available and shows green tick as shown in the picture above. for that I made that nonsense mistake which I asked and seek help for how to do it was this below: to add a if condition if (Txt_AddUser_UserName_TextChanged(sender, e) == true) in signup button click event Btn_SignUp_Click part of code.

C#:
private void Btn_SignUp_Click(object sender, EventArgs e)
        {
            String Role = Combo_UserRole.Text;
            String Name = Txt_AddUser_Name.Text;
            String DOB = DatePicker_AddUser_DOB.Text;
            Int64 Mobile = Int64.Parse(Txt_AddUser_MobileNo.Text);
            String Email = Txt_AddUser_Email.Text;
            String UserName = Txt_AddUser_UserName.Text;
            String Password = Txt_AddUser_Password.Text;
            try
            {
                if (Txt_AddUser_UserName_TextChanged(sender, e) == true)
                {
                    Query = "insert into Users_Table (User_Role,User_Name,DOB,User_Mobile,User_Email,User_Username,User_Password) values('" + Role + "','" + Name + "','" + DOB + "','" + Mobile + "','" + Email + "','" + UserName + "','" + Password + "')";
                    SqlCommand cmd = new SqlCommand(Query, Conn.Connect);
                    Conn.OpenConnection();
                    int t = cmd.ExecuteNonQuery();
                    Conn.CloseConnection();
                    if (t > 0)
                    {
                        MessageBox.Show("Data Inserted Successfully!", "Success! Data Inserted", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        MessageBox.Show("Data is not Inserted! Try enter the data correctly", "Error! Data Not Inserted", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            catch (Exception msg)
            {
                MessageBox.Show(msg.Message);
            }
        }
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
4,130
Location
Sydney, Australia
Programming Experience
10+
It sounds like what you're saying is that what you care about is whether or not the Text of a particular TextBox contains an existing user name or not. In that case, declare a bool that represents that:
C#:
private bool isUserNameNew = false;
You can then set that flag in your TextChanged event handler:
C#:
var sql = "SELECT COUNT(*) FROM Users_Table WHERE User_Name = @User_Name";

using (var connection = new SqlConnection(connectionString))
using (var command = new SqlCommand(sql, connection))
{
    command.Parameters.Add("@User_Name", SqlDbType.VarChar, 50).Value = Txt_AddUser_UserName.Text;
    connection.Open();
    
    isUserNameNew = ((int)command.ExecuteScalar()) == 0;
}
and test it in your Click event handler:
C#:
if (isUserNameNew)
{
    // Create new user here.
}
Again, an event handler can't "be true". Only a Boolean expression can be true, which means a field or property of type bool, a method with a return type of bool or some expression that evaluates to type bool. A void method can't BE anything. It just DOES something.
 

Omer Butt

Active member
Joined
Sep 6, 2021
Messages
26
Programming Experience
Beginner
It sounds like what you're saying is that what you care about is whether or not the Text of a particular TextBox contains an existing user name or not. In that case, declare a bool that represents that:
C#:
private bool isUserNameNew = false;
You can then set that flag in your TextChanged event handler:
C#:
var sql = "SELECT COUNT(*) FROM Users_Table WHERE User_Name = @User_Name";

using (var connection = new SqlConnection(connectionString))
using (var command = new SqlCommand(sql, connection))
{
    command.Parameters.Add("@User_Name", SqlDbType.VarChar, 50).Value = Txt_AddUser_UserName.Text;
    connection.Open();
   
    isUserNameNew = ((int)command.ExecuteScalar()) == 0;
}
and test it in your Click event handler:
C#:
if (isUserNameNew)
{
    // Create new user here.
}
Again, an event handler can't "be true". Only a Boolean expression can be true, which means a field or property of type bool, a method with a return type of bool or some expression that evaluates to type bool. A void method can't BE anything. It just DOES something.
What I Sound like that I Care about what user type in username textbox, Signup depends on Username textbox if the user typed a username is already in the database then clicking on a signup button will gives error and will not insert form data to database. What user will type defines the two images green tick and cross. If user types a username that already exists it will show red cross which means change username and if user types a new username that is not already exists in a database it will show a green tick and it will make a successful entry upon clicking a signup button.
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
4,130
Location
Sydney, Australia
Programming Experience
10+
What I Sound like...
It's kinda for me to tell you what you sound like because I'm the one who's reading what you're typing. What you intend it to sound like and what it actually sounds like aren't necessarily the same thing, as evidenced by your first three posts. Thankfully, you did provide a decent description in your fourth post and now you've gone and repeated what I said was my understanding of what you wanted. I have already provided you with the solution. First you wouldn't provide the explanation and now you won't stop providing it. I have shown you how to get a value of type bool that indicates whether the user name entered is new or not. You can then use that value to make decisions. I've shown you how to do that with an if statement. You can also use an if...else to decide which of two things to do, e.g. which of two images to display. Of course, that can be more streamlined:
C#:
PictureBox_Username_tick_cross.ImageLocation = Path.Combine(@"C:\Users\faroo\OneDrive\Desktop\Graphin8-POS\Images",
                                                            isUserNameNew
                                                                ? "yes.png"
                                                                : "no.png");
 
Solution

Omer Butt

Active member
Joined
Sep 6, 2021
Messages
26
Programming Experience
Beginner
It's kinda for me to tell you what you sound like because I'm the one who's reading what you're typing. What you intend it to sound like and what it actually sounds like aren't necessarily the same thing, as evidenced by your first three posts. Thankfully, you did provide a decent description in your fourth post and now you've gone and repeated what I said was my understanding of what you wanted. I have already provided you with the solution. First you wouldn't provide the explanation and now you won't stop providing it. I have shown you how to get a value of type bool that indicates whether the user name entered is new or not. You can then use that value to make decisions. I've shown you how to do that with an if statement. You can also use an if...else to decide which of two things to do, e.g. which of two images to display. Of course, that can be more streamlined:
C#:
PictureBox_Username_tick_cross.ImageLocation = Path.Combine(@"C:\Users\faroo\OneDrive\Desktop\Graphin8-POS\Images",
                                                            isUserNameNew
                                                                ? "yes.png"
                                                                : "no.png");
Thank you Sir and sorry as being first time posting on a forum as a student didn't knew much but today I learned next time when I will post a thread it will be well explained at a very first Thank you for your Patience Regards
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
3,762
Location
Chesapeake, VA
Programming Experience
10+
Top Bottom