Results 1 to 4 of 4

Thread: Parameters.Add() loop [RESOLVED]

Threaded View

  1. #1

    Thread Starter
    Fanatic Member drpcken's Avatar
    Join Date
    Apr 2004
    Location
    devenv
    Posts
    591

    Parameters.Add() loop [RESOLVED]

    Here's what I've got so far:

    I'm creating two parameters in a loop and filling them with a variable. Then after running my ExecuteNonQuery I emtpy the parameters with Parameters.Remove() and start the loop again, filling the parameters with new values, and executing.

    Now when I only loop once, it works fine. If the loop occurs more than once, and the Parameters.Remove() takes effect, I get an error:
    The SqlParameterCollection only accepts non-null SqlParameter type objects, not String objects.

    If I comment out my Parameters.Remove() lines, and only loop once, it works fine. Am I using the correct property of Parameter.Remove() to emtpy or remove these parameters? Like I said, everything works perfectly if I only loop once with one set of values for the Parameters and comment out Parameters.Remove(). It seems to me that its not removing the Parameter correctly and starting the loop over, and adding them again.

    Here's my code:

    VB Code:
    1. Dim sTeamLeaderID, sCounselorID As Integer
    2. Dim sqlConn As New SqlClient.SqlConnection("server=(local); uid=sa; pwd=;initial catalog=IFPAWEB")
    3. Dim sqlSPteam As New SqlClient.SqlCommand("sp_teamleadercounselor", sqlconn)
    4.  
    5. Dim i As Integer
    6.    
    7. sTeamLeaderID = CounselorsTeamLeaderName.Value
    8.      Try
    9.         sqlConn.Open()
    10.          For i = 0 To CounselorsCounselors.Items.Count - 1
    11.         If CounselorsCounselors.Items(i).Selected Then
    12.                 sCounselorID = CounselorsCounselors.Items(i).Value
    13.                    
    14.                    With sqlSPteam
    15.                        .CommandType = CommandType.StoredProcedure
    16.                        
    17.                        .Parameters.Add("@counselorid", Convert.ToInt32(sCounselorID))
    18.                .Parameters.Add("@teamleaderid", sTeamLeaderId)
    19.                .ExecuteNonQuery()
    20.                .Parameters.Remove("@counselorID")
    21.                .Parameters.Remove("@teamleaderid")
    22.                                                                        
    23.                    End With
    24.                        
    25.                  End If                                        
    26.               Next i
    27.      Finally
    28.     SqlConn.Close()
    29.      End Try

    Thanks!!
    Last edited by drpcken; Aug 24th, 2004 at 09:16 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width