IGottaGo IGottaGo - 5 months ago 21
Vb.net Question

Failed to convert parameter value from a string to a decimal vb.net

I have an application where in the textbox I insert a number and click on a button and it should add that value on a textbox that I've called "Balance". To make that happen I've used a query to sum the amount and display it where I want. But I at the moment I have some issues with casts.

This is the function that I am using to get the results and then make some if statements to validate it. I have this function on a class.

Public Function BalancoTotal()
SqlCon = New SqlConnection
SqlCon.ConnectionString = "........"
Try
SqlCon.Open()
Query2 = "SELECT
CAST(SUM(amountTransaction) AS INT)
FROM Transactions"

SqlCmd = New SqlCommand(Query2, SqlCon)
SqlDR = SqlCmd.ExecuteReader
If SqlDR.Read() Then
Return SqlDR.GetDecimal(0)
End If
Catch ex As Exception
MsgBox(ex.Message)
Finally
SqlCon.Close()
End Try
End Function


And then I've called this function on my main form like this

txtBalance.Text = callMethod.BalancoTotal()


In the load form I've used this to lines of code to format the textbox

txtBalance.Text = 0.00
txtBalance.Text = FormatCurrency(txtBalance.Text)


But this is giving me 2 erros:


failed to convert parameter value from a string to a decimal


And


specified cast is not valid


Do you have any idea what I am doing wrong?

Answer

Try:

txtBalance.Text = callMethod.BalancoTotal().ToString()

txtBalance.Text = "0.00"

Event if it should cast it ...

For the other problem you should try:

.Add("@balaTransacao", SqlDbType.BigInt).Value = cint(txtBalance.Text)