1

I have looked at the other questions with this title and I think the problem is something local with my code that I am missing.

The function that this button preforms is to calculate the points/rewards that a person earns based on the transaction total. For example, $10 = 1 point, 19=1 point, 20=2. 10 Points = 1 Rewards points, which is equal to a ten dollar credit.

My Code receives the title error message. I will include the entire function for completeness.

    private void button1_Click(object sender, EventArgs e)
    {
        try{
            string cs = @"server=localhost;userid=root;password=root;database=dockingbay94";
            MySqlConnection conn;
            //MySqlDataReader rdr = null;
            using (conn = new MySqlConnection(cs));

            if (conn.State != ConnectionState.Open)
            {
                conn.Open();
            }
            string input = textBox2.Text;
            MySqlCommand myCommand2 = conn.CreateCommand();
            myCommand2.CommandText = "SELECT Points FROM members WHERE id = @input";
            MySqlDataAdapter MyAdapter2 = new MySqlDataAdapter();
            MyAdapter2.SelectCommand = myCommand2;


            double transaction = Convert.ToDouble(textBox3.Text);
            double tmp_transaction = Math.Floor(transaction);
            string transaction_date = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            double pointsbefore = (tmp_transaction / 10.0);
            int currentpoints = Convert.ToInt32(pointsbefore);
            int rewards = 0;
            int oldpoints = 0;
            string temp = "";
            pointsbefore = Math.Floor(pointsbefore);
            int new_points;
            double tmp_rewards = 0.0;
            double tmp_points;
            int new_rewards;

            oldpoints = (int)myCommand2.ExecuteScalar();

            new_points = currentpoints + oldpoints;
            tmp_points = new_points / 10;

            int tmp_rewards2 = 0;
            if (new_points > 10)
            {
                tmp_rewards = Math.Floor(tmp_points);
                tmp_rewards2 = Convert.ToInt32(tmp_rewards);
            }
            else if (new_points == 10)
            {
                tmp_rewards2 = 1;
            }
            else
            {
                tmp_rewards2 = 0;
            }

            new_rewards = rewards + tmp_rewards2;
            int points_left = 0;
            if (new_points > 10)
            {
                for (int i = 10; i < new_points; i++)
                {
                    points_left++;
                }
            }
            else if (new_points == 10)
            {
                points_left = 0;
            }
            else if (new_points < 10)
            {
                for (int i = 0; i < new_points; i++)
                {
                    points_left++;
                }
            }




        string query = "UPDATE members Set Points=@Points, rewards_collected=@Rewards, transaction_total=@Transaction, transaction_date=@TransactionDate" + "WHERE id = @input;";

        MySqlCommand cmdDataBase = new MySqlCommand(query, conn);
        cmdDataBase.Parameters.Add("@input", SqlDbType.Int).Value = Convert.ToInt32(textBox2.Text);
        cmdDataBase.Parameters.AddWithValue("@Points", new_points);
        cmdDataBase.Parameters.AddWithValue("@Rewards", new_rewards);
        cmdDataBase.Parameters.AddWithValue("@Transaction", textBox3.Text);
        cmdDataBase.Parameters.AddWithValue("@TransationDate", transaction_date);


        MySqlDataReader myReader2;
        myReader2 = cmdDataBase.ExecuteReader();
        MessageBox.Show("Data Updated");

    if(conn.State == ConnectionState.Open){
            conn.Close();
        }


    }
   catch(Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
}

I am not sure where the error could be. Probably not sending the right value.

Thanks

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Craig
  • 173
  • 4
  • 15

1 Answers1

2

This line is wrong

using (conn = new MySqlConnection(cs));

Remove the semicolon and include everything that needs the MySqlConnection variable inside a {} block

using (MySqlConnection conn = new MySqlConnection(cs))
{
    // No need to test if the connection is not open....
    conn.Open();
    .........

    // Not needed (at least from your code above
    // MySqlDataAdapter MyAdapter2 = new MySqlDataAdapter();
    // MyAdapter2.SelectCommand = myCommand2;

    ... calcs follow here

    // Attention here, if the query returns null (no input match) this line will throw
    oldpoints = (int)myCommand2.ExecuteScalar();

    .... other calcs here


    MySqlCommand cmdDataBase = new MySqlCommand(query, conn);
    cmdDataBase.Parameters.Add("@input", SqlDbType.Int).Value = Convert.ToInt32(textBox2.Text);
    cmdDataBase.Parameters.AddWithValue("@Points", new_points);
    cmdDataBase.Parameters.AddWithValue("@Rewards", new_rewards);
    cmdDataBase.Parameters.AddWithValue("@Transaction", textBox3.Text);
    cmdDataBase.Parameters.AddWithValue("@TransationDate", transaction_date);

    // Use ExecuteNonQuery for INSERT/UPDATE/DELETE and other DDL calla
    cmdDataBase.ExecuteNonQuery();

    // Not needed
    // MySqlDataReader myReader2;
    // myReader2 = cmdDataBase.ExecuteReader();

    // Not needed, the using block will close and dispose the connection
    if(conn.State == ConnectionState.Open)
        conn.Close();

}

There is also another error in the final query. Missing a space between @TransactionDate parameter and the WHERE clause. In cases where a long SQL command text is needed I find very useful the verbatim string line character continuation @

string query = @"UPDATE members Set Points=@Points, rewards_collected=@Rewards, 
                        transaction_total=@Transaction, transaction_date=@TransactionDate
                        WHERE id = @input;";
Steve
  • 213,761
  • 22
  • 232
  • 286
  • Still getting error. When I take suggestion on the using statement my code does not connect. I remove the code and revert back and made changes to string as you demonstrated and still get error. I will keep working on it, thanks, – Craig Jun 20 '14 at 14:41
  • Still Same Error when I revert back and change query. When I change the Connection i get this Error: "connection must be valid and Open" – Craig Jun 20 '14 at 15:01
  • On which line. You should be able to see where is the stop. On the first ExecuteScalar or in the finale ExecuteReader – Steve Jun 20 '14 at 15:48
  • I have commented the answer to clear some point that could be confusing. Hope it helps. – Steve Jun 20 '14 at 15:55
  • made the changes and cleaned it up. But still get message box with fatal error. How do I set my code or use the debugger to find what part of the code is causing the issue – Craig Jun 20 '14 at 18:55
  • Usually (if you haven't remapped the keyboard) you go to the first line of your code and press F9 to set a breakpoint, the you start the program in debug mode with the menu DEBUG -> Start Debugging. Run until you reach the point and at the breapoint hit you procede step by step using the F10 or F11 keys. – Steve Jun 20 '14 at 19:03
  • I am now getting an error at cmdDataBase.ExecuteNonQuery();, where it says there was a fatal error. Any Advice? – Craig Jun 22 '14 at 20:04
  • Fatal error? That's not all that you get from the error line. Please give all the details from that exception, in particular the message and the message on the inner exception – Steve Jun 22 '14 at 20:32
  • @Steve....I hate myself right now. When I declare AddwithValue(TransactionDate). I mispelled transaction. I am still getting used to programming in Visual Studio, as I mostly came from Unix Programming. And figuring out the debugger has annoyed me. Thanks for all your help – Craig Jun 22 '14 at 21:53