0

I'm having a difficulty in determining what is the specific problem in this code. I'm a beginner and I'm still learning about mysql and c#. How do I solve this?

else
{
    int countMemberCat = 0;
    helper.openConnection();
    string query2 = "SELECT Count (*) as 'NoOfMembershipRate' FROM membershiprate WHERE memberrate_desc = @mdesc";
    MySqlCommand cmd2 = new MySqlCommand(query2, helper.getConnection());
    cmd2.Parameters.AddWithValue("mdesc", txtMemRateDesc.Text);
    MySqlDataReader reader = cmd2.ExecuteReader();

    while (reader.Read())
    {
        countMemberCat = int.Parse(reader["NoOfMembershipRate"].ToString());

    }
    helper.CloseConnection();

    if (countMemberCat > 0)
    {
        MessageBox.Show("Membership Category already exist!");
    }
    else
    {
        try
        {
            double price = double.Parse(txtMemRate.Text);
            ClearErrorMessage();

            DialogResult dr = MessageBox.Show("Are you sure you want to add new membership category?", "Add?", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.Yes)
            {
                if (helper.openConnection())
                {
                    string query = "INSERT INTO `membershiprate` (`memberrate_desc`, memberrate_id, `memberrate_price`, memberrate_count) VALUES (@mdesc, memberrate_id, @mprice, memberrate_id)";
                    MySqlCommand cmd = new MySqlCommand(query, helper.getConnection());
                    cmd.Parameters.AddWithValue("mdesc", txtMemRateDesc.Text);
                    cmd.Parameters.AddWithValue("mprice", txtMemRate.Text);

                    cmd.ExecuteNonQuery();
                    helper.CloseConnection();


                    this.Close();
                    MessageBox.Show("Successfully added!");
                }

This is the where the problem starts. It says here that it has an unhandled mysql exception.

An unhandled exception of type 'MySql.Data.MySqlClient.MySqlException' occurred in MySql.Data.dll Additional information: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '*) as 'NoOfMembershipRate' FROM membershiprate WHERE memberrate_desc = 'Adult'' at line 1

barbsan
  • 3,418
  • 11
  • 21
  • 28

1 Answers1

0

I think problem in this below lines.

 string query = "INSERT INTO `membershiprate` (`memberrate_desc`, memberrate_id, `memberrate_price`, memberrate_count) VALUES (@mdesc, memberrate_id, @mprice, memberrate_id)";

memberrate_id and memberrate_count value not set in your insert query.

So,Please just insert two column after check.

string query = "INSERT INTO `membershiprate` (`memberrate_desc`, `memberrate_price`) VALUES (@mdesc,  @mprice)";
 MySqlCommand cmd = new MySqlCommand(query, helper.getConnection());
                            cmd.Parameters.AddWithValue("mdesc", txtMemRateDesc.Text);
                            cmd.Parameters.AddWithValue("mprice", txtMemRate.Text);

                            cmd.ExecuteNonQuery();
                            helper.CloseConnection();


                            this.Close();
                            MessageBox.Show("Successfully added!");

Please check this solution.

Hemang A
  • 1,012
  • 1
  • 5
  • 16