Jump to content

The name "XXX" does not exist in the current context - C#

finplayer21

Hello,

 

I'm pretty n00b, so please forgive me :D So I have been working on this small project, The idea is that the program checks if the username and password are correct from a MySql database (it's a basic login form). Now I want it to check if the database has the value set to 1 or 2. The "tryLogin" bool is the one that checks if the Password and Username are correct and it works fine. As you can see I have the "Value" string declared with the "checkValue" bool, but when I use it below in the line "if (checkValue(Value) == 1)" it gives me this error: The name "Value" does not exist in the current context. How do i fix this?

        public bool tryLogin(string username, string password)        {            MySqlConnection con = new MySqlConnection("datasource=ZzZZzZZzZZ;port=3306;database=UuuUuuuuUUU;user=AaAAAaaAAa;password=xxXXXxXX!;");            MySqlCommand cmd = new MySqlCommand("Select * FROM Users WHERE Username = '" + username + "' AND password = '" + password + "';");            cmd.Connection = con;            con.Open();            MySqlDataReader reader = cmd.ExecuteReader();            if (reader.Read() != false)            {                if (reader.IsDBNull(0) == true)                {                    cmd.Connection.Close();                    reader.Dispose();                    cmd.Dispose();                    return false;                }                else                {                    cmd.Connection.Close();                    reader.Dispose();                    cmd.Dispose();                    return true;                }            }            else            {                return false;            }        }        public bool checkValue(string Value)        {            MySqlConnection con1 = new MySqlConnection("datasource=ZzZZzZZzZZ;port=3306;database=UuuUuuuuUUU;user=AaAAAaaAAa;password=xxXXXxXX!;");            MySqlCommand cmd1 = new MySqlCommand("Select * FROM Users WHERE Values = '" + Value + "';");            cmd1.Connection = con1;            con1.Open();            MySqlDataReader reader1 = cmd1.ExecuteReader();            if (reader1.Read() != false)            {                if (reader1.IsDBNull(0) == true)                {                    cmd1.Connection.Close();                    reader1.Dispose();                    cmd1.Dispose();                    return false;                }                else                {                    cmd1.Connection.Close();                    reader1.Dispose();                    cmd1.Dispose();                    return true;                }            }            else            {                return false;            }        }        private void button1_Click(object sender, EventArgs e)        {            try            {                if (tryLogin(username.Text, password.Text) == true)                {                    MessageBox.Show("Welcome to the program");                    if (checkValue(Value) == 1)                    {                        this.Hide();                        DWH f2 = new DWH();                        f2.ShowDialog();                        this.Close();                    }                    else                    {                        this.Hide();                        //Insert shit here                    }                }                else                {                    MessageBox.Show("Login details are incorrect!");                }            }            catch (Exception ex)            {                MessageBox.Show(ex.Message);            }        }
Link to comment
Share on other sites

Link to post
Share on other sites

You've declared 'Value' in the signature of the method 'checkValue(string Value)' so it is useable only within that method.  

 

Unrelated to your problem but I think you should read this

Link to comment
Share on other sites

Link to post
Share on other sites

You've declared 'Value' in the signature of the method 'checkValue(string Value)' so it is useable only within that method.  

 

If it's that why the first bool works? I've just copy pasted it and changed some of the names etc. If that's the problem how do I fix it?

Link to comment
Share on other sites

Link to post
Share on other sites

Would appear you need to understand method signatures and how to pass and use parameters.  This should help.

 

What I think you're doing by the way is passing Value instead of Value.txt is that some user input?

Link to comment
Share on other sites

Link to post
Share on other sites

I'm trying to check if a Value of the user is set to 1 or something else in the database (Here is a screenshot of the Users row in the db: http://gyazo.com/e2007f7cafc77a3f80ec9647ada3e542 ). If the Value is 1 it will open Form2 and if it's something else it opens From3. I'm not even sure if i'm doing it right...

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×