Results 1 to 4 of 4

Thread: Parameters.Add() loop [RESOLVED]

  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.

  2. #2
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416
    why wont u use parameters.clear instead of parameters.remove???

  3. #3
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    You now have two threads with exactly the same question.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  4. #4

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

    I tried parameter.clear() and used each parameter as an arguement. Then I realized I only had to write it once with no parameters to clear out each filled parameter

    Thanks alot man!!

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