c# - Make textbox default value equals to 0 -
i have set textbox = 0
<p>credit balance: </p><asp:textbox id="txtcredit" runat="server" style="width:70px" text = "0"></asp:textbox>
and code behind this:
scn.open(); sqlcommand cmd = new sqlcommand("select creditrequest userdata username=@username", scn); cmd.parameters.add("@username", sqldbtype.nvarchar).value = session["new"]; object value = cmd.executescalar(); if (value != null) { txtcredit.text = convert.todecimal(value).tostring("#,##0.00"); }
what want happen if has value, display it. if none, default value equal 0. receiving error
object cannot cast dbnull other types.
i suspect, value coming dbnull
, guard against dbnull
using convert.isdbnull
method.
if (!convert.isdbnull(value)) { txtcredit.text = convert.todecimal(value).tostring("#,##0.00"); }
Comments
Post a Comment