Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to write data to a file in VB.net

Posted on 2006-11-20
6
Medium Priority
?
180 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 200 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
Industry Leaders: 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 900 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 900 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

824 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