Wrox Programmer Forums
|
VS.NET 2002/2003 Discussions about the Visual Studio.NET programming environment, the 2002 (1.0) and 2003 (1.1). ** Please don't post code questions here ** For issues specific to a particular language in .NET, please see the other forum categories.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VS.NET 2002/2003 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 20th, 2006, 04:04 AM
Registered User
 
Join Date: Mar 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default How To Use StreamWriter

Hello,

I am New @ Vb.Net and I have a problem in writing data in a notepad.
The problem is that,It should write the heading in the file only once,but instead it repeats it for each and every data I write.
eg it should write like this
Name Surname
Lucky Thami
Bebeza Slago
instead it writes
Name Surname
Lucky Thami
Name Surname
Bebeza Slago

Hope and trusting you can Understand the problem
Thx:)

LT Bebeza
 
Old March 20th, 2006, 04:30 AM
Wrox Technical Editor
 
Join Date: Dec 2005
Posts: 271
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I am a bit confused.
Have you already written the program?
If so, could you post the source code, some sample input and the desired output?

- A.Kahtava
 
Old March 20th, 2006, 04:32 AM
Wrox Technical Editor
 
Join Date: Dec 2005
Posts: 271
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Perhaps this link will help:

How To: Reading and Writing Text Files: http://www.csharp-station.com/HowTo/...eTextFile.aspx

- A.Kahtava
 
Old March 20th, 2006, 07:01 AM
Registered User
 
Join Date: Mar 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

 'Declare variable swrWrietr as streamwriter
        Dim swrWriter As IO.StreamWriter

        'Create a file
        swrWriter = IO.File.AppendText("Sample.txt")
        'Write Heading first only onec NB Don`t Repeate the heading
        swrWriter.WriteLine("Name " & Space(10) & "Surname")
        'write on the Name and Surname @ runtime
        swrWriter.WriteLine(txtName.Text & Space(10) & txtSurname.Text)
        swrWriter.Close()

LT Bebeza
 
Old March 23rd, 2006, 06:29 AM
Registered User
 
Join Date: Mar 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by Slago
  'Declare variable swrWrietr as streamwriter
        Dim swrWriter As IO.StreamWriter

        'Create a file
        swrWriter = IO.File.AppendText("Sample.txt")
        'Write Heading first only onec NB Don`t Repeate the heading
        swrWriter.WriteLine("Name " & Space(10) & "Surname")
        'write on the Name and Surname @ runtime
        swrWriter.WriteLine(txtName.Text & Space(10) & txtSurname.Text)
        swrWriter.Close()

LT Bebeza
 
Old March 23rd, 2006, 06:42 AM
Registered User
 
Join Date: Mar 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello LT Bebeza:
I think you have change the code as follows

  'Declare variable swrWrietr as streamwriter
        Dim swrWriter As IO.StreamWriter

        'Create a file
        swrWriter = IO.File.AppendText("Sample.txt")
        'Write Heading first only onec NB Don`t Repeate the heading
        swrWriter.WriteLine("Name " & Space(10) & "Surname")
 forecah(.....)
{
        'write on the Name and Surname @ runtime
        swrWriter.WriteLine(txtName.Text & Space(10) & txtSurname.Text)
}
        swrWriter.Close()


I hope it will solve your problem.

If you still you have problem then please provide the details where( in which method) you were writing above lines of code and how you are using ( calling written method).

Regards
Kristi.

Quote:
quote:Originally posted by Slago
  'Declare variable swrWrietr as streamwriter
        Dim swrWriter As IO.StreamWriter

        'Create a file
        swrWriter = IO.File.AppendText("Sample.txt")
        'Write Heading first only onec NB Don`t Repeate the heading
        swrWriter.WriteLine("Name " & Space(10) & "Surname")
        'write on the Name and Surname @ runtime
        swrWriter.WriteLine(txtName.Text & Space(10) & txtSurname.Text)
        swrWriter.Close()

LT Bebeza
 
Old March 23rd, 2006, 06:59 AM
Registered User
 
Join Date: Mar 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

the problem is that at Run time it Repeats The statement
swrWriter.writeline("Name " & Space(10) & "Surname")

Which it must not Repeat

LT Bebeza
 
Old April 4th, 2006, 03:32 PM
Authorized User
 
Join Date: Jun 2005
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Default

One weak solution cud be the usage of flag - a bool variable.
Donno the syntax of VB, but the logic is as follows:

static bool flag = false; //initialized somewhere
//in-between code
if(!flag)
{
    swrWriter.WriteLine("Name " & Space(10) & "Surname")
    flag = true;
}
//rest of the code

Hope it works.
-Shazia.







Similar Threads
Thread Thread Starter Forum Replies Last Post
Help Needed: StreamWriter Function OnceANewbie Visual Basic 2005 Basics 1 August 10th, 2009 05:50 AM
StreamWriter problem cheh Visual Basic 2008 Essentials 1 August 26th, 2008 03:18 AM
FileStream vs StreamWriter Makolyte C# 2005 0 June 22nd, 2007 01:22 PM
What object is streamwriter located under? kenn_rosie ASP.NET 1.0 and 1.1 Basics 0 February 21st, 2006 08:10 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.