c# - FormatException when trying to write string to database -
c# - FormatException when trying to write string to database -
i writting little web application , want add together details database using web form. using asp.net , c# create web application , visual studio 2013 developing tool. using barcode scanner scan barcode product. because can not include screenshot user interface, have 3 labels, 3 textboxes (barcode,description,price), add together button , have created database save details. 3 colums in database barcode
, description
, price
, info types varchar(50)
, varchar(50)
, decimal(10,2)
. when run page , scan barcode exception:
system.formatexception: input string not in right format.
i have tried without barcode scanner , set barcode keyboard , working fine.
here more details exception:
server error in '/' application.input string not in right format. description: unhandled exception occurred during execution of current web request. please review stack
trace more info error , originated in code.
exception details: system.formatexception: input string not in right format.
source error:
line 31: con.open(); line 32: line 33: da.insertcommand.executenonquery(); line 34: line 35: con.close();
source file: path.aspx.cs line: 33
stack trace:
[formatexception: input string not in right format.] system.number.stringtonumber(string str, numberstyles options, numberbuffer& number, numberformatinfo info, boolean
parsedecimal) +11177559 system.number.parsedecimal(string value, numberstyles options, numberformatinfo numfmt) +172 system.convert.todecimal(string value, iformatprovider provider) +67 system.string.system.iconvertible.todecimal(iformatprovider provider) +10 system.convert.changetype(object value, type conversiontype, iformatprovider provider) +11043185 system.data.sqlclient.sqlparameter.coercevalue(object value, metatype destinationtype, boolean& coercedtodatafeed,
boolean& typechanged, boolean allowstreaming) +5332823
[formatexception: failed convert parameter value string decimal.] system.data.sqlclient.sqlparameter.coercevalue(object value, metatype destinationtype, boolean& coercedtodatafeed,
boolean& typechanged, boolean allowstreaming) +5331815 system.data.sqlclient.sqlparameter.getcoercedvalue() +185 system.data.sqlclient.sqlparameter.validate(int32 index, boolean iscommandproc) +102 system.data.sqlclient.sqlcommand.buildparamlist(tdsparser parser, sqlparametercollection parameters) +201 system.data.sqlclient.sqlcommand.buildexecutesql(commandbehavior behavior, string commandtext, sqlparametercollection
parameters, _sqlrpc& rpc) +237 system.data.sqlclient.sqlcommand.runexecutereadertds(commandbehavior cmdbehavior, runbehavior runbehavior, boolean
returnstream, boolean async, int32 timeout, task& task, boolean asyncwrite, sqldatareader ds) +1421 system.data.sqlclient.sqlcommand.runexecutereader(commandbehavior cmdbehavior, runbehavior runbehavior, boolean
returnstream, string method, taskcompletionsource1 completion, int32 timeout, task& task, boolean asyncwrite) +177 system.data.sqlclient.sqlcommand.internalexecutenonquery(taskcompletionsource
1 completion, string methodname, boolean
sendtopipe, int32 timeout, boolean asyncwrite) +208 system.data.sqlclient.sqlcommand.executenonquery() +163 possystem.stock.addstock.btnadd_click(object sender, eventargs e) in path\add.aspx.cs:33 system.web.ui.webcontrols.button.onclick(eventargs e) +9615682 system.web.ui.webcontrols.button.raisepostbackevent(string eventargument) +103 system.web.ui.webcontrols.button.system.web.ui.ipostbackeventhandler.raisepostbackevent(string eventargument) +10 system.web.ui.page.raisepostbackevent(ipostbackeventhandler sourcecontrol, string eventargument) +13 system.web.ui.page.raisepostbackevent(namevaluecollection postdata) +35 system.web.ui.page.processrequestmain(boolean includestagesbeforeasyncpoint, boolean includestagesafterasyncpoint)
+1724
any 1 has thought why , how can prepare please?
here code when button clicked:
protected void btnadd_click(object sender, eventargs e) { int y; da.insertcommand = new sqlcommand("insert products values(@barcode,@description,@price)", con); da.insertcommand.parameters.add("@barcode", sqldbtype.varchar).value = txtbarcode.text; da.insertcommand.parameters.add("@description", sqldbtype.varchar).value = txtdescription.text; da.insertcommand.parameters.add("@price", sqldbtype.decimal).value = txtpprice.text; con.open(); da.insertcommand.executenonquery(); con.close(); }
this line...
da.insertcommand.parameters.add("@price", sqldbtype.decimal).value = txtpprice.text;
seems problem. trying save text decimal field. work if value of string convertible decimal sql driver on occasion failing. maybe because cost string contains currency formatting (e.g. £, $).
try parsing decimal first
c# sql asp.net barcode
Comments
Post a Comment