Maggie Ralph
Maggie Ralph

Reputation: 9

My StreamReader ReadLine is reading every other line

I am looping through a text file and reading and then parsing each line.. then, inserting into a sql server. The problem is, I am getting every other line. Any thoughts?

I read another post on here that is similar and it says that that person was calling the ReadLine twice... which makes sense. I just cannot spot where it is happening.

Private Sub btnUpdateRSR_Click(sender As System.Object, e As System.EventArgs) Handles btnUpdateRSR.Click


        ' Get RSR File
        lblStatus.Text = "Getting RSR File"
        Application.DoEvents()

        If chkDLRSR.Checked = True Then
            ftpGetRSR()
        End If

        Application.DoEvents()
        lblStatus.Text = "Got RSR File"

        ' Whack existing data

        killRSRData()


        ' Run Update of Invnetory

        Dim sCmd As New SqlCommand()
        Dim fp As StreamReader
        Dim MyFile As String = String.Empty
        Dim dblRecordCount As Double


        Try
            'Open file with StreamReader
            fp = File.OpenText("c:\ct_info\rsr.txt")
        Catch err As Exception
            lblStatus.Text = "File Read Failed! Reason: " & err.ToString()
        End Try
        sCmd.Connection = New System.Data.SqlClient.SqlConnection("Data Source=vortx-sql01.vortx.com;Initial Catalog=expresspolicesupply;Persist Security Info=True;User ID=expresspolicesupply;Password=blahblah")
        sCmd.Connection.Open()
        Dim strArr() As String

        While String.IsNullOrEmpty(fp.ReadLine) = False ' fp.ReadLine.IsNullOrEmpty = False
            MyFile = fp.ReadLine


            strArr = MyFile.Split(";")
            'Show what is in the second position of the array.
            'MessageBox.Show(strArr(1))
            Try
                Dim RSRstocknumber As String = strArr(0)
                Dim UPCcode As String = strArr(1)
                Dim ProductDescription As String = Replace(strArr(2), "'", """")
                Dim DepartmentNumber As String = strArr(3)
                Dim ManufacturerID As String = strArr(4)
                Dim RetailPrice As String = strArr(5)
                Dim RSRRegularPrice As String = strArr(6)
                Dim Weight As String = strArr(7)
                Dim InventoryQuantity As String = strArr(8)
                Dim Model As String = Replace(strArr(9), "'", """")
                Dim FullManufacturerName As String = Replace(strArr(10), "'", """")
                Dim ManufacturerPartNo As String = strArr(11)
                Dim AllocatedCloseoutDeleted As String = strArr(12)
                Dim ExpandedProductDescription As String = Replace(strArr(13), "'", """")
                Dim ImageName As String = strArr(14)



                lblStatusPrevious.Text = "Previous one: " & lblStatus.Text
                lblStatus.Text = strArr(0)
                Application.DoEvents()





                With sCmd
                    .CommandText = "INSERT into rsr (rsrstocknumber, upccode, productDescription, departmentnumber, ManufacturerID, RetailPrice, RSRRegularPrice, Weight, InventoryQuantity, Model, FullManufacturerName, ManufacturerPartNo, AllocatedCloseoutDeleted, ExpandedProductDescription, ImageName) " & _
                        " Values('" & RSRstocknumber & "', '" & UPCcode & "', '" & ProductDescription & "', '" & DepartmentNumber & "', '" & ManufacturerID & "', '" & _
                RetailPrice & "', '" & RSRRegularPrice & "', '" & Weight & "', '" & InventoryQuantity & "', '" & Model & "', '" & FullManufacturerName & "', '" & ManufacturerPartNo & "', '" & _
                AllocatedCloseoutDeleted & "', '" & ExpandedProductDescription & "','" & ImageName & "');"
                    'MessageBox.Show(sCmd.CommandText.ToString)
                    .CommandType = CommandType.Text
                    .ExecuteNonQuery()

                    ' Update record counter

                    dblRecordCount = dblRecordCount + 1
                    lblRecordCount.Text = dblRecordCount
                    Application.DoEvents()



                End With
            Catch ex As Exception
                lblErrorLabel.Text = "Error on thown on " & lblRecordCount.Text
                'MessageBox.Show("Error occurred! Details: " & ex.Message)
            End Try
        End While
        fp.Close()          'Close file with StreamReader
        sCmd.Connection.Close()

        lblStatus.Text = "Updating website for RSR"
        lblStatusPrevious.Text = ""

        spUpdateRSR()



        lblStatus.Text = "Done"
        lblStatusPrevious.Text = ""


    End Sub

Upvotes: 0

Views: 2229

Answers (3)

This line is reading the file, and then you read the next one in the while loop:

While String.IsNullOrEmpty(fp.ReadLine) = False

You are calling it twice :)

You need to use this to check whether it is the end of the file:

While fp.EndOfStream = False 

Upvotes: 1

Ste
Ste

Reputation: 1136

You are calling ReadLine twice.

 While String.IsNullOrEmpty(fp.ReadLine) = False ' fp.ReadLine.IsNullOrEmpty = False
        MyFile = fp.ReadLine

Once in the IsNullOrEmpty check and once on MyFile = fp.ReadLine

Upvotes: 1

SLaks
SLaks

Reputation: 888213

That's because you're calling ReadLine() twice.

Every other line gets swallowed by String.IsNullOrEmpty(fp.ReadLine) = False.

Upvotes: 1

Related Questions