vb.net - Error when saving from format(val(textbox.text), "#,##0") to numeric field type in SQL -
in sql server, have table this:
tableitem
------------------------
item | varchar (15),
price | numeric(18, 2)
in vb.net, have code:
textbox1.text = "book" textbox2.text = 20.000,00 textbox2.text = format(val(textbox2.text), "#,##0.00")
procedure save :
dim sqlquery string = "insert tableitem(item,price) values('" & textbox1.text & "','" & textbox2.text & "')" try conn.open() cmd .connection = conn .commandtype = commandtype.text .commandtext = sqlquery .executenonquery end catch ex exception msgbox("error @ : " & ex.message) conn.close() end try
but getting error:
error converting data type varchar numeric
if change & textbox2.text &
& val(textbox2.text) &
in query, data stored 20 in price.
you not allowed assign 20.000,00 textbox2.text property. try:
textbox2.text = "20.00"
on note not pass variables directly sql insert query. try code below:
dim sqlquery string = "insert tableitem(item,price) values(@item, @price)" try conn.open() cmd .parameters.addwithvalue("@item, textbox1.text) .parameters.addwithvalue("@price, textbox2.text) .connection = conn .commandtype = commandtype.text .commandtext = sqlquery .executenonquery end catch ex exception msgbox("error @ : " & ex.message) end try conn.close()
Comments
Post a Comment