Problem to the loop

Hi,
to the following codes
        Dim sql As String = String.Format("select * from {0}.sun_accountcode_map", KTS_REPORTS_DBNAME) &
                                          If(String.IsNullOrEmpty(documentCode), "", String.Format(" where documentcode='{0}'", documentCode))
        Dim data As DataTable = GetDataTable(sql, theConn)
        For Each row As DataRow In data.Rows
            Dim theItem As New SunAccountCodeItem
            theItem.DataRowToProperty(row)
            Using writer As StreamWriter = New StreamWriter("c:\\xxe.txt")
                writer.WriteLine(theItem.AnalysisValue + " ee " + theItem.DocumentCode)
            End Using
            Me.Add(theItem)
        Next

Open in new window

If I directly run the relevant sql (captured in 1st line above), I get nearly 700 records in the DB. But I only get 1 line in the text file to the above loop. why?
LVL 12
HuaMin ChenProblem resolverAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
because you overwrite the targeted file in your loop... try this instead:

        Dim sql As String = String.Format("select * from {0}.sun_accountcode_map", KTS_REPORTS_DBNAME) &
                                          If(String.IsNullOrEmpty(documentCode), "", String.Format(" where documentcode='{0}'", documentCode))
        Dim data As DataTable = GetDataTable(sql, theConn)

Using writer As StreamWriter = New StreamWriter("c:\\xxe.txt")

        For Each row As DataRow In data.Rows
            Dim theItem As New SunAccountCodeItem
            theItem.DataRowToProperty(row)            
                writer.WriteLine(theItem.AnalysisValue + " ee " + theItem.DocumentCode)
            Me.Add(theItem)
        Next

   End Using

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PortletPaulfreelancerCommented:
You appear to be overwriting the file on each iteration

the file should be defined once, and then just added to in the loop
0
AndyAinscowFreelance programmer / ConsultantCommented:
Didn't I tell you that a few questions ago?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.