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_quantity(INTEGER)
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(); } } }