Well the question title may not be self explanatory, so let me go ahead and elaborate.
Consider, a TextBox that accepts only numeric value or is left empty. The value(text) entered is stored in an integer(int32) variable. The problem arises when the user enters the digit 0 or leaves the TextBox empty, as the conversion from string to int, converts an empty string to “0” as well.
So my question stands: How do I differentiate the 2 scenarios?
EDIT I figured a lot of questions may be answered by the code and exact problem(as I see it)
if (txtOtherId.Text == string.Empty)
{
otherId = Convert.ToInt32(null);
}
else
{
otherId = Convert.ToInt32(txtOtherId.Text);
}
What have you tried? Can we see your code?
Now, I tried the following:
So I can’t reproduce. However, if I use
nullinstead of"", theConvert.ToInt32does convert into zero (0). However,ParseandTryParsestill fail withnull.UPDATE:
Now that I see your code. Consider changing the type of
otherIdfrominttoint?where the question mark makes it a nullable type. Then:If you want to be sure no exceptions can happen, do this: