Doubt about the error "Error converting data type varchar to float"

4
  

Error converting data type varchar to float

This error is occurring when I make a Update in a table, below the code:

SqlCommand cmd = new SqlCommand("UPDATE Notas SET nota = '" + nota.nota + "' WHERE idMateria = '" + nota.idMateria + "' and idAluno = '" + nota.idAluno + "';", con);
cmd.ExecuteNonQuery();

The note field is created in DB as float , I already tried to do the conversion but I can not get any tips?

    
asked by anonymous 09.12.2015 / 03:02

2 answers

3
  

Editing
Warning: This response may leave your code vulnerable to   SQL Injection, use this other answer .

Well, let's break it down ...

    / li>
  • As we talked about in the chat of the issue, it seems that your database is set to the default WHERE where the actual values are separated by period and not comma like here in our Brazil.

The question is:
What kind of variable is int ?
varchar or en-US ?

string

In this case you will need to do a REPLACE in your query replacing the comma with a dot, and after that you'll also need to do a CONVERT or CAST so that nota.nota is magically transformed into a string value. This is required because it is not possible to insert a float into a field that was created to receive string .

The query would look like this:

SqlCommand cmd = new SqlCommand("UPDATE Notas SET nota = CONVERT(FLOAT, REPLACE('" + nota.nota + "', ',', '.')) WHERE idMateria = " + nota.idMateria + " and idAluno = " + nota.idAluno + ";", con);

Or you could still do float via C # . before creating the query:

nota.nota = nota.nota.Replace(",", ".");

And thus generate the query without the replace:

SqlCommand cmd = new SqlCommand("UPDATE Notas SET nota = CONVERT(FLOAT, '" + nota.nota + "') WHERE idMateria = " + nota.idMateria + " and idAluno = " + nota.idAluno + ";", con);

Would these be the best options? In my opinion, it does not have a very good smell ...
In my humble opinion, if string was of type float would look a lot nicer.

float

As the value is coming from replace ?% < nota.nota or float ?

If it's arriving as a string, convert to View on string , but I'd rather prefer convert to float on float already sending object ready.

So your query would look like this:

SqlCommand cmd = new SqlCommand("UPDATE Notas SET nota = " + nota.nota + " WHERE idMateria = " + nota.idMateria + " and idAluno = " + nota.idAluno + ";", con);
    
09.12.2015 / 03:20
5

You need to use parameters:

var cmd = new SqlCommand(@"UPDATE Notas 
                           SET nota = @nota 
                           WHERE idMateria = @idMateria 
                           and idAluno = @idAluno", con);
cmd.Parameters.Add("@nota", SqlDbType.Float).Value = nota.nota;
cmd.Parameters.Add("@idMateria", SqlDbType.Int).Value = nota.idMateria;
cmd.Parameters.Add("@idAluno", SqlDbType.Int).Value = nota.idAluno;
cmd.ExecuteNonQuery();

Parameters solve for you the type of the variable, prevent SQL Injection and strange behavior in execution. See more here .

    
09.12.2015 / 15:29