I'm working on my Add Materials for my Sales and Inventory. So far I need a validation where when adding a material, Selling Price must be greater than Purchase Price.
Here is my code and I'm getting an error in the if/else statement where the values of my txtPurchasePrice and txtSellingPrice are decimal(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;
}
}
My Error
CS0019: Operator '>=' cannot be applied to operands of type 'string' and 'string'
Why is that >=
or <=
or ||
or <
or >
is cannot be used? in this case?
I'm assuming txtSellingPrice
and txtPurchasePrice
are TextBox
controls, hence their Text
property is of type string
. You can't use >=
since that has no semantical meaning. You'll need to parse the text first into decimal
:
var sellingPrice = decimal.Parse(txtSellingPrice.Text);
var purchasePrice = decimal.Parse(txtPurchasePrice.Text);
if (sellingPrice >= purchasePrice)
{
// stuff
}
If you're not sure the text is a valid decimal
value, use decimal.TryParse
:
decimal sellingPrice;
if (!decimal.TryParse(txtSellingPrice.Text, out sellingPrice))
{
// Not a valid decimal, do something.
}
decimal purchasePrice;
if (!decimal.TryParse(txtPurchasePrice.Text, out purchasePrice))
{
// Not a valid decimal, do something.
}
if (sellingPrice >= purchasePrice)
{
// stuff
}
Hwo can a string be greater than another string?
You need to convert the string to a numeric value before you do the comparison.
Since you are dealing with money, you should use Decimal
:
if (Decimal.Parse(txtSellingPrice.Text) >= Decimal.Parse(txtPurchasePrice.Text))
要比较两个字符串的数值,您必须将它们解析为数值类型,例如double!
Use Convert.ToDecimal(txtSellingPrice.Text) >= Convert.ToDecimal(txtPurchasePrice.Text)
String value cannot be compared like that
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.