M A Hossan M A Hossan - 3 months ago 6
ASP.NET (C#) Question

c#. Do not get expected result

I am currently doing one project implementation but i am facing some problem. What i am trying to achieve from this if the Account Balance is positive the if command will be executed and it should display the message that you have enough balance otherwise if should go to else statement and should display message you do not have enough balance but every time i am running the application and entering the necessary information it always goes to else statement and displaying you do not enough balance. the information i am submitting its comes form textbox in windows from application. My database column name is statementamount. Here is code in c#..If any one if help me to solve this problem . I will be grateful

private void button3_Click(object sender, EventArgs e)
{

try
{

SqlConnection cn = new SqlConnection(@"Data Source=KHUNDOKARNIRJOR\KHUNDOKERNIRJOR;Initial Catalog=Login;Integrated Security=True");
SqlCommand cmd = new SqlCommand("SELECT (case when statementamount>0 then 1 else 0 end), cast(statementamount as nvarchar(MAX)) FROM ACCOUNT WHERE Account_Number ='" + textBox1.Text + "'and statementamount='" + textBox3.Text + "'", cn);
cmd.Parameters.AddWithValue("@Account_Number", textBox1.Text);
cmd.Parameters.AddWithValue("@statementamount", textBox3.Text);
cn.Open();
string value = cmd.ExecuteScalar() as string;


if (textBox3.Text == value)
{
MessageBox.Show("You have enough balance!");
}
else
{
MessageBox.Show("You no enough balance!");
}


}
catch (SqlException ex)
{
MessageBox.Show(" Not Possivle" + ex);


}
}
}


}
AND here is out put .....
enter image description here

and database information.
enter image description here

Answer

I Edited whole code as below,

SqlCommand cmd = new SqlCommand("SELECT statementamount FROM  Accounts WHERE Account_Number =@Account_Number", cn);
cmd.Parameters.AddWithValue("@Account_Number", textBox1.Text);
cn.Open();
var value = cmd.ExecuteScalar();


            if (Convert.ToDouble(textBox3.Text)  <= Convert.ToDouble(value))
            {
                MessageBox.Show("You have  enough balance!");
            }
            else
            {
                MessageBox.Show("You no enough balance!");
            }
Comments