StrataFrame Forum

Parameterized Store Procedures Failing

http://forum.strataframe.net/Topic25649.aspx

By Jeff Pagley - 1/29/2010

Hi SF Team,

I have just setup ES on a server and I began to do some testing.  The code (below) has been running for a couple of years on a local production server with a SQLExpress database, but when this same code (below) runs connecting through ES, I am getting an error (see below).  This piece of code is using a BO to run a parameterized stored procedure.  I am getting the same error when running other BOs using parameterized stored procredures.  It seems this error is associated with the BO.FillByStoredProcedure method.   What is going on?

Thanks,

Jeff

Public Sub FillByCallBackTop1ByCampaignIDAndUserIDAndDateRange(ByVal campaignID As Integer, ByVal userID As Integer, _

ByVal startDate As Date, ByVal endDate As Date, ByVal campaignMaximumCallTries As Integer)

Dim parms(4) As SqlParameter

parms(0) = New SqlParameter("@CampaignID", SqlDbType.Int)

parms(0).Value = campaignID

parms(1) = New SqlParameter("@UserID", SqlDbType.Int)

parms(1).Value = userID

parms(2) = New SqlParameter("@StartDate", SqlDbType.SmallDateTime)

parms(2).Value = startDate

parms(3) = New SqlParameter("@EndDate", SqlDbType.SmallDateTime)

parms(3).Value = endDate

parms(4) = New SqlParameter("@CampaignMaximumCallTries", SqlDbType.SmallInt)

parms(4).Value = campaignMaximumCallTries

Me.FillByStoredProcedure("CampaignContactsView_CallBack_Top_ByCampaignIDUserIDStartEndDate_Select", parms)

End Sub

=========================================================================================

Exception (EnterpriseDataSourceException)

Message = "An error occurred while executing the command on the remote data source. See the inner exception for more details."

InnerException (SqlException): Source=".Net SqlClient Data Provider"; Target=null; Tag=null; Class=16; LineNumber=0; Number=201; Server="TESTSERVER\SQLEXPRESS"; State=4; ErrorCode=-2146232060;

Message = "Procedure or Function 'CampaignContactsView_CallBack_Top1_ByCampaignIDUserIDStartEndDate_Select' expects parameter '@CampaignID', which was not supplied."

StackTrace =

" at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)

at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)

at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)

at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)

at System.Data.SqlClient.SqlDataReader.ConsumeMetaData()

at System.Data.SqlClient.SqlDataReader.get_MetaData()

at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)

at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)

at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior ... (truncated) ...xecuteReader(CommandBehavior behavior, String method)

at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)

at System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior)

at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)

at System.Data.Common.DbDataAdapter.Fill(DataTable[] dataTables, Int32 startRecord, Int32 maxRecords, IDbCommand command, CommandBehavior behavior)

at System.Data.Common.DbDataAdapter.Fill(DataTable dataTable)

at MicroFour.StrataFrame.Data.DbDataSourceItem.GetDataTable(DbCommand Command, OnChangeEventHandler CallBack)

at MicroFour.StrataFrame.Data.SqlDataSourceItem.GetDataTable(DbCommand Command, OnChangeEventHandler CallBack)

at MicroFour.StrataFrame.Data.Enterprise.RequestProcessor.ExecuteDataTable(ExecuteDataTableParams params)"

Errors (SqlErrorCollection): Count=1;

#0 = {System.Data.SqlClient.SqlError: Procedure or Function 'CampaignContactsView_CallBack_Top1_ByCampaignIDUserIDStartEndDate_Select' expects parameter '@CampaignID', which was not supplied.}

Procedure = "CampaignContactsView_CallBack_Top1_ByCampaignIDUserIDStartEndDate_Select"

Environment: ThreadIdentity="" DateTime=["2010-01-29 07:53:48.537"] ThreadName=null WindowsIdentity="STRUSS\JPagley" ThreadId="10" DomainName="ProspectHelper.vshost.exe" OSVersion=["Microsoft Windows NT 5.1.2600 Service Pack 3"]

MachineName="1LPJPAGLEY" UserName="JPagley"

By Edhy Rijo - 1/29/2010

Hi Jeff,



I have not used the ES or the method FillByStoredProcedure, here is how I fill the BO using store procedure:





Using cmd As New SqlCommand()

cmd.CommandType = CommandType.StoredProcedure

cmd.CommandText = "dbo.sp_FU_CheckDuplicateInvoice_FileName"



'-- Create and set the parameters

cmd.Parameters.AddWithValue("@TransactionType", Enumerations.TransactionType.FirstUseImport).SqlDbType = SqlDbType.SmallInt

cmd.Parameters.AddWithValue("@PK_Transaction", pPK_Transaction).SqlDbType = SqlDbType.Int

cmd.Parameters.AddWithValue("@FU_CarrierInvoiceNo", pCarrierInvoice_FileName).SqlDbType = SqlDbType.VarChar

cmd.Parameters.AddWithValue("@FU_ImportFileName", pCarrierInvoice_FileName).SqlDbType = SqlDbType.VarChar

cmd.Parameters.AddWithValue("@SearchByInvoice", pSearchByInvoice).SqlDbType = SqlDbType.Bit



'-- Execute the query

Me.FillDataTable(cmd)

End Using





Probable there is a problem with the FillByStoredProcedure but with above code I have never got any problem. Hope it can help you.
By Jeff Pagley - 1/29/2010

Hi Edhy,

That is a very easy code change to make.  I going to make the change and then test it.

Thanks,

Jeff

By Jeff Pagley - 1/29/2010

Hi Edhy,

Yeh..that worked and it was easy   I guess I was doing it the hard way.

I going to have to go back and refactor my code.

Thank You!

Jeff

By Edhy Rijo - 1/29/2010

Hi Jeff,



Glad it worked for you. For now I don't have the need for ES, but it is something that I may start using real soon.
By Trent L. Taylor - 1/30/2010

Thanks, Edhy! Glad you got it going, Jeff! Smile
By Keith Chisarik - 2/12/2010

I get a very similar error, code works fine when not using ES. I am using SF method ExecuteStoredProcedure to call the Sproc. The SPROC returns a single value that I capture by setting the parameter.direction = ParameterDirection.ReturnValue. How do I make this work using ES?

Error:

BusinessLayerException
  An error occurred while saving an the data to the server.
EnterpriseDataSourceException
  An error occurred while executing the command on the remote data source.  See the inner exception for more details.
SqlException
  Procedure or function 'GetNextWingForAcademy' expects parameter '@acadpk', which was not supplied.

Source     : MicroFour StrataFrame Business

Calling Code:

Dim retval As New SqlClient.SqlParameter()

retval.ParameterName = "@nextwing"

retval.Direction = ParameterDirection.ReturnValue

MicroFour.StrataFrame.Data.DataBasics.DataSources("").ExecuteStoredProcedure("GetNextWingForAcademy", _

DbCommandExecutionType.ExecuteScalar, New SqlClient.SqlParameter("@acadpk", p_acadpk), retval)

If Not retval.Value Is Nothing Then

r_nextwing = retval.Value

End If

By Greg McGuffey - 2/13/2010

Likely, this isn't it, but your sproc defines just one parameter right? @acadpk? I'm asking because it is more usual to use an OUTPUT parameter rather than the return value.
By Jeff Pagley - 2/13/2010

Hi Keith,

I have never used the ExecuteStoredProcedure command.   The one thing I see in common with both of our issues is the use of the SqlParameter object.  When I refactored my code to use the SQLCommand object and eliminated the use of the SqlParameter object, it fixed the problem for me. 

I hope this helps.

Jeff

By Trent L. Taylor - 2/13/2010

Keith,



Do you get the same error when using the ExecuteScalar instead of the ExecuteStoredProcecdure?



Dim cmd As New SqlCommand("dbo.GetNextWingForAcademy")

Dim retval As New SqlClient.SqlParameter()



'-- Set the command type

cmd.CommandType = SqlCommandType.StoredProcedure



retval.ParameterName = "@nextwing"

retval.Direction = ParameterDirection.ReturnValue



cmd.Parameters.Add(New SqlClient.SqlParameter("@acadpk", p_acadpk))

cmd.Parameters.Add(retval)



MicroFour.StrataFrame.Data.DataBasics.DataSources("").ExecuteScalar(cmd)



If Not retval.Value Is Nothing Then



r_nextwing = retval.Value



End If




I didn't run this code, but it should be close. Try running ExecuteScalar to see if you have any different results. This should work. We can discuss philosophy on this later, but let's start from here and we can see where we are.
By Keith Chisarik - 2/14/2010

Works fine when using ExecuteScalar as you posted Trent. I am not too concerned with the philosophy, but if you feel like giving a brief explanation of why one works and the other doesnt.... or why ExecuteScalar should be used over the other I'll take it. If not, I'm just happy to have something that works because I had this issue in quite a few places with after moving this app to ES.

Thanks as always.

Greg, your right it didnt end up being the problem but OUTPUT is propably technically the right return type to use, I knew it was always going to be a single integer return value so I felt ok using a return value. I may indeed refactor, thanks for the input.

By Trent L. Taylor - 2/14/2010

In short, ExecuteScalar is a direct shot to the data execution of the data layer with no other interpretation.  The SQL command is handed off and handled as is by the data layer whereas there is some interpretted logic in the ExecuteStoredProcedure (which is generally intended for more simple SPROCS). Generally speaking, I always use the ExecuteScalar, ExecuteNonQuery, etc. anytime that I want to execute a SPROC anyway as it is actually slightly better on the performance side of things.  At any rate, I am glad that you got it going.