Click here to Skip to main content
15,890,408 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I use update query to update the payment
SQL
SqlCommand cmd = new SqlCommand(UPDATE Bank SET Payment = (Payment - '"+tprice.Text +"') WHERE NICNO = '" + FN.Text + "'", con);

the following error will occur
Operand data type nvarchar is invalid for subtract operator.
Posted

The error means what it says. You're putting the contents of the price textbox in quotes, which makes it a string. Also, your code will blow up if a non number is entered, and if someone is smart, they can use your code to erase your entire DB, due to SQL injection.
 
Share this answer
 
add value reference of tprice
cmd.parameter.addwithvalue("@payment", tprice.text)
 
Share this answer
 
My wild guess is this is what you are trying to do -
SQL
SqlCommand cmd = new SqlCommand("UPDATE Bank SET Payment = '(Payment - "+tprice.Text +")' WHERE NICNO = '" + FN.Text + "'", con);


see if it helps.
 
Share this answer
 
v2
Hi,

Try this if could help...

C#
string sqlCmdText = "UPDATE Bank  SET Payment = (Payment - " + tprice.Text + ") WHERE NICNO = '" + FN.Text + "'";
SqlConnection conn = new SqlConnection("YourWebConfigConnectionStringID");
using (SqlCommand cmd = new SqlCommand(sqlCmdText, conn))
{
    cmd.CommandType = CommandType.Text;
    try
    {
        conn.Open();
        cmd.ExecuteNonQuery();
    }
    catch (SqlException)
    {
        // You want to catch your exception here?...
    }
    finally
    {
        cmd.Dispose();
        conn.Dispose();
        // No need to close cmd and conn will automatically close by using...
    }
 }


Please do not forget to vote if could help...

Happy coding...
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900