Link to home
Start Free TrialLog in
Avatar of Ilianam
Ilianam

asked on

Loop fot this

I have this
myCommand = New SqlClient.SqlCommand("MDAddClaimServiceDates", myConnection)
                    myCommand.CommandType = CommandType.StoredProcedure
                    With myCommand
                        .Parameters.Add("@DenialID", SqlDbType.Int)
                        .Parameters.Add("@SrvcStartDt", SqlDbType.SmallDateTime)
                        .Parameters.Add("@SrvcEndDt", SqlDbType.SmallDateTime)
                        .Parameters.Add("@SrvcDateID", SqlDbType.Int)
                        .Parameters("@DenialID").Value = DenialID
                        .Parameters("@SrvcStartDt").Value = txtService1.Text
                        .Parameters("@SrvcEndDt").Value = txtService1.Text
                        .Parameters("@SrvcDateID").Direction = ParameterDirection.Output

                        .ExecuteNonQuery()
                        ServiceID = (.Parameters("@SrvcDateID").Value)
                        If txtCPT1.Text <> "" Then
                            myCommand = New SqlClient.SqlCommand("MDAddClaimService", myConnection)
                            myCommand.CommandType = CommandType.StoredProcedure
                            With myCommand
                                .Parameters.Add("@DenialID", SqlDbType.Int)
                                .Parameters.Add("@SrvcDateID", SqlDbType.Int)
                                .Parameters.Add("@CptRevCd", SqlDbType.NVarChar)
                                .Parameters("@DenialID").Value = DenialID
                                .Parameters("@SrvcDateID").Value = ServiceID
                                .Parameters("@CptRevCd").Value = txtCptRev1.Text
                                .ExecuteNonQuery()

                            End With
I need to repeat the same code for 6 diferent textboxes, i.e: txtService2....txtService6
Is there a way to put the sme code within a for loop?
Avatar of maidinhtai
maidinhtai

That's easy. What you have to do is create a routine like this:
Private Sub RoutineName(ByVal txtService1 as textbox)
myCommand = New SqlClient.SqlCommand("MDAddClaimServiceDates", myConnection)
                    myCommand.CommandType = CommandType.StoredProcedure
                    With myCommand
                        .Parameters.Add("@DenialID", SqlDbType.Int)
                        .Parameters.Add("@SrvcStartDt", SqlDbType.SmallDateTime)
                        .Parameters.Add("@SrvcEndDt", SqlDbType.SmallDateTime)
                        .Parameters.Add("@SrvcDateID", SqlDbType.Int)
                        .Parameters("@DenialID").Value = DenialID
                        .Parameters("@SrvcStartDt").Value = txtService1.Text
                        .Parameters("@SrvcEndDt").Value = txtService1.Text
                        .Parameters("@SrvcDateID").Direction = ParameterDirection.Output

                        .ExecuteNonQuery()
                        ServiceID = (.Parameters("@SrvcDateID").Value)
                        If txtCPT1.Text <> "" Then
                            myCommand = New SqlClient.SqlCommand("MDAddClaimService", myConnection)
                            myCommand.CommandType = CommandType.StoredProcedure
                            With myCommand
                                .Parameters.Add("@DenialID", SqlDbType.Int)
                                .Parameters.Add("@SrvcDateID", SqlDbType.Int)
                                .Parameters.Add("@CptRevCd", SqlDbType.NVarChar)
                                .Parameters("@DenialID").Value = DenialID
                                .Parameters("@SrvcDateID").Value = ServiceID
                                .Parameters("@CptRevCd").Value = txtCptRev1.Text
                                .ExecuteNonQuery()
                            End With
End Sub
And call the routine in the loop.
Avatar of YZlat
Dim i as Integer
For i=1 To 6
      Dim tb as New textBox
      tb=FindControl("txtService" & i)
      Dim tb2 as New textBox
      tb2=FindControl("txtCptRev" & i)
      myCommand = New SqlClient.SqlCommand("MDAddClaimServiceDates", myConnection)
        myCommand.CommandType = CommandType.StoredProcedure
                    With myCommand
                        .Parameters.Add("@DenialID", SqlDbType.Int)
                        .Parameters.Add("@SrvcStartDt", SqlDbType.SmallDateTime)
                        .Parameters.Add("@SrvcEndDt", SqlDbType.SmallDateTime)
                        .Parameters.Add("@SrvcDateID", SqlDbType.Int)
                        .Parameters("@DenialID").Value = DenialID
                        .Parameters("@SrvcStartDt").Value = tb.Text
                        .Parameters("@SrvcEndDt").Value = tb.Text
                        .Parameters("@SrvcDateID").Direction = ParameterDirection.Output

                        .ExecuteNonQuery()
                        ServiceID = (.Parameters("@SrvcDateID").Value)
                        If txtCPT1.Text <> "" Then
                            myCommand = New SqlClient.SqlCommand("MDAddClaimService", myConnection)
                            myCommand.CommandType = CommandType.StoredProcedure
                            With myCommand
                                .Parameters.Add("@DenialID", SqlDbType.Int)
                                .Parameters.Add("@SrvcDateID", SqlDbType.Int)
                                .Parameters.Add("@CptRevCd", SqlDbType.NVarChar)
                                .Parameters("@DenialID").Value = DenialID
                                .Parameters("@SrvcDateID").Value = ServiceID
                                .Parameters("@CptRevCd").Value = tb2.Text
                                .ExecuteNonQuery()

                            End With
Next
Avatar of Ilianam

ASKER

It's FindControl for windows or web forms?
FindControl works for both, doesn't it?
Actually no, there is no FindControl for Windows forms. Try this instead

Dim i as Integer
Dim strCPT as String
For i=1 To 6
     myCommand = New SqlClient.SqlCommand("MDAddClaimServiceDates", myConnection)
        myCommand.CommandType = CommandType.StoredProcedure
                    With myCommand
                        .Parameters.Add("@DenialID", SqlDbType.Int)
                        .Parameters.Add("@SrvcStartDt", SqlDbType.SmallDateTime)
                        .Parameters.Add("@SrvcEndDt", SqlDbType.SmallDateTime)
                        .Parameters.Add("@SrvcDateID", SqlDbType.Int)
                        .Parameters("@DenialID").Value = DenialID
                        .Parameters("@SrvcStartDt").Value = Me.Controls.Item(col.Item("txtService" & i)).Text
                        .Parameters("@SrvcEndDt").Value = Me.Controls.Item(col.Item("txtService" & i)).Text
                        .Parameters("@SrvcDateID").Direction = ParameterDirection.Output

                        .ExecuteNonQuery()
                        ServiceID = (.Parameters("@SrvcDateID").Value)
                       
                       strCPT=Me.Controls.Item(col.Item("txtCPT" & i)).Text
                        If strCPT<> "" Then
                            myCommand = New SqlClient.SqlCommand("MDAddClaimService", myConnection)
                            myCommand.CommandType = CommandType.StoredProcedure
                            With myCommand
                                .Parameters.Add("@DenialID", SqlDbType.Int)
                                .Parameters.Add("@SrvcDateID", SqlDbType.Int)
                                .Parameters.Add("@CptRevCd", SqlDbType.NVarChar)
                                .Parameters("@DenialID").Value = DenialID
                                .Parameters("@SrvcDateID").Value = ServiceID
                                .Parameters("@CptRevCd").Value = Me.Controls.Item(col.Item("txtCptRev" & i)).Text
                                .ExecuteNonQuery()

                            End With
Next
ASKER CERTIFIED SOLUTION
Avatar of YZlat
YZlat
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Ilianam

ASKER

it gives a lot of errors
what errors?
Avatar of Ilianam

ASKER

all were related to the controls