1

I'm trying to retrieve an amount from SQL.

I have a table tbVendas with a column ValorTotalVendas of datatype DECIMAL.

I have the following select query

SELECT IdVendas AS 'Venda' 
FROM tbVendas 
WHERE ValorTotalVendas = '@valorTotal'

And below my command

SqlCommand comando = new SqlCommand(strSql, conn);
comando.Parameters.Add("@valorTotal", SqlDbType.Decimal);
comando.Parameters["@valorTotal"].Value = Convert.ToDecimal(txbValorFiltro.Text);

DataTable vendas = new DataTable();

conn.Open();

SqlDataAdapter sqdDA = new SqlDataAdapter(strSql, conn);
sqdDA.Fill(vendas);

But I get this error:

Error converting datatype varchar to numeric

Can anyone please help me?

3
  • What's the value of txbValorFiltro.Text and what's the decimal separator in your Operative System? Commented Mar 28, 2018 at 17:40
  • 1
    Did you check to make sure all the values can actually be converted to numeric? Seems like the most obvious step to do. And don't store numeric values in the database as non-numeric. Commented Mar 28, 2018 at 17:41
  • You need to remove the single quotes around the parameter name: SELECT IdVendas AS 'Venda' FROM tbVendas WHERE ValorTotalVendas = @valorTotal. Otherwise, it sees it as the string instead of seeing it as a parameter name. Commented Mar 28, 2018 at 17:47

4 Answers 4

1

Your select statement looks as follows :

SELECT IdVendas AS 'Venda' FROM tbVendas WHERE ValorTotalVendas = '@valorTotal'

Take a clear look at '@valorTotal'.In any sql statement, '' is used to pass a value to any column or so.But when you use '@' , it means you are passing a parameter to the column/cell so that you can use it in future for multiple operations.

So the basic problem is,you want to pass a parameter but you are passing it as a value instead.So,either you pass a value or a parameter :

  SELECT IdVendas AS 'Venda' FROM tbVendas WHERE ValorTotalVendas = 'YourValueHere'

 ///Or

 SELECT IdVendas AS 'Venda' FROM tbVendas WHERE ValorTotalVendas = @parameterHere

Hope you understand.

Sign up to request clarification or add additional context in comments.

Comments

0

Rafael,

You must remove the single quote of your query:

"SELECT IdVendas AS 'Venda' FROM tbVendas WHERE ValorTotalVendas = @valorTotal"

The SQL Server is interpreting '@valorTotal' as a VARCHAR instead of DECIMAL.

Thanks.

Comments

0

You can try to add a safe-mechanism like below to prevent potential bata-based future errors:

decimal amount;
bool checkValue = Decimal.TryParse(txbValorFiltro.Text, out amount);

if(checkValue)
   comando.Parameters["@valorTotal"].Value = amount;
else
   comando.Parameters["@valorTotal"].Value = -1; //or null, if field is nullable

Comments

0

try this

SELECT IdVendas AS 'Venda' FROM tbVendas WHERE ValorTotalVendas = @valorTotal

you are facing the error because of the '@valorTotal' , you will no more face this issue after changing it to @valorTotal

6 Comments

It would be better if you explain this :)
@zackraiyan just explained.
Sorry but not much of an explanation ... But i appreciate you posted an answer...You can take a look at mine if u want :) .. We're all here to help each other right ?:)
@zackraiyan when the issue is identified, you can definitely explain it. we have answered the question wat too long before you started :)
,i don't mean to hurt u man :( ... I just pointed out so that u get a few more upvotes :(
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.