Click here to Skip to main content
15,901,957 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello friends,
I was using command object to perform insert.
I'm using same object for two different purpose
1. First it executes StoredProcedure<br />
2. Second it executes simple text<br />


This all happens in loop, first time it works fine but in second turn it gives error:
Too many parameters

I paste down code for your refrence

Public Function saveDetails(ByVal Clientid As Integer, ByVal Conditions As String) As Integer
      Dim strArr As String()
      Dim intRecordid As Integer
      Dim strQuery As String
      Dim flag As Boolean = False

      Dim objConn As SqlConnection = getConnection()
      Dim objCMD As New SqlCommand("usp_saveTNC_save", objConn)
      Dim sp As SqlParameter

      strArr = Conditions.Split(",")
      Dim strArrVal As String = ""
      Try
          objCMD.Connection = objConn
          If objConn.State = ConnectionState.Closed Then
              objConn.Open()
          End If

          For i As Integer = 0 To strArr.Length - 1
              'SAVE RECORD IN tbl_TConditions .:[BEGIN]:.
              objCMD.CommandText = "usp_saveTNC_save"
              objCMD.CommandType = CommandType.StoredProcedure
              strArrVal = strArr(i)
              objCMD.Parameters.AddWithValue("@condition", strArrVal)
              If flag = False Then
                  sp = New SqlParameter("@id", SqlDbType.Int)
                  sp.Direction = ParameterDirection.Output
                  objCMD.Parameters.Add(sp)
                  flag = True
              End If

              objCMD.ExecuteNonQuery()
              intRecordid = sp.Value.ToString()
              'SAVE RECORD IN tbl_TConditions .:[END]:.

              'SAVE RECORD IN tbl_TConditions_client .:[BEGIN]:.
              strQuery = "INSERT INTO tbl_TConditions_client VALUES(" & intRecordid & "," & Clientid & ")"
              objCMD.CommandText = strQuery.ToString
              objCMD.CommandType = CommandType.Text
              objCMD.ExecuteNonQuery()
              'SAVE RECORD IN tbl_TConditions_client .:[END]:.

          Next
          Return 1
      Catch ex As Exception
          Return -1
      Finally
          objConn.Dispose()
          objConn.Close()
          objCMD.Dispose()
          objCMD = Nothing
      End Try
  End Function


Any idea, how to resolve this?

Thanks in advance
Posted

This is because you are not clearing the parameters for each iteration. Try adding objCMD.Parameters.Clear() above the Next
 
Share this answer
 
Comments
Toniyo Jackson 5-Aug-11 5:35am    
Correct +5. I given him the alternate solution.
walterhevedeich 5-Aug-11 8:36am    
Thanks Toniyo.
dhage.prashant01 5-Aug-11 7:16am    
thanks, resloved
You can follow walterhevedeich's solution. Or Declare Dim sp As SqlParameter inside the For loop.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900