vs 2005 doubling controls and For Next Loops

Last post 02-17-2007 8:32 AM by scottt40. 2 replies.

Sort Posts:

  • vs 2005 doubling controls and For Next Loops

    02-15-2007, 8:52 PM
    • Member
      687 point Member
    • scottt40
    • Member since 09-05-2002, 2:26 PM
    • CT
    • Posts 175

    I have noticed for some time now that when I go to select a control while in a sqlDataSource Wizard as a variable that it shows more than one control with the same name.  I am also having a problems with for next loops doubling the data inserted into database tables.  What might cause this?  I am running Windows Server 2003 and VS2005 with SQL 2005 on the same machine.  I remotely write my code from a Windows XP machine. 

    Here is the for next loop:

    Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs)
            Dim StudentUserName As String
    
    
            For i As Integer = 0 To GridView1.Rows.Count - 1
                Dim cb As CheckBox = CType(GridView1.Rows(i).FindControl("StudentCb"), CheckBox)
    
    
                'If it's checked, delete it...
                If cb.Checked Then
                    'Programmatically access the CheckBox from the TemplateField
                    Dim StepsTb As TextBox = CType(GridView1.Rows(i).FindControl("StepsTb"), TextBox)
    
                    Dim Grade As Integer = 0
                    If Not StepsTb.Text = "0" Then
                        Grade = StepsTb.Text
                    End If
                    Profile.Score = Grade
                    'If it's checked, delete it...
    
                    StudentUserName = GridView1.DataKeys(i).Value
                    Profile.StudentUserName = StudentUserName
    
                    If Page.IsValid Then
                        Try
                            InsertDs.Insert()
                            'ErrorLb.Text += Grade & " " & StudentUserName & " " & DateTextBox.Text & " " & ClassTb.SelectedValue & "</br>"
    
                        Catch ex As Exception
                            ErrorLb.Text += "The following error occured while trying to update the data: " & ex.ToString() & "<br/>"
                            ErrorLb.ForeColor = Drawing.Color.Red
    
                        End Try
    
                    End If
                End If
    
            Next
    
    
            'ErrorLb.Text = "Your information was entered correctly. <br/> "
            ErrorLb.ForeColor = Drawing.Color.Green
            ErrorLb.Font.Bold = True
    
            GridView2.DataBind()
            FormView1.DataBind()
            FormView2.DataBind()
        End Sub
    
    
    The code is designed to loop through a templated field's text box on a gridview and enter this information into sql if a check box is checked. 
    Thanks Scott
  • Re: vs 2005 doubling controls and For Next Loops

    02-16-2007, 9:22 PM
    • Star
      13,726 point Star
    • JoshStodola
    • Member since 01-16-2007, 9:17 AM
    • Heartland of America
    • Posts 3,177

    Try renaming this variable:

    Dim StudentUserName As String

    Dim strStudentUserName As String

    Of course you will have to rename it throughout the rest of the subroutine also.

    Hope this helps!  Please mark the most helpful post as "Answer"

    Josh Stodola ← Come check out my blog!
  • Re: vs 2005 doubling controls and For Next Loops

    02-17-2007, 8:32 AM
    • Member
      687 point Member
    • scottt40
    • Member since 09-05-2002, 2:26 PM
    • CT
    • Posts 175
    I problem was that the score integer was being sent outside the If then statement, and I had placed a text value of 0 in each text box in the gridview control's template which the score value was derived from.  By removing the value from the textbox and moving the score inside the if then statement the problem was solved.
    Thanks Scott
Page 1 of 1 (3 items)