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

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

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.

Edited 7 Years Ago by zortec: n/a

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

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.

Edited 7 Years Ago by Geekitygeek: n/a

Comments
excellent info!

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

This question has already been answered. Start a new discussion instead.