Question : SQL + VB.net, update query.  How to convert .. varchar to money  ?

See full code below.

I'm trying to run an update statement to save all fields.  I have three form fields....income, paymentamount,contractamount that are giving me trouble.  I'm getting conversion errors when I run it..... "error converting varchar to numeric"

I tried formatting the strings as currency... in vb using ...formatcurrency(val(txtLeadIncome.text.tostring),2)

I've also tried using CONVERT( ...in the sql statement.

What is the proper way to convert varchar to "money" in sql ?

------------------------------------------------Trouble is here...
"',ReferencePhoneNumber='" & txtReferencePhoneNumber.Text & _
"',Income=" & "CONVERT(money,'" & strincome & "',2)" & _
",PaymentAmount=" & "CONVERT(money,'" & strpaymentamount & "',2)" & _
",ContractAmount=" & "CONVERT(money,'" & strcontractamount & "',2)" & _
",PaymentPlan='" & txtPaymentPlan.Text & _
-------------------------------------------------------------------
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
Dim strincome As String = txtLeadIncome.Text
        Dim strpaymentamount As String = txtPaymentAmount.Text
        Dim strcontractamount As String = txtContractAmount.Text
        If txtLeadIncome.Text = "" Then strincome = "0.0000"
        If txtPaymentAmount.Text = "" Then strpaymentamount = "0.000"
        If txtContractAmount.Text = "" Then strcontractamount = "0.0000"
 
        Dim sqlstrSaveFields As String = "LeadName='" & txtLeadName.Text & _
"',Address1='" & txtLeadAddress.Text & _
"',Address2='" & txtAddress2.Text & _
"',City='" & txtLeadCity.Text & _
"',State='" & txtLeadState.Text & _
"',Zipcode='" & txtLeadZipCode.Text & _
"',PhoneNumber='" & txtLeadPhone.Text & _
"',Relationship='" & cboRelationshipStatus.Text & _
"',Surname='" & cboSurname.Text & _
"',Age='" & txtLeadAge.Text & _
"',DOB='" & txtLeadDOB.Text & _
"',RentOwnLWP='" & cboRentOwnLWP.Text & _
"',RentOwnLWPHowLong='" & txtRentOwnLWPHowlong.Text & _
"',Occupation='" & txtLeadOccupation.Text & _
"',OccupationHowLong='" & txtOccupationHowLong.Text & _
"',Employer='" & txtEmployer.Text & _
"',WorkPhoneNumber='" & txtWorkPhoneNumber.Text & _
"',SpouseName='" & txtSpouseName.Text & _
"',SpouseOccupation='" & txtSpouseOccupation.Text & _
"',SSN='" & txtLeadSSN.Text & _
"',Reference='" & txtReference.Text & _
"',ReferencePhoneNumber='" & txtReferencePhoneNumber.Text & _
"',Income=" & "CONVERT(money,'" & strincome & "',2)" & _
",PaymentAmount=" & "CONVERT(money,'" & strpaymentamount & "',2)" & _
",ContractAmount=" & "CONVERT(money,'" & strcontractamount & "',2)" & _
",PaymentPlan='" & txtPaymentPlan.Text & _
"',PaymentPlanYears='" & txtPaymentPlanYears.Text & _
"',CreditCardNumber='" & txtCreditCardNumber.Text & _
"',CreditCardCVV='" & txtCVVCode.Text & _
"',CreditCardExpDate='" & txtExpirationDate.Text & _
"',CreditCardName='" & txtNameOnCreditCard.Text & _
"',App='" & txtAppCode.Text & _
"',BankName='" & txtBankName.Text & _
"',BankCity='" & txtBankCity.Text & _
"',BankState='" & txtBankState.Text & _
"',BankRoutingNumber='" & txtBankRoutingNumber.Text & _
"',BankAccountNumber='" & txtBankAccountNumber.Text & _
"',BillDate='" & txtBillDate.Text & _
"',Notes='" & txtNotes.Text & _
"',AlternativePhoneNumber='" & txtLeadAlternatePhoneNumber.Text & _
"',CardType='" & cboCreditCardType.Text & "'"
 
 
        cboNewCallResult.Text = ""
        cboNewKillReason.Text = ""
 
 
        Dim conn As New SqlConnection()
        conn.ConnectionString = dbconstring
        Dim cmd As New SqlCommand
        cmd.CommandType = CommandType.Text
        cmd.CommandText = "UPDATE " & "[user].tbl" & accounttype & "_" & listname & " SET " & sqlstrSaveFields & " WHERE LeadID='" & leadID & "'"
        cmd.Connection = conn
 
        Dim previousConnectionState As ConnectionState = conn.State
        Try
            If conn.State = ConnectionState.Closed Then
                conn.Open()
                cmd.ExecuteNonQuery()
            End If
 
 
 
 
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            If previousConnectionState = ConnectionState.Closed Then
                conn.Close()
            End If
        End Try
 
    End Sub

Answer : SQL + VB.net, update query.  How to convert .. varchar to money  ?

You will get conversion errors when you are trying to convert alpha-numeric strings to numeric.

Check if your ReferencePhoneNumber column values are stored in xxx-xxx-xxxx or (xxx) xxx xxxx or xxx xxx xxxx formats.
If yes, you will get conversion errors.

Income, PaymentAmount,ContractAmount columns are defined as money. you should not get any error in this.
Correct the issue with ReferencePhoneNumber.

You can filter out such values with WHERE clause or you can correct those values and then CONVERT to whatever the datatype you want.
Random Solutions  
 
programming4us programming4us