3

I am getting error on this int b = Convert.ToInt32(qty.Text); if i type more than 11 numbers in quantity textbox

if (Item_Name.Text == dr["Item_Name"].ToString() && Item_Code.Text == dr["Item_Code"].ToString()) { int a = Convert.ToInt32(dr["Selling_Price"].ToString()); if (qty.Text == "") { Selling_Price.Text = ""; } else { int b = Convert.ToInt32(qty.Text); //Error On This Line int c = a * b; Selling_Price.Text = c.ToString(); } } 
2

1 Answer 1

6

You're getting that exception because an integer cannot store a number that large.

Int32.MaxValue Field : The value of this constant is 2147483647

Switching to a long should solve the problem for you. When you're dealing with numbers that represent money, you might want to consider using decimal too.

if (Item_Name.Text == dr["Item_Name"].ToString() && Item_Code.Text == dr["Item_Code"].ToString()) { int price = Convert.ToInt32(dr["Selling_Price"].ToString()); if (qty.Text == "") { Selling_Price.Text = ""; } else { long quantity = Convert.ToInt64(qty.Text); long total = price * quantity; Selling_Price.Text = total.ToString(); } } 
Sign up to request clarification or add additional context in comments.

2 Comments

If you can answer me this question as well i will be thankful to you:
I am using c# Windows Form. I have a quantity textbox if i type more then available stock for an selected item so how can i show messagebox and i want textbox.maxlength should be equal to available stock. Thanks

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.