Solved

How to write data to a file in VB.net

Posted on 2006-11-20
6
177 Views
Last Modified: 2010-04-23
Hi!

Could someone please look at my code and tell me what am I doing wrong?
Even thou iCount gives me a correct number of records and loops thru an if
statement correctly it still displays the same message no matter what (makes no difference if iCount = 0 or iCount > 0) it shows the same message -- All Accounts were submitted for processing -- which is incorrect since
when I tested my application I know that it should display certain number of records...

On the other hand, if I try to switch my “if statement” around it displays a blank page instead…

Thanks!
...
 Dim iCount As Integer
 iCount = rs.Tables(0).Rows.Count

            If iCount > 0 Then

                txtFile.WriteLine("" & vbCrLf)
                txtFile.WriteLine("All Accounts were submitted for processing." & vbCrLf)

            Else
               
                txtFile.WriteLine("" & vbCrLf)
                txtFile.WriteLine("RTP No" & vbTab & "Account No" & vbTab & "Amount" & vbCrLf)
               
                For i As Integer = 0 To iCount - 1
                    txtFile.WriteLine(rs.Tables(0).Rows(i).Item("RTPNo") & vbTab & rs.Tables(0).Rows(i).Item("AccountNo") & vbTab & rs.Tables(0).Rows(i).Item("Amount") & vbCrLf)
                Next i


            End If
0
Comment
Question by:avi4
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 13

Expert Comment

by:newyuppie
ID: 17982152
just a few things:
1) it doesnt make sense that in your ELSE (if count is 0, aka no records) you loop For i as Integer = 0 to iCount -1. iCount is obviously 0 here, so why would you loop from 0 to 0?

2) maybe post the code you have on your (...). where is this code, in an event handler of some kind? im thinking maybe this piece of code, depending on where you put it, is triggering more than once. and the final trigger overwrites whatever you did before.

NY
0
 
LVL 6

Assisted Solution

by:riyazthad
riyazthad earned 50 total points
ID: 17983407
I THINK YOU ARE LOOKING FOR THIS LOGIC.

Dim iCount As Integer
 iCount = rs.Tables(0).Rows.Count

            If iCount = 0 Then

                txtFile.WriteLine("" & vbCrLf)
                txtFile.WriteLine("All Accounts were submitted for processing." & vbCrLf)

            Else
               
                txtFile.WriteLine("" & vbCrLf)
                txtFile.WriteLine("RTP No" & vbTab & "Account No" & vbTab & "Amount" & vbCrLf)
               
                For i As Integer = 0 To iCount - 1
                    txtFile.WriteLine(rs.Tables(0).Rows(i).Item("RTPNo") & vbTab & rs.Tables(0).Rows(i).Item("AccountNo") & vbTab & rs.Tables(0).Rows(i).Item("Amount") & vbCrLf)
                Next i


            End If
0
 

Author Comment

by:avi4
ID: 17988591
Hi,
Thanks for your comments but I still get a blank page...and I can't understand why?!
Here is more of my code...
Thanks!

 Public Sub Report()

        Dim sql As String
        Dim rs As New DataSet
        Dim txtFile As StreamWriter
        Dim tempdate As String
        Dim thelockbox As String
        Dim UseDateX As String

       Try

            Dim objConn As New SqlConnection("server=***;database=***;uid=***;pwd=***")
            thelockbox = XType
            UseDateX = Now.ToShortDateString
            tempdate = theDateX

            sql = "select Account_No, Amount, Tran_Date " & _
                   "from Customer " & _
                   "where Account_No='" & MstrAcct & "' and status<>1 "

  txtFile = New StreamWriter("\\...\...\Reports\" & "Report_" & tempdate & ".txt", True)

  objConn.Open()

            Try

               Dim da As SqlDataAdapter = New SqlDataAdapter(sql, objConn)

                da.SelectCommand.CommandTimeout = 0
                'fill dataset
                da.Fill(rs)

            Catch ex As Exception
                MsgBox("Error.")
            End Try

            Dim iCount As Integer
            iCount = rs.Tables(0).Rows.Count

            If iCount > 0 Then

                txtFile.WriteLine("" & vbCrLf)
                txtFile.WriteLine("All Accounts were submitted for processing." & vbCrLf)

            Else
               
                txtFile.WriteLine("" & vbCrLf)
                txtFile.WriteLine("Account No " & vbTab & "Amount " & vbTab & "Transaction Date" & vbCrLf)

                For i As Integer = 0 To iCount - 1
                   
                    txtFile.WriteLine(rs.Tables(0).Rows(i).Item("Account_No") & vbTab & rs.Tables(0).Rows(i).Item("Amount") & vbTab & vbTab & GR3rs.Tables(0).Rows(i).Item("Tran_Date") & vbCrLf)

                Next i

            End If

               'txtFile.Close()

        Catch ex As Exception

         Finally

            'txtFile.Close()
            txtFile = Nothing
            rs = Nothing
     
       End Try
 End Sub

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Assisted Solution

by:Joe_Griffith
Joe_Griffith earned 225 total points
ID: 17988809
When you say blank page you are talking about the file right?
Have you tried uncommenting the close statements?
0
 
LVL 13

Accepted Solution

by:
newyuppie earned 225 total points
ID: 17989087
as joe says, try uncommenting the txtFile close, add a txtFile.Flush() before the close, and consider riyazthad comment on changing your code.
0
 

Author Comment

by:avi4
ID: 17989800

Thanks so much! It's working now... :))      
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question