StrataFrame Forum

Problem with sql parameters when using FillByStoredProcedure

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

By Peter Jones - 5/30/2007

Hi,

We have the following code which refreshes all combo boxes on a form (DevExpress BTW) when the form goes into add new record mode. It makes sure the user only sees records in combo boxes that are currently 'active'.

When the code executes it throws the exception: {"The SqlParameter is already contained by another SqlParameterCollection."} in the .FillByStoredProcedure call. In researching this problem it seems as though the problem pops up a fair bit with the most common solution being to execute a cmd.Parameters.Clear() or something similar after the proc executes.

Apparently the error means exactly what it says however, in my case, I'm not so sure. I've rebooted both the data server backend and my workstation but it remains - I would have thought it would execute once then not again until the form was closed and reopened. However, it has worked at least once (maybe only once - I'm not sure now) but right now it errors every time the code is run, i.e. even after a reboot.

Obviously taking the "cmd.Parameters.Clear()" approach would mean doing something in the StrataFrame sources which we are not about to do so, any comments or suggestions on how to get around this problem would be appreciated.

Cheers, Peter

>>>>>>>>>>>>>>>>>>>>>>>>

    Public Shared Sub RefreshCombosForNewRow(ByVal BOs As MicroFour.StrataFrame.Business.BusinessObjectCollection)

        ' Right now we are just starting a new row so we want to make sure any lookup stored procs
        ' are refreshed using a parameter to only select active records.

        Dim ProcParam As System.Data.SqlClient.SqlParameter
        ProcParam = New System.Data.SqlClient.SqlParameter
        With ProcParam
            .ParameterName = "@Active"
            .DbType = Data.DbType.Int32
            .Direction = Data.ParameterDirection.Input
            .Value = 1
        End With

        ' Cycle through all BO's on the form. If they have insert proc name then the BO is for lookups and
        ' we need to refresh the combo box with just active records.
        For i As Integer = 0 To BOs.Count - 1
            If BOs.Item(i).InsertStoredProcedureName.ToString.Length >= 1 Then
                BOs.Item(i).FillByStoredProcedure(BOs.Item(i).InsertStoredProcedureName.ToString, ProcParam)
            End If
        Next
    End Sub
>>>>>>>>>>>>>>>>>>>>>>>>>>>>

By Peter Jones - 6/4/2007

Hi Guys,

You may have missed this post and one from Chan on the same day - no response as yet.

Cheers, Peter

By Peter Jones - 6/4/2007

Hi Guys,

No worries with this - my mate Pete has put me right. Simple really, declare the para within the If and get a new one for each iteration:

        For i As Integer = 0 To BOs.Count - 1
            If BOs.Item(i).InsertStoredProcedureName.ToString.Length >= 1 Then

                 Dim ProcParam As System.Data.SqlClient.SqlParameter
                 ProcParam = New System.Data.SqlClient.SqlParameter
                 With ProcParam
                    .ParameterName = "@Active"
                    .DbType = Data.DbType.Int32
                    .Direction = Data.ParameterDirection.Input
                    .Value = 1
                End With
                BOs.Item(i).FillByStoredProcedure(BOs.Item(i).InsertStoredProcedureName.ToString, ProcParam)
            End If
        Next

Cheers, Peter

By Trent L. Taylor - 6/4/2007

Yeah....looks like you found your answer Smile  Sorry for the missed post.