0

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

4
Contributors
8
Replies
9
Views
8 Years
Discussion Span
Last Post by snakay
0

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

0

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.

0

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 by zortec: n/a

0

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

1

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 by Geekitygeek: n/a

Votes + Comments
excellent info!
0

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.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.