5
By performing the following UPDATE
through my API (.NET Core):
UPDATE Aula
SET WHATEVER = WHATEVER
WHERE ID_AULA = @examID
Code:
string query = builder
.AppendLine("UPDATE Aula")
.AppendLine("SET WHATEVER = WHATEVER")
.AppendLine("WHERE ID_AULA = @examID").ToString();
SqlCommand command = new SqlCommand(query, sqlConnection);
command.Parameters.AddWithValue("@examID", item.ExamID);
sqlConnection.Open();
command.ExecuteNonQuery();
I get the following error: Conversion failed when converting the varchar value '22234390|22234391' to data type int.
Being that my field ID_AULA
is a field varchar
and the parameter @examID
comes from a property string
.
If I do the same thing for the bank that way, it works:
UPDATE Aula
SET WHATEVER = WHATEVER
WHERE ID_AULA = '22245089|22245090'
Why it happens and how to correct?
Put a larger snippet of code please
– Rovann Linhalis
@Rovannlinhalis made
– perozzo
you speak of the field ID_AULA, but in your code you use the field ID_AULA_DETRAN, you are sure it is the same field?
– Júlio Neto
@Júlioneto was my mistake when omitting the name of haha fields, but yes, I am using the right field.
– perozzo
how is the statement of
item.ExamID
?– Rovann Linhalis
@Rovannlinhalis a property string normal public string Examid {get;set;}
– perozzo
tried to declare the type in Sqlparameter?
command.Parameters.Add(new SqlParameter { ParameterName = "@examID", DbType = DbType.String, Value = item.ExamID });
– Leandro Angelo
I think it might be what Leandro Angelo said. Related: https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/configuring-parameters-and-parameter-data-types
– Rovann Linhalis
@Leandroangelo thank you very much!
– perozzo