asp.net - Usercontrol int property bind to a null value from database -


i have build usercontrol in asp.net, , there property following. works fine when bound value integer. however, if bound field return null database, return invalid cast error.

change nullable int not desirable because changes how programmer work control's property in code-behind.

just wonder how these things should implemented? thanks,

[defaultvalue(0)] public int fixedlo {         {         if (viewstate["fixedlo"] != null)             return convert.toint32(viewstate["fixedlo"]);         else             return 0;     }     set     {         if (value == null)             viewstate["fixedlo"] = 0;         else              viewstate["fixedlo"] = value;     } } 

would checking dbnull trick?

[defaultvalue(0)] public int fixedlo {         {         if (viewstate["fixedlo"] != null)             return convert.toint32(viewstate["fixedlo"]);         else             return 0;     }     set     {         if (value == null || value dbnull)             viewstate["fixedlo"] = 0;         else              viewstate["fixedlo"] = value;     } } 

Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -