It’s Friday afternoon and I’m being driven mad by a simple .NET problem. Can you help?
I’m binding in a repeater where I know there’s a possibility that the value for a field in a dataset could be null. The field is an integer.
So in ItemDataBound I’m doing this...
Int? myInt = (int?)DataBinde r.Eval( Container.DataI tem, “myIntField”);
This comes up with an invalid cast exception, as does this...
Int myInt = (int)DataBinder .Eval( Container.DataI tem, “myIntField”);
My cop out is to do this....
String strTemp = DataBinder.Eval ( Container.DataI tem, “myIntField”, “{0}” ); // this returns a string
myInt = (strTemp != string.empty) ? Int.Parse( strTemp ) : 0; // or variations on this using Convert or Int.TryParse
There has to be a simpler way?
Sphengle
I’m binding in a repeater where I know there’s a possibility that the value for a field in a dataset could be null. The field is an integer.
So in ItemDataBound I’m doing this...
Int? myInt = (int?)DataBinde r.Eval( Container.DataI tem, “myIntField”);
This comes up with an invalid cast exception, as does this...
Int myInt = (int)DataBinder .Eval( Container.DataI tem, “myIntField”);
My cop out is to do this....
String strTemp = DataBinder.Eval ( Container.DataI tem, “myIntField”, “{0}” ); // this returns a string
myInt = (strTemp != string.empty) ? Int.Parse( strTemp ) : 0; // or variations on this using Convert or Int.TryParse
There has to be a simpler way?
Sphengle
Comment