Solved

How to write data to a file in VB.net

Posted on 2006-11-20
6
173 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 7

Assisted Solution

by:Joe_Griffith
Joe_Griffith earned 225 total points
Comment Utility
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
Comment Utility
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
Comment Utility

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

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This video discusses moving either the default database or any database to a new volume.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now