user19064227
user19064227

Reputation: 13

Referencing Variables Between Methods

I'm new to C# so this is quite a noobie question; how would I reference variable Oint in my SQLCOMMAND method

Error: The name 'Oint' does not exist in the current context

If there are some good docs out there that go into detail on C# referencing, I would love if you could provide links.

Thank you in advance

     void OE()
        {
            String OE = comboBox1.SelectedItem.ToString();
            if (O == "Yes")
            {
                int Oint = 1;
            }
            else if (O == "No")
            {
                int Oint = 0;
            }
            else if (O == "OLO only")
            {
                int Oint = -1;
            }
        }

        void SQLCOMMAND()
        {

            try

            {
                    String u_conn_input = textBox1.Text;
                    String conn_string = @"Server=.\" + @u_conn_input + ";Database=Test1;User Id=Test;Password=Test;Timeout=5;";
                    SqlConnection con = new SqlConnection(conn_string);
                    SqlCommand command = new SqlCommand("update test1..coup set oe=" + Oint + "where [Coupon Code]=\'" + textBox2.Text + "\'", con);
                    con.Open();
                    command.ExecuteNonQuery();
                    textBox3.Text = textBox3.Text + "Coupon Updated" + Environment.NewLine;
                    MessageBox.Show(comboBox1.SelectedItem.ToString());
                
            }
            catch (SqlException)
            {
                textBox3.Text = textBox3.Text + "Coupon Update Failed" + Environment.NewLine;
            }
        }

Upvotes: 0

Views: 85

Answers (1)

Rickless
Rickless

Reputation: 1455

Error: The name 'OEint' does not exist in the current context

Because each block has its own scope, "visibility" so to speak. You could share this variable by either passing it as a parameter or by defining it in a global/bigger/containing scope.

Upvotes: 1

Related Questions