c.s = "UPDATE produit SET codeBare = '" tbCodeBare.Text "',ref = '" tbRef.Text "',designation = '" tbDesignation.Text "',qte = " float.Parse(tbQte.Text.Replace(',', '.')) ", minQte = " float.Parse(tbMinQte.Text.Replace(',', '.')) ", puDevis = " float.Parse(tbPUDevis.Text.Replace(',', '.')) ", puAchat = " float.Parse(tbPUAchat.Text.Replace(',', '.')) " , typeQty = '" cbTypeQte.Text "', puVente = " float.Parse(tbPUVente.Text.Replace(',','.')) " ,totalEmbalage = '" tbTotalEmb.Text "' WHERE produit.produitID = " int.Parse(lblIdProduit.Text.ToString()) "; ";
System.FormatException : 'Input string was not in a correct format.'
When I update table without a float
column it works, but with a float
column it does not.
CodePudding user response:
c.s = "UPDATE produit SET codeBare = @Codebare, ref = @Ref , qty = @Qty , typeQty = @Typeqty,designation = @Designation,minQty = @Minqty ,puDevis = @Pudevis,puAchat = @Puachat,puVente = @Puvente, totalEmbalage = @totalEmbalage WHERE produit.produitID = @Produitid; ";
c.cn.Open();
SqlCommand cmd = new SqlCommand(c.s, c.cn);
cmd.Parameters.AddWithValue("@Codebare", tbCodeBare.Text);
cmd.Parameters.AddWithValue("@Ref", tbRef.Text);
cmd.Parameters.AddWithValue("@Qty", float.Parse(tbQte.Text));
cmd.Parameters.AddWithValue("@Designation", tbDesignation.Text);
cmd.Parameters.AddWithValue("@Minqty", float.Parse(tbMinQte.Text));
cmd.Parameters.AddWithValue("@Typeqty", cbTypeQte.Text);
cmd.Parameters.AddWithValue("@Pudevis", float.Parse(tbPUDevis.Text)));
cmd.Parameters.AddWithValue("@Puachat", float.Parse(tbPUAchat.Text));
cmd.Parameters.AddWithValue("@Puvente", float.Parse(tbPUVente.Text));
cmd.Parameters.AddWithValue("@totalEmbalage", tbTotalEmb.Text);
cmd.Parameters.AddWithValue("@Produitid", int.Parse(lblIdProduit.Text));
CodePudding user response:
First of all, as Franz Gleichmann said in the comments please use parameterized queries to avoid SQL injection attacks.
Otherwise to come to your questions:
- For readability it is common to use line breaks for large lines.
- Please separate the
float.Parse()
andint.Parse()
to make separate assignments to local variables.
Most likely the exception was caused by float.Parse()
and if you do separate assignments you will see exactly which textbox input was invalid.
First:
c.s = "UPDATE produit SET codeBare = '" tbCodeBare.Text
"',ref = '" tbRef.Text
"',designation = '" tbDesignation.Text
"',qte = " float.Parse(tbQte.Text.Replace(',', '.'))
", minQte = " float.Parse(tbMinQte.Text.Replace(',', '.'))
", puDevis = " float.Parse(tbPUDevis.Text.Replace(',', '.'))
", puAchat = " float.Parse(tbPUAchat.Text.Replace(',', '.'))
" , typeQty = '" cbTypeQte.Text
"', puVente = " "" float.Parse(tbPUVente.Text.Replace(',', '.'))
" ,totalEmbalage = '" tbTotalEmb.Text
"' WHERE produit.produitID = " int.Parse(lblIdProduit.Text.ToString()) "; ";
End then:
NumberStyles style = NumberStyles.AllowDecimalPoint;
CultureInfo culture = CultureInfo.CreateSpecificCulture("fr-FR");
bool parseSuccess = float.TryParse(tbDesignation.Text, style, culture, out float theDesignation);
parseSuccess = parseSuccess && float.TryParse(tbQte.Text.Replace, style, culture, out float theQte);
parseSuccess = parseSuccess && float.TryParse(tbMinQte.Text, style, culture, out float theMinQte);
parseSuccess = parseSuccess && float.TryParse(tbPUDevis.Text, style, culture, out float thePUDevis);
parseSuccess = parseSuccess && float.TryParse(tbPUAchat.Text, style, culture, out float thePUAchat);
parseSuccess = parseSuccess && float.TryParse(tbPUVente.Text, style, culture, out float thePUVente);
parseSuccess = parseSuccess && int.TryParse(lblIdProduit.Text.ToString(), out int theIdProduit);
if (!parseSuccess)
{
MessageBox.Show("Wrong input");
return;
}
c.s = "UPDATE produit SET codeBare = '" tbCodeBare.Text
"',ref = '" tbRef.Text
"',designation = '" theDesignation
"',qte = " theQte
", minQte = " theMinQte
", puDevis = " thePUDevis
", puAchat = " thePUAchat
" , typeQty = '" cbTypeQte.Text
"', puVente = " "" thePUVente
" ,totalEmbalage = '" tbTotalEmb.Text
"' WHERE produit.produitID = " lblIdProduit "; ";