Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I am trying to read data from a database and into a labels text property.

However I am receiving the error 'Error Converting datatype varchar to numeric'.

I am now really bewildered to why... It is occurring on While reader.Read()

Any help will be much appreciated, Thanks.

Dim queryString As String = "SELECT pointsBalance FROM Accounts WHERE custID = '" & CustId & "'"
       Dim SqlConnectionString As String = My.Settings.LoyaltyConnectionString
       CreateCommand(queryString, SqlConnectionString)

   End Sub

   Public Sub CreateCommand(ByVal queryString As String, ByVal connectionString As String)

       Using connection As New SqlConnection(connectionString)
           connection.Open()
           Dim command As New SqlCommand(queryString, connection)
           Dim reader As SqlDataReader = command.ExecuteReader()
           While reader.Read()
               LabelOutput = reader(0)
           End While
       End Using

   End Sub
Posted

As the error suggests, varchar cannot be converted into a number implicitly.
You need to apply a cast to do this type of conversion.
 
Share this answer
 
Check the design of the "Accounts" table. It probably has custID as a varchar and the CustId variable is numeric. Change the design of the table or pass the string version of CustId.
 
Share this answer
 
Comments
Glen Childs 7-Nov-13 11:02am    
Hello Richard, the custID was in the Accounts as an Numeric(16,0) and CustId variable was a string.. So I have changed the variable to be an Integer and this has solved the problem thanks:)

Quick question I have added FrmCustomerAccounts.lblPoints.Text = LabelOutput into the While under 'labeloutput = reader (0)' however this isnt adding the number into the label's text, what have I done wrong?
Richard C Bishop 7-Nov-13 11:08am    
You are welcome. Accept this solution so the thread will be removed from the unanswered queue.

What is LabelOutPut? If it is a label on your page, change that line to this:

LabelOutput.Text = reader(0)
Glen Childs 7-Nov-13 11:13am    
Labelout put is a string, it does appear though that the while loop is being skipped.. .
Richard C Bishop 7-Nov-13 11:40am    
Your query might not be returning any records.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900