Error query Sql Server insert, not correct value format C# -
i have problem inserting data sql server 2012
.
problem when save quantita
and prezzo
the values changed! can be?. quantita
, prezzo
money
values taken datagridview leave under example image of datagridview
used insertion , result of insertion database.
value within datagridview:
after insert sql-server:
sqlconnection conn = db.apriconnessione(); sqltransaction sqltran = conn.begintransaction(); try { //avvio la transazione sqlcommand command = conn.createcommand(); command.transaction = sqltran; //inserimentobolla datetime dataconvertita = convert.todatetime(labeldataddtmod.text); command.commandtext = "set identity_insert bolla on"; command.executenonquery(); command.commandtext = "insert bolla(numeroddt,idcantiere,dataddt,agente,ragionesociale,codicecliente,riferimentiinterni,importo,destinazione,filiale,magazzino,preparato,vettore,tipotrasporto) values('" + labelnumddtmod.text+"','"+idcantiere+"',convert(datetime,'"+dataconvertita+"', 103),'" + labelagentemod.text+"','"+labelragionesocialemod.text+"','"+int.parse(labelcodiceclientemod.text)+"','"+labelrifinternomod.text+"','"+float.parse(labelimportomod.text)+"','"+labeldestmod.text+"','"+labelfilialemod.text+"','"+labelmagazzinomod.text+"','"+labelpreparatodamod.text+"','"+labelvettoremod.text+"','"+labeltipotrasportomod.text+"')"; command.executenonquery(); command.commandtext = "set identity_insert bolla off"; command.executenonquery(); //fine bolla //inserimento articolo (int rows = 0; rows < datagridviewarticoli.rows.count; rows++) { string fornitore = datagridviewarticoli.rows[rows].cells[0].value.tostring(); string modellofornitore = datagridviewarticoli.rows[rows].cells[1].value.tostring(); string siglametel = datagridviewarticoli.rows[rows].cells[2].value.tostring(); string codicemetel = datagridviewarticoli.rows[rows].cells[3].value.tostring(); string codiceinterno = datagridviewarticoli.rows[rows].cells[4].value.tostring(); string descrizione = datagridviewarticoli.rows[rows].cells[5].value.tostring(); float prezzo = float.parse(datagridviewarticoli.rows[rows].cells[6].value.tostring()); // messagebox.show(" "+prezzo); float quantita = float.parse(datagridviewarticoli.rows[rows].cells[8].value.tostring()); // messagebox.show("quantita: "+quantita); command.commandtext = "insert articolocantiere(idcantiere,idutente,codart,codmarca,codiceinterno,modellofornitore,prezzo,quantita,fornitore,importato) values('" + idcantiere + "','"+u.idutente+"','" + codicemetel + "','" + siglametel + "','" + codiceinterno + "','" + modellofornitore + "','" + prezzo + "','" + quantita + "','comet','bolla')"; command.executenonquery(); } //fine inserimento articolo //conferma delle transazioni con la commit sqltran.commit(); } catch (exception ex) { sqltran.rollback(); messagebox.show("errore nell'inserimento "+ex); } conn.close(); this.dialogresult = dialogresult.ok; this.close();
you insert string values ',' separator money
data type, , problem.
first of all, in sql server decimal separator '.', not ','.
second, should not pass numbers
strings
here code reproduce problem:
declare @t table(col money) insert @t values ('1,44'); select * @t; ---------- -- 144,00
money data type accepts input ','
separator, treats not decimal separator thousand separator.
so resolve it, best way pass in numbers numbers, worst thing can replace ',' '.' before insert:
declare @t table(col money) insert @t values (replace ('1,44', ',', '.')); select * @t; ----- ---1,44
Comments
Post a Comment