Search code examples
c#sql-serversql-server-express

Trouble inserting number with commas into database


I have a problem of inserting numbers with comma into database. It only accepts dot but i have function that only works with commas so is there any idea to solve this like converting decimal seperation from dot to comma

if (radioButton1.Checked)
        {
            Avance = 200;
        }
        else if (radioButton2.Checked)
        {
            Avance = 0;
        }
        cnx.Open();
        SqlCommand cmd = cnx.CreateCommand();
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "insert into Employeur values('" + this.txt_ID.Text + "','" + this.txt_Nom.Text + "','" + this.txt_QUA.Text + "','" + this.txt_Salaire.Text + "','" + this.txt_NBRJ.Text + "','" + this.txt_HSUP.Text + "','" + this.txt_SalireHeur.Text + "','" + this.txt_Somme.Text + "','" + this.txt_Dette.Text + "','" + this.Avance + "','" + this.txt_Credit.Text + "','" + this.txt_Montant.Text + "','" + this.txt_Paye.Text + "','" + this.txt_Reste.Text + "')";
        cmd.ExecuteNonQuery();
        cnx.Close();
        MessageBox.Show("Se payement est enregistrer");

Solution

  • You desperately need to learn how to parameterize your queries. You have several other issues going on here to. Here is a shortened version of how this query should look. Of course I would prefer to get the query out of my code entirely with a stored procedure.

    cmd.CommandText = "insert into Employeur (ID, Nom) values(@txt_ID, @txt_Nom)";
    cmd.Parameters.Add("@txt_ID", SqlDbType.VarChar, 30).Value = this.txt_ID.Text;
    cmd.Parameters.Add("@txt_Nom", SqlDbType.VarChar, 30).value = this.txt_Nom.Text;
    

    You would need to set the appropriate datatypes and sizes to your tables.

    Also, look into the USING statement. And never just reuse a connection.