Help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jay

    #1

    Help

    I am getting an error on my save dialog. No matter if I try and save a file
    or cancel out of it. Below is what I have code.

    Dim textstreamwrite r As StreamWriter

    Dim responsedialogr esults As DialogResult


    SaveFileDialog1 .InitialDirecto ry = Application.Sta rtupPath

    responsedialogr esults = SaveFileDialog1 .ShowDialog

    textstreamwrite r.WriteLine(rtb FontBox.Text)

    textstreamwrite r.Flush()

    textstreamwrite r.Close()

    If responsedialogr esults = DialogResult.Ca ncel Then

    textstreamwrite r.Close()

    End If


  • Saurabh Nandu

    #2
    RE: Help

    Hi,

    What is the error you are getting? And on which line is the error being
    indicated?

    Also it might be a good idea to write your code as below, os that it only
    writes to the Stream if the Ok button has been pressed on the dialog

    Dim textstreamwrite r As StreamWriter

    Dim responsedialogr esults As DialogResult


    SaveFileDialog1 .InitialDirecto ry = Application.Sta rtupPath

    responsedialogr esults = SaveFileDialog1 .ShowDialog

    if responsedialogr esults DialogResult.Ok Then
    textstreamwrite r.WriteLine(rtb FontBox.Text)

    textstreamwrite r.Flush()

    textstreamwrite r.Close()

    Else If responsedialogr esults = DialogResult.Ca ncel Then

    textstreamwrite r.Close()

    End If

    End if

    Comment

    • Jay

      #3
      Re: Help

      It errors out at textstreamwrite r.WriteLine(rtb FontBox.Text), and when I put
      your code it it errors out at textstreamwrite r.Close()


      the error for below is An unhandled exception of type
      'System.NullRef erenceException ' occurred in Assignment1.exe

      Additional information: Object reference not set to an instance of an
      object."

      Dim textstreamwrite r As StreamWriter

      Dim responsedialogr esults As DialogResult

      SaveFileDialog1 .InitialDirecto ry = Application.Sta rtupPath

      responsedialogr esults = SaveFileDialog1 .ShowDialog

      textstreamwrite r.WriteLine(rtb FontBox.Text)

      textstreamwrite r.Flush()

      textstreamwrite r.Close()

      If responsedialogr esults = DialogResult.Ca ncel Then

      textstreamwrite r.Close()

      End If




      "Saurabh Nandu" <SaurabhNandu@d iscussions.micr osoft.com> wrote in message
      news:B027441C-8323-413B-9C1D-B24E4186D4AE@mi crosoft.com...[color=blue]
      > Hi,
      >
      > What is the error you are getting? And on which line is the error being
      > indicated?
      >
      > Also it might be a good idea to write your code as below, os that it only
      > writes to the Stream if the Ok button has been pressed on the dialog
      >
      > Dim textstreamwrite r As StreamWriter
      >
      > Dim responsedialogr esults As DialogResult
      >
      >
      > SaveFileDialog1 .InitialDirecto ry = Application.Sta rtupPath
      >
      > responsedialogr esults = SaveFileDialog1 .ShowDialog
      >
      > if responsedialogr esults DialogResult.Ok Then
      > textstreamwrite r.WriteLine(rtb FontBox.Text)
      >
      > textstreamwrite r.Flush()
      >
      > textstreamwrite r.Close()
      >
      > Else If responsedialogr esults = DialogResult.Ca ncel Then
      >
      > textstreamwrite r.Close()
      >
      > End If
      >
      > End if
      >[/color]


      Comment

      • Saurabh Nandu

        #4
        Re: Help

        Hi

        Well you are getting a null reference since you have not created an Object
        of the Type StreamWriter. In .NEt you need to explicitly create objects
        before you can use them. So you need to explictly create the object
        StreamWriter passing the path of the file to save the text.


        So modify your code to

        Dim textstreamwrite r As StreamWriter = new StreamWriter("c :\filename.txt" )

        Dim responsedialogr esults As DialogResult


        SaveFileDialog1 .InitialDirecto ry = Application.Sta rtupPath

        responsedialogr esults = SaveFileDialog1 .ShowDialog

        if responsedialogr esults DialogResult.Ok Then
        textstreamwrite r.WriteLine(rtb FontBox.Text)

        textstreamwrite r.Flush()

        textstreamwrite r.Close()

        Else If responsedialogr esults = DialogResult.Ca ncel Then

        textstreamwrite r.Close()

        End If

        End if

        Comment

        • Jay

          #5
          Re: Help

          I am missing something here. When I run you code ""Dim textstreamwrite r As
          StreamWriter = new StreamWriter("c :\filename.txt" )"" I am getting a blank
          file (correct name) on my C:\. And when I change it so it is pulling data
          from a rich text box (Dim textstreamwrite r As StreamWriter = New
          StreamWriter(rt bFontBox.Select edText) I am getting following error

          An unhandled exception of type 'System.Argumen tException' occurred in
          mscorlib.dll

          Additional information: Empty path name is not legal.

          What is going on here, it was working at one time! I should say I could
          save but not cancel. Now I can do anything!



          "Saurabh Nandu" <SaurabhNandu@d iscussions.micr osoft.com> wrote in message
          news:06152BB4-1EE0-4CFF-86BE-4298997FA05D@mi crosoft.com...[color=blue]
          > Hi
          >
          > Well you are getting a null reference since you have not created an Object
          > of the Type StreamWriter. In .NEt you need to explicitly create objects
          > before you can use them. So you need to explictly create the object
          > StreamWriter passing the path of the file to save the text.
          >
          >
          > So modify your code to
          >
          > Dim textstreamwrite r As StreamWriter = new StreamWriter("c :\filename.txt" )
          >
          > Dim responsedialogr esults As DialogResult
          >
          >
          > SaveFileDialog1 .InitialDirecto ry = Application.Sta rtupPath
          >
          > responsedialogr esults = SaveFileDialog1 .ShowDialog
          >
          > if responsedialogr esults DialogResult.Ok Then
          > textstreamwrite r.WriteLine(rtb FontBox.Text)
          >
          > textstreamwrite r.Flush()
          >
          > textstreamwrite r.Close()
          >
          > Else If responsedialogr esults = DialogResult.Ca ncel Then
          >
          > textstreamwrite r.Close()
          >
          > End If
          >
          > End if
          >[/color]


          Comment

          • Saurabh Nandu

            #6
            Re: Help

            Jay,

            When you say it was working before, where was it saving the contents of the
            RichTextBox to ? The reason it was giving you an error was that you were
            trying to use the StreamWriter object before creating an instance of it.
            Hence the NullReference Error.

            The last sample I gave you will create a filename with the path you provide
            in the constructor. And then when you call a WriteLine on the StreamWriter it
            will save the contents of method call into the file.

            I have updated code that will take the path of the file from the SaveFile
            dialog and save the contents of the textbox.

            Dim textstreamwrite r As StreamWriter

            Dim responsedialogr esults As DialogResult

            SaveFileDialog1 .InitialDirecto ry = Application.Sta rtupPath

            responsedialogr esults = SaveFileDialog1 .ShowDialog
            if responsedialogr esults = DialogResult.OK Then
            ' Create a new StreamWriter Object with the filename selected
            textstreamwrite r = new StreamWriter( SaveFileDialog1 .FileName )
            ' Write the contents of the TextBox
            textstreamwrite r.WriteLine(rtb FontBox.Text)
            textstreamwrite r.Flush()
            textstreamwrite r.Close()
            End if


            Comment

            • Jay

              #7
              Re: Help

              That got it to work. Thank you so very much. Not sure what I did to mess
              things up so bad but thank you. I do have one concern about the below code
              you gave me. If my rich text box is blank then it errors out on me. Also I
              tried to update the path and it like you did, it would create a file name 1
              with no extention. It would have an IE icon for it and my text would be
              inside. Any ideas? And thank you so much for your help, I have been at
              this for a long time now. : )


              "Saurabh Nandu" <SaurabhNandu@d iscussions.micr osoft.com> wrote in message
              news:4463F9FA-77E6-4FA2-B38E-6206CFAD015A@mi crosoft.com...[color=blue]
              > Jay,
              >
              > When you say it was working before, where was it saving the contents of
              > the
              > RichTextBox to ? The reason it was giving you an error was that you were
              > trying to use the StreamWriter object before creating an instance of it.
              > Hence the NullReference Error.
              >
              > The last sample I gave you will create a filename with the path you
              > provide
              > in the constructor. And then when you call a WriteLine on the StreamWriter
              > it
              > will save the contents of method call into the file.
              >
              > I have updated code that will take the path of the file from the SaveFile
              > dialog and save the contents of the textbox.
              >
              > Dim textstreamwrite r As StreamWriter
              >
              > Dim responsedialogr esults As DialogResult
              >
              > SaveFileDialog1 .InitialDirecto ry = Application.Sta rtupPath
              >
              > responsedialogr esults = SaveFileDialog1 .ShowDialog
              > if responsedialogr esults = DialogResult.OK Then
              > ' Create a new StreamWriter Object with the filename selected
              > textstreamwrite r = new StreamWriter( SaveFileDialog1 .FileName )
              > ' Write the contents of the TextBox
              > textstreamwrite r.WriteLine(rtb FontBox.Text)
              > textstreamwrite r.Flush()
              > textstreamwrite r.Close()
              > End if
              >
              >[/color]


              Comment

              • Saurabh Nandu

                #8
                Re: Help

                I am glad it works for you. For the rest 2 questions I'll give you hints and
                you can then fix the code.

                For the emptry textbox error, change the code to check if the textbox has
                text before you call the StreamWriter's WriteLine property to ensure only if
                there is text you write to the file.

                For the file extension, check up the SaveFileDialog' s properties to set the
                file extension for your files, so that the extension is appended by default.


                Comment

                • Jay

                  #9
                  Re: Help

                  I got it. I just wrote a if statement that checks my txt box for "" and
                  then if true it writes a blank file. Thanks again. : ) I am trying to
                  learn how to write code from an online class, it is hard sometimes because
                  there is no one to ask questions and I have so many. Thank you again : )
                  "Saurabh Nandu" <SaurabhNandu@d iscussions.micr osoft.com> wrote in message
                  news:D88326CC-96B7-4D17-B202-93C9A181577D@mi crosoft.com...[color=blue]
                  >I am glad it works for you. For the rest 2 questions I'll give you hints
                  >and
                  > you can then fix the code.
                  >
                  > For the emptry textbox error, change the code to check if the textbox has
                  > text before you call the StreamWriter's WriteLine property to ensure only
                  > if
                  > there is text you write to the file.
                  >
                  > For the file extension, check up the SaveFileDialog' s properties to set
                  > the
                  > file extension for your files, so that the extension is appended by
                  > default.
                  >
                  >[/color]


                  Comment

                  Working...