?
Solved

How to write data to a file in VB.net

Posted on 2006-11-20
6
Medium Priority
?
179 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 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

777 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