mark333...333...333
mark333...333...333

Reputation: 1360

C# Conditioning if the Value In The Database Column Reach the Value Zero

I'm creating if else conditional statement if the column "ending_balance" reach the value 0. So, the adding of new payment must not be insert. Here's my code.

private void btn_payment_Click(object sender, EventArgs e)

Data select = new Data();
select.Connection();
MySqlCommand cmd = new MySqlCommand("SELECT ending_balance FROM tblsalary_payments WHERE customer_id='"+customer_id+"'", select.connect);
MySqlDataReader dr = cmd.ExecuteReader();
    dr.Read();

       if(txt_payment.Text == "")
        {
            MessageBox.Show("Payment is Required");
            return;
        }
        else if (dr["ending_balance"].ToString() == "0")
        {
            MessageBox.Show("Balance Is Already Zero");
        }
        else
        {
        ... // Do the Insert Query if the Validation Passed
        }
    dr.Close();
 }

I don't know why I can still insert the query. Even though I edited the column value into zero.

Upvotes: 0

Views: 85

Answers (2)

japzdivino
japzdivino

Reputation: 1746

You can validate it directly without converting ToString.. since you are validating integers, also check the datatype of your table structure, make sure you are validating the same datatype to avoid converting integer to string.

Also, it is better to use stored procedure than creating your sql statements on the behind code to make it more secure.

Regards

Upvotes: 1

Drew
Drew

Reputation: 24960

Some ideas to debug:

put a breakpoint or dump this out dr["ending_balance"].ToString() ... perhaps the string="0.00" and not "0"... that is afterall your last line of defense.

Don't compare to a string, but compare the Value to 0.

Use msgbox() to examine pesky problems, a way to dump information (albeit a modal/blocking thing).

Upvotes: 2

Related Questions