【发布时间】:2015-07-09 11:14:55
【问题描述】:
我正在为我的销售和库存添加材料。到目前为止,我需要验证添加材料时,销售价格必须大于购买价格。
这是我的代码,我在 if/else 语句中遇到错误,其中我的 txtPurchasePrice 和 txtSellingPrice 的值为十进制 (18, 2)。
protected void btnAdd_Click(object sender, EventArgs e)
{
if (txtSellingPrice.Text >= txtPurchasePrice.Text)
{
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "INSERT INTO Materials VALUES (@UnitID, @Name, @SellingPrice, @PurchasePrice, " +
"@Description, @Available, @CriticalLevel, @Maximum, @Status, @DateAdded, @DateModified)";
cmd.Parameters.AddWithValue("@UnitID", txtUnitID.Text);
cmd.Parameters.AddWithValue("@Name", txtName.Text);
cmd.Parameters.AddWithValue("@SellingPrice", txtSellingPrice.Text);
cmd.Parameters.AddWithValue("@PurchasePrice", txtPurchasePrice.Text);
cmd.Parameters.AddWithValue("@Description", txtDesc.Text);
cmd.Parameters.AddWithValue("@Available", "0");
cmd.Parameters.AddWithValue("@CriticalLevel", txtCritical.Text);
cmd.Parameters.AddWithValue("@Maximum", txtMax.Text);
cmd.Parameters.AddWithValue("@Status", "Available");
cmd.Parameters.AddWithValue("@DateAdded", DateTime.Now);
cmd.Parameters.AddWithValue("@DateModified", DBNull.Value);
cmd.ExecuteNonQuery();
con.Close();
Helper.AddLog("1", "Add", "Added a new Material");
Response.Redirect("~/Materials/Default.aspx");
}
else
{
error.Visible = true;
}
}
我的错误
CS0019:运算符“>=”不能应用于“字符串”类型的操作数 和'字符串'
为什么不能使用>=或<=或||或<或>?在这种情况下?
【问题讨论】:
-
因为这些运算符用于数字或枚举值(
||除外)。您认为一个字符串如何“大于或等于”另一个字符串?这个“更大”的主题在 IMO 的文本比较中会很复杂.. -
只是想和运营商说清楚 :) 谢谢!