Risho
Risho

Reputation: 2647

Nullable (int?) keep throwing non-nullable exception

This is a first that I've encountered this - of course that does't sais a lot, but never the less I need to consult with the collective as my searches while showed that it can be done, I'm not able to work it out.

The issue is that if user leaves the text box empty C# automagically enters zero on the postback and into the database as well. I just want the text box left blank.

I have a class there the property is declared:

private int? _PageNumber = null;

public int? PageNumber
{
    get { return _PageNumber; }
    set { _PageNumber = value; }
}

The in my code file I have:

if (tbPageNumber.Text != "")
        ia.PageNumber = Convert.ToInt32(tbPageNumber.Text);
    else
        ia.PageNumber = null;

and this is where I get the exception:

CS0037: Cannot convert null to 'int' because it is a non-nullable value type

So I thought if I can't change the type, perhaps I can change the output. So I thought perhaps I can fake it by placing this in the texbox's text property:

'<%# Eval("PageNumber") == 0 ? null:Eval("PageNumber") %>'

This throws CS0019: Operator '==' cannot be applied to operands of type 'object' and 'int' exception.

Why is so damn stubborn???

Upvotes: 3

Views: 3981

Answers (6)

Risho
Risho

Reputation: 2647

Okay people, you gonna kill me for this.

The issue was that the class in which the ia.PageNumber is declared in a separate project in the solution and for some reason it did't compile, hence the comments about compiler-time problems.

This is a first such solution with multiple projects that I've worked with and I had no reason to think to compile individual projects (I use Visual Studio 2010).

Well, lesson learned. Many thanks to all who contributed.

Upvotes: 2

Silvermind
Silvermind

Reputation: 5944

I made an extension method for this which helps me out a lot with these web-related string conversions in case of value types:

public static class ExtensionMethods
{
    public static T As<T>(this string input, T fallback = default(T), 
                          IFormatProvider provider = null)
    {
        var type = Nullable.GetUnderlyingType(typeof(T));
        if (type == null)
            type = typeof(T);
        T result;
        try
        {
            result = (T)Convert.ChangeType(input, type, provider);
        }
        catch(Exception)
        {
            result = fallback;
        }
        return result;
    }
}

And use it like this:

ia.PageNumber = tbPageNumber.Text.As<int>();
ia.PageNumber = tbPageNumber.Text.As<int?>();
ia.PageNumber = tbPageNumber.Text.As<int?>(10);

Upvotes: 3

Mike Perrenoud
Mike Perrenoud

Reputation: 67918

To be very straight forward, there is no way that ia.PageNumber is referring to the definition you provided because the following code not only compiles, and runs, but writes out Setting to NULL.:

private int? _PageNumber = null;

public int? PageNumber
{
    get { return _PageNumber; }
    set { _PageNumber = value; }
}

if (string.Empty != "")
{
    PageNumber = Convert.ToInt32(string.Empty);
    Console.WriteLine("Setting to value.");
}
else
{
    PageNumber = null;
    Console.WriteLine("Setting to NULL.");
}

the bottom line is that ia.PageNumber isn't int? or _PageNumber isn't an int?. To further concrete my position, the following code:

PageNumber = Convert.ToInt32(" ");
Console.WriteLine("Setting to value {0}.", PageNumber);

throws the expected exception:

Input string was not in a correct format.

so the problem can't be a padded string either. And to finally concrete my position, the following code:

PageNumber = Convert.ToInt32("");
Console.WriteLine("Setting to value {0}.", PageNumber);

throws the expected exception:

Input string was not in a correct format.

so the problem can't be an empty string either. And to further concrete this position based on the comment made by jadarnel27 that you might be converting null, that actually succeeds as expected, the following code:

PageNumber = Convert.ToInt32(null);
Console.WriteLine("Setting to value {0}.", PageNumber);

actually, compiles, runs, and throws no execption. It outputs the string Setting to value 0. as expected.

Upvotes: 5

Jason P
Jason P

Reputation: 27022

Try this:

ia.PageNumber = (int?)null;

Edit: I was thinking of a different issue.. The error indicates that ia.PageNumber is an int, not an int?, despite the sample code provided.

Upvotes: 0

user1193035
user1193035

Reputation:

CS0019: Operator '==' cannot be applied to operands of type 'object' and 'int'

Try this 0 to "0"

'<%# Eval("PageNumber") == "0" ? 0:Eval("PageNumber") %>'

Cannot convert null to 'int' because it is a non-nullable value type

Don't use null in pagenumber .Because the pagenumber property get only integer values.But null is not a integer. So you get this above error.But now I changed from null to 0

Upvotes: 0

Ash
Ash

Reputation: 41

I would try the following:

if (!string.IsNullOrEmpty(tbPageNumber.Text))
    ia.PageNumber = Convert.ToInt32(tbPageNumber.Text);
else
    ia.PageNumber = null;

An empty string and a null string are two different things, therefore a string with null value could be slipping through your code.

Upvotes: 4

Related Questions