简体   繁体   中英

need help minus value from database column

I am trying to minus value in database column from textbox I got error an expression of non-boolean type specified in a context where condition is expected

  cn.Open();
  SqlCommand command = new SqlCommand();
  command.Connection = cn;

  command.CommandText = "select * from class where  quanitity - '"+Convert.ToInt32(textBox10.Text)+"'";
  command.ExecuteNonQuery();

  cn.Close();

您不是要减去column的值。您想要这样

        command.CommandText = "update class set quanitity = quanitity - "+Convert.ToInt32(textBox10.Text) ;

这肯定会工作

command.CommandText = string.Format("update class set quanitity= quanitity - {0}",Convert.ToInt32(textBox10.Text));

You need a value to compare your two numbers to. You are subtracting your TextBox10 value from quantity, but what result set do you want to see after subtracting the value? There needs to be a comparison somewhere to the right of your WHERE.

command.CommandText = "select (quantity - "+Convert.ToInt32(textBox10.Text)+") from class ;

I suspect you SQL Injection alert ..Do not Concatenate string it's wide open for sql injection alert.So use parameterized query

command.CommandText = "select * from class where  quanitity -@txtbox10";
command.Parameters.AddWithValue("@txtbox10", Convert.ToInt32(textBox10.Text));
command.ExecuteNonQuery();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM