0

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();
            }
        }          
    }
    
8
  • What is the variable DiscountTotal and what is its value while debugging? Commented Sep 8, 2018 at 15:33
  • I'm passing the results of Cost * (Discount / Convert.ToDecimal(100)) to that variable DiscountTotal, the value of it is 0 while debugging Commented Sep 8, 2018 at 15:38
  • and the line below: txt_quantity.Text? Commented Sep 8, 2018 at 15:47
  • Where ? which one ? Commented Sep 8, 2018 at 15:54
  • Where you write: This is where i'm getting the error at.. Commented Sep 8, 2018 at 15:59

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much. I really really appreciate it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.