Loop not working properly RRS feed

  • Question

  • User253958130 posted

    I know that this is very trivial but i am having problem with this one. What is wrong with my loop? Most the times it just loops once instead of n number of times(which is equal to the number of rows in my grid). Thanks in advance for all for the help.

    Public Shared Sub uploadImages(ByVal imagesGrid As Camstar.WebClient.FormsBuilder.WebGridControls.WebGrid, ByVal DCDStep As String, ByVal DefectID As String, ByVal DCDName As String, ByVal DCDTableId As String)
            Dim rowCount As Integer = imagesGrid.Rows.Count
            Dim conn As SqlConnection
            Dim cmd As SqlCommand
            Dim obj As New myCommon
            'loop as many times as the number of rows in the grid
            Dim index As Integer
            For index = 0 To rowCount - 1
                    'Create Command Object
                    cmd = New SqlCommand
                    conn = New SqlConnection
                    conn.ConnectionString = obj.GetConnectionString()
                    cmd.Connection = conn
                    cmd.CommandText = "INSERT INTO AGDCDFileHistory (DCDFile,DCDStep, DCDFileHistoryId,DCDDefectFK,DCDName,DCDTableId) VALUES (@DCDFile,@DCDStep, @DCDFileHistoryId,@DCDDefectFK,@DCDName,@DCDTableId)"
                    Dim BLOB() As Byte
                    Dim filestream As New FileStream(imagesGrid.Rows(index).Cells(1).Text, FileMode.Open, FileAccess.Read)
                    Dim reader As New IO.BinaryReader(filestream)
                    BLOB = reader.ReadBytes(CInt(My.Computer.FileSystem.GetFileInfo(imagesGrid.Rows(index).Cells(1).Text).Length))
                    Dim DCDFileParameter As New SqlParameter("@DCDFile", SqlDbType.VarBinary)
                    Dim DCDStepParameter As New SqlParameter("@DCDStep", SqlDbType.NVarChar, 255)
                    Dim DCDFileHistoryIDParameter As New SqlParameter("@DCDFileHistoryID", SqlDbType.Char, 16)
                    Dim DCDDefectFKParameter As New SqlParameter("@DCDDefectFK", SqlDbType.Char, 16)
                    Dim DCDNameParameter As New SqlParameter("@DCDName", SqlDbType.NVarChar, 255)
                    Dim DCDTableIdParameter As New SqlParameter("@DCDTableId", SqlDbType.NVarChar, 16)
                    'Read File into a byte array
                    DCDFileParameter.Value = BLOB
                    DCDStepParameter.Value = DCDStep
                    DCDFileHistoryIDParameter.Value = Now.Year & Now.Month & Now.Day & Now.Hour & Now.Minute & Now.Second & Now.Millisecond
                    DCDDefectFKParameter.Value = DefectID
                    DCDNameParameter.Value = DCDName
                    DCDTableIdParameter.Value = DCDTableId
                    imagesGrid.Rows(index).Cells(2).Text = index
                Catch ex As Exception
                    ' Close the database connection if it is still open.
                    If (cmd.Connection.State = ConnectionState.Open) Then
                    End If
                    cmd = Nothing
                    conn = Nothing
                End Try
        End Sub



    Sunday, September 19, 2010 9:04 AM

All replies

  • User-1148431695 posted


    To me the loop seems to be ok. How many rows are there in your grid? If there is only 1 row, then the loop should execute only once.

    Also, instead of For Next can you try to use For Each Next loop?

    Sunday, September 19, 2010 9:38 AM
  • User253958130 posted

    Using For Each-Next loop is not making any difference.

    I have noticed that it loops correct number of times if i step through the code in debug mode. That is why i am unable to catch the bug.

    I would really appreciate any help. Thank You

    Sunday, September 19, 2010 5:59 PM
  • User253958130 posted

    Finally resolved the problem. The problem was that i was taking current date and time in milliseconds as PK for the table. So, the inserts and the loop was okay when i was in debug mode since things were slow at that time. When i was not in debug mode then many times there were duplicate PKs that led to exception. So, now everything is working fine. 

    Thanks for all you help.

    Sunday, September 19, 2010 7:23 PM
  • User-558580128 posted

    hi, what was your resolution when you found out that that was the problem?

    Monday, September 20, 2010 3:52 AM