Hi,

I'm having exception on this line, when some of the textboxes do not have value in

if (Convert.ToInt32(LeaveDay.Text) + Convert.ToInt32(SickDay.Text) + Convert.ToInt32(WorkDay.Text) > limitDay)

appreciate any help
thanks
snky

Recommended Answers

All 8 Replies

You should check if the textboxes are null and look at using try/catch to process exceptions.

Check beforehand if your textboxes are not empty.
if (LeaveDay.Text != string.Empty) etc.

That is a long way. What I'm asking is if there is any shorter or smarter way of doing that just on the "if" statement. If we are talking about a big project with many textboxes checking try/catch with each field is very cumbersome. For instance I tried "??" as coalesce but did not help. So I'm looking for a short cut method.

This is probably the best way to do the check:

if (string.IsNullOrEmpty(LeaveDay.Text))

You can also write a function that does validation for a textbox.

it seems there is no way of escaping from preparing the "if" statement with many elements in arith. op. So you havet to check textboxes with other"if"s before coming to the final "if" where you execute your actual test. Too much code !! So back to square one...
thank you all

Please remember to mark the thread as solved. Thank you

it is only slightly less cumbersome, but you can use inline If statements:

Convert.ToInt32((string.IsNullOrEmpty(LeaveDay.Text)) ? "0" : LeaveDay.Text)

If LeaveDay.Text is empty or null it will use "0", otherwise it uses LeaveDay.Text.

Or possibly try using

int Day;
int.TryParse(LeaveDay.Text, out Day);

if tryparse suceeds it will place the converted value in Day, if it fails, Day will equal zero.

commented: excellent info! +5

well thanks Ryshad, it works. As you said it is less cumbersome and at least one line

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.