Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Any help would be greatly appreciated. I have a date field that when the query is run, it errors out when it reaches a blank or null value. I am new to VB and the application which is in Visual Studio. I am hoping someone can advise me. I get the error at ' myTempCmd = cmd.ExecuteScalar '

The code:

Case DataFields.SettleDate.DisplayName


Dim SQLTimeout = 120
Dim myTempCon As SqlConnection
myTempCon = New SqlConnection(ConfigurationManager.ConnectionStrings("DT_LoanReport.My.MySettings.DT_ConnectionString").ConnectionString)
myTempCon.Open()

Dim myTempCmd As String = ""
myTempCmd += "SELECT trades.del_date"
myTempCmd += " FROM gen as gen WITH (NOLOCK)"
myTempCmd += " LEFT outer join trades WITH (NOLOCK) on gen.trade_no = trades.trade_no"
myTempCmd += " WHERE "
myTempCmd += " gen.trade_no = '" + myDataReader("trade_no").ToString.Trim + "'"

Dim cmd As New SqlCommand()
cmd.Connection = myTempCon
cmd.CommandText = myTempCmd
'Dim myTempValue As String = ""
cmd.CommandTimeout = SQLTimeout


Try
myTempCmd = cmd.ExecuteScalar


Catch ex As SqlException
MsgBox("The Querry took too long, please try again")

myTempCon.Close()

_Value = ""
Exit Select
End Try

myTempCon.Close()

_Value = myTempCmd
Posted
Comments
[no name] 9-Jul-15 14:36pm    
And the error message that I get when I run my code is...?
dlreynolds 9-Jul-15 14:37pm    
Error converting varchar to numeric :)
[no name] 9-Jul-15 14:49pm    
What is the actual error message? Or do you think that it might be that gen.trade_no is really an integer and you are passing a string in your SQL query?
dlreynolds 9-Jul-15 15:00pm    
the date(trade.del_date) will populate as long as there are no null values. trade_no is a (string, number (5,0)
dlreynolds 9-Jul-15 14:49pm    
Error converting data type varchar to numeric. I'm sorry, I left out data type.

1 solution

So start by looking at your data: specifically what the DataReader is returning for "trade_no", and what the trades and gen tables are holding for their trade_no columns.

But the most likely is that the DateRear is pass a non-numeric value as a string to your SQL.

And do yourself a favour: Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
 
Share this answer
 

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