Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
alboi
Starting Member
12 Posts |
Posted - 2005-08-01 : 18:02:25
|
I have tried everythign to try to fix this issue however i was not able. please help here is a rundown of my appI have a stored procedure as suchCREATE PROCEDURE AvgLast (@ag integer Output,@dateA DateTime,@dateB DateTime,@prodSku varchar(6))AS SELECT AVG(Qty) AS @ag FROM dbo.SalesHWHERE (ProdSku = '@prodsku') AND (SDate BETWEEN @dateA and @dateB)GO i fire this trought this function Public Function GetAvg(ByVal DateA As DateTime, ByVal DateB As DateTime, ByVal a_prodsku As String) As String Dim paramsIn As New DbParams Dim paramsOut As New DbParams Dim param1 As New DbParam("DateA", DbType.DateTime, False, DateA) Dim param2 As New DbParam("Dateb", DbType.DateTime, False, DateB) Dim param3 As New DbParam("prodsku", DbType.Int16, False, a_prodsku) Dim param As New DbParam("ag", DbType.AnsiString, False) paramsIn.Add("DateA", param1) paramsIn.Add("dateb", param2) paramsIn.Add("prodsku", param3) paramsOut.Add("ag", param) Dim ret As String = "" Dim cm As IDbCommand Dim rd As IDataReader cm = _DataAccess.getCommand(paramsIn, paramsOut, "AvgLast") Try rd = cm.ExecuteReader If rd.Read Then ret = rd.Item("") End If Catch ex As Exception ret = ex.Message End Try Return ret End Function the GetCOmmand is coded like such Friend Overloads Function getCommand(ByVal paramsIn As DbParams, ByVal paramsOut As DbParams, ByVal spName As String) As IDbCommand Dim cn As IDbConnection cn = m_connects.getConnection() Dim cmd As IDbCommand = cn.CreateCommand cmd.CommandText = spName cmd.CommandType = CommandType.StoredProcedure loadParams(cmd, paramsIn, ParameterDirection.Input) loadParams(cmd, ParamsOut, ParameterDirection.Output)end Function to load the params i have another sub which is coded as such Private Overloads Sub loadParams(ByVal cmd As IDbCommand, ByVal aParams As DbParams, ByVal pramDir As ParameterDirection) If aParams Is Nothing Then Exit Sub Dim x As Integer = 0 Dim en As IEnumerator = aParams.Values.GetEnumerator While en.MoveNext Dim p As DbParam = DirectCast(en.Current, DbParam) Dim param As IDbDataParameter = cmd.CreateParameter() 'Try ' param.DbType = Me.inferType(de.Value) 'Catch ex As Exception ' Throw New Exception(de.Key & " value - unable to infer type.") 'End Try param.DbType = p.ParamType param.Value = p.ParamValue param.Direction = pramDir param.ParameterName = p.ParamName cmd.Parameters.Insert(x, param) End While End Sub i get an error saying Error Covnerting Data type varchar to DateTimeit seems that its a error with the Stored Procedure ?? as the error comes as exception when Stored Procedure is ran.thanks |
|
rockmoose
SQL Natt Alfen
3279 Posts |
Posted - 2005-08-01 : 18:05:28
|
Might be a data problem, what is the datatype of the SDate column ?SELECT * FROM dbo.SalesH WHERE ISDATE(SDate) = 0rockmoose |
|
|
alboi
Starting Member
12 Posts |
Posted - 2005-08-01 : 18:07:07
|
The SDate is DateTime in the table |
|
|
alboi
Starting Member
12 Posts |
Posted - 2005-08-01 : 18:30:27
|
why do i need the "(" in those declarations ?? |
|
|
rockmoose
SQL Natt Alfen
3279 Posts |
Posted - 2005-08-01 : 18:39:46
|
quote: Originally posted by alboi why do i need the "(" in those declarations ??
Sorry, deleted post. I was too quick.It seems to me that somehow the code is sending a string to the date parameters. But I don't know how.If You can use profiler to see exactly what sql is sent when You execute the line:cm.ExecuteReaderIt might give You some hints.What is DbParameter ?, I did not find it in my MSDN Library Doc.VB is not my skillset.I do know one thing,'@prodsku' should not be quoted in the stored procedure...rockmoose |
|
|
alboi
Starting Member
12 Posts |
Posted - 2005-08-01 : 18:59:34
|
dbparams is a collection of parameters basicly this is the dbparam code Option Explicit On Option Strict OnNamespace DataAccessFramework Public Class DbParam Private _FieldName As String Private _ParamType As DbType Private _ParamValue As Object Private _IsPrimaryKey As Boolean Public Sub New(ByVal aFieldName As String, ByVal aParamType As DbType, ByVal aIsPrimaryKey As Boolean) MyBase.new() _FieldName = aFieldName _ParamType = aParamType _IsPrimaryKey = aIsPrimaryKey End Sub Public Sub New(ByVal aFieldName As String, ByVal aParamType As DbType, ByVal aIsPrimaryKey As Boolean, ByVal aParamValue As Object) MyBase.new() _FieldName = aFieldName _ParamType = aParamType _ParamValue = aParamValue _IsPrimaryKey = aIsPrimaryKey Select Case _ParamType Case Is = DbType.DateTime Dim t1 As New DateTime(1753, 1, 1) Dim t2 As New DateTime(9999, 12, 31) Dim t As DateTime = DirectCast(_ParamValue, DateTime) If (t < t1) Or (t > t2) Then _ParamValue = DBNull.Value End If Case Else If _ParamValue Is Nothing Then _ParamValue = DBNull.Value End If End Select End Sub Public ReadOnly Property FieldName() As String Get Return _FieldName End Get End Property Public ReadOnly Property ParamType() As DbType Get Return _ParamType End Get End Property Public Property ParamValue() As Object Get Return _ParamValue End Get Set(ByVal Value As Object) _ParamValue = Value End Set End Property Public ReadOnly Property ParamName() As String Get Return "@" & _FieldName End Get End Property Public ReadOnly Property IsPrimaryKey() As Boolean Get Return _IsPrimaryKey End Get End Property End ClassEnd Namespace and the dbparams (the collection) isNamespace DataAccessFramework Public Class DbParams Inherits Hashtable Public Shadows Sub Add(ByVal key As String, ByVal value As DbParam) MyBase.Add(key, value) End Sub Public Shadows Property Item(ByVal parameterName As String) As DbParam Get Return MyBase.Item(parameterName) End Get Set(ByVal Value As DbParam) MyBase.Item(parameterName) = Value End Set End Property Public Shadows Function Contains(ByVal parameterName As String) As Boolean Return MyBase.Contains(parameterName) End Function Public Shadows Function ContainsKey(ByVal parameterName As String) As Boolean Return MyBase.ContainsKey(parameterName) End Function Public Shadows Function ContainsValue(ByVal aParam As DbParam) As Boolean Return MyBase.ContainsValue(aParam) End Function Public Shadows Sub Remove(ByVal parameterName As String) MyBase.Remove(parameterName) End Sub End ClassEnd Namespace |
|
|
rockmoose
SQL Natt Alfen
3279 Posts |
Posted - 2005-08-01 : 19:23:36
|
Can't see anything obviously wrong here. (read through the code)Try Profiler to catch the sql sent.rockmoose |
|
|
alboi
Starting Member
12 Posts |
Posted - 2005-08-01 : 19:28:44
|
profiler??sorry i am lost! |
|
|
rockmoose
SQL Natt Alfen
3279 Posts |
Posted - 2005-08-01 : 19:43:56
|
Sql Server has a tool called Profiler.Run "profiler" from the command prompt.Or in EM, uner Tools : "Sql Profiler"Or from Start -> Programs -> Sql Server -> ProfilerWith that tool You can monitor all SQL sent to a Sql Server instance.It's a little awkward the first times, but very good once You get the hang of it.Start Profiler, Choose: File -> New -> Trace, connect to Your SQL Server.You are now monitoring the sql traffic sent to that Sql Server.You can start / stop / pause the trace,so that You only need to monitor the part where bad sql code is sent.(It can generate a lot of output)rockmoose |
|
|
alboi
Starting Member
12 Posts |
Posted - 2005-08-01 : 20:08:59
|
thanks man, however the server is our schools and i have to be sysadmin to be able to trace this trafic :) |
|
|
rockmoose
SQL Natt Alfen
3279 Posts |
Posted - 2005-08-02 : 01:49:05
|
:(Are You sure about the proc definition You posted, because there is an error in the one You posted that won't compile.In Query Analyzer, You can run:EXEC sp_helptext 'AvgLast'To see the current definition.This part is bad: AVG(Qty) AS @agHas to be: @ag = AVG(Qty)Other than that, I can only suggest that You step into the code and do a detailed checkof all the parameters in the command object. The type, name and value.rockmoose |
|
|
Kristen
Test
22859 Posts |
Posted - 2005-08-02 : 02:23:15
|
Rocky I'm sure you are doing find, but I just stopped by anyway!Alboi:Just in case you missed Rocky's two points here's the cleaned up code:CREATE PROCEDURE AvgLast( @ag integer Output, @dateA DateTime, @dateB DateTime, @prodSku varchar(6))ASSELECT @ag = AVG(Qty)FROM dbo.SalesHWHERE ProdSku = @prodsku AND SDate BETWEEN @dateA and @dateB AND Qty IS NOT NULLGO Can you try running it in Query Analyser, or some other SQL tool, rather than through your application - to test if the SProc gets what you want.DECLARE @ag intEXEC AvgLast @ag = @ag OUTPUT, @dateA = '01Jan2005', @dateB = '31Dec2005', @prodSku = '123456'SELECT [@ag OUTPUT] = @ag Note that the end limit (@dateB) will only select up to the "time" of @dateB - if it only has a date, and no time, it will not select and values for SDate for that final date which themselves have a time.I've addedAND Qty IS NOT NULLto prevent any NULLs being processed (which would otherwise cause a warning to be send back through ADO which might muck up your application)Kristen |
|
|
rockmoose
SQL Natt Alfen
3279 Posts |
Posted - 2005-08-02 : 02:31:16
|
Hi Kristen, thanx for the company!I'm off fishing with the kids for a couple of days,it sure ain't going to be warm enough to go swimming :(So we'll fish and fight the mosquitoes.rockmoose |
|
|
|
|
|
|
|