Solved

How to write data to a file in VB.net

Posted on 2006-11-20
6
176 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
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

840 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