Reputation: 59
In the code below, I'm trying to validate the Textbox (txt_quantity and txt_discount)
But instead of getting this MessageBox.Show("Cannot be empty");
I'm getting the error of
('Input string was not in a correct format.')
Did i forgot something here?
txt_discount(DECIMAL)
decimal Discount, DiscountTotal, Discountgiven, Total;
int Cost, Quantity, ID;
byte[] data;
public void Imagedisplay()
{
using (var con = SQLConnection.GetConnection())
{
using (var selects = new SqlCommand("Select * from employee_product where Codeitem =@Codeitem ", con))
{
selects.Parameters.Add("@Codeitem", SqlDbType.VarChar).Value = _view.txt_code.Text;
using (var reader = selects.ExecuteReader())
{
while (reader.Read())
{
data = (byte[])reader["Image"];
Cost = Convert.ToInt32(reader["Unitcost"]);
Convert.ToInt32(DiscountTotal);
// This is where i'm getting the error at
Quantity = Convert.ToInt32(txt_quantity.Text);
Discount = Convert.ToDecimal(txt_discount.Text); //
Discountgiven = Cost * (Discount / Convert.ToDecimal(100));
DiscountTotal = Cost - Discountgiven;
Total = DiscountTotal * Quantity;
}
}
}
}
}
private void btn_ok_Click(object sender, EventArgs e)
{
Imagedisplay();
using (var con = SQLConnection.GetConnection())
{
if (string.IsNullOrEmpty(txt_quantity.Text) || string.IsNullOrEmpty(txt_discount.Text))
{
MessageBox.Show("Cannot be empty");
}
else
{
using (var command = new SqlCommand("Insert into product_result (Date, Image, Code, Name, Price, Discount, Quantity, Total) Values (@Date, @Image, @Code, @Name, @Price, @Discount, @Quantity, @Total)", con))
{
command.Parameters.Add("@Date", SqlDbType.VarChar).Value = date;
command.Parameters.Add("@Image", SqlDbType.VarBinary).Value = data;
command.Parameters.Add("@Code", SqlDbType.VarChar).Value = _view.txt_code.Text.Trim();
command.Parameters.Add("@Name", SqlDbType.VarChar).Value = _view.txt_name.Text.Trim();
command.Parameters.Add("@Price", SqlDbType.VarChar).Value = _view.txt_price.Text;
command.Parameters.Add("@Discount", SqlDbType.Decimal).Value = txt_discount.Text.Trim();
command.Parameters.Add("@Quantity", SqlDbType.Int).Value = txt_quantity.Text.Trim();
command.Parameters.Add("@Total", SqlDbType.Decimal).Value = Total;
command.ExecuteNonQuery();
Totals();
}
using (var selects = new SqlCommand("Update employee_product set quantity = quantity - @Quantity where Codeitem= @Codeitem", con))
{
selects.Parameters.Add("@Codeitem", SqlDbType.VarChar).Value = _view.txt_code.Text;
selects.Parameters.Add("@Quantity", SqlDbType.Int).Value = txt_quantity.Text;
selects.ExecuteNonQuery();
this.Close();
}
_view.Display();
}
}
}
Upvotes: 0
Views: 2521
Reputation: 1538
Use a NumericUpDown instead of a textbox to capture integer/decimal values. It handles all the validation for you stopping users from entering non-numeric values. You can set the maximum and minimum values you require and you don't have to worry about no values being entered as the NumericUpDown will always have a default value.
If you're using integers then you just need to cast to an int when you retrieve the value, otherwise it returns a decimal. So your code would be:
Quantity = Convert.ToInt32(numericupdown1.Value);
Discount = numericupdown2.Value;
If you're hellbent on using textboxes then you need to remove whitespace with .Trim()
Quantity = Convert.ToInt32(txt_quantity.Text.Trim());
And use int.TryParse
instead;
int value = 0;
if (int.TryParse(txt_quantity.Text.Trim(), out value)
{
// Successful conversion so value now contains your integer
}
You can do the same with decimals.
Upvotes: 2