locked
Error in writing to Text files RRS feed

  • Question

  • User-794283032 posted

    i have error in when trying to wite to tet files. my case is user will input data to textbox and dropdownlist. and the values will be save to text file. when i run the codes and give the input. data is not save to the text file when i click save button. can someone fix it?. below is my codes. tQ

    Sub cmdSave_click()

    Try

    'Open a file for writing

    Dim FILENAME As String = Server.MapPath("MapDescription.txt")

     

    Dim DescriptionBox1 As New TextBox

    Dim MapSelect As New DropDownList

    'Get a StreamReader class that can be used to read the file

    Dim objStreamWriter As New System.IO.StreamWriter(FILENAME, True)

    objStreamWriter.WriteLine(DescriptionBox1.Text)

    objStreamWriter.WriteLine(MapSelect.Text)

    Label2.Text = "Data Saved"

    objStreamWriter.Close()

    Catch err As Exception

    Label2.Text = "File saved failed. Reason is as follows " & err.ToString()

    Finally

    End Try

     

    Monday, April 28, 2008 11:47 PM

Answers

  • User955742345 posted

    This would be easier if you used an ItemTemplate to define a custom column in a GridView.  Then you could easily identify and re-find the control.

    With the way you have chosen to do this, you will need to re-find your control in any subsequent page event handler.  (If you are only creating one dynamic textbox, all you need to do is move the textbox variable to the module-level, so all events can see it.  If you are creating more than one, follow instructions below.)

    In order to re-find your textbox, you need to locate the cell it is in, similar to where you added the control to the specific cell to begin with.  You will also need to give the text box an explicit ID, which should only occur once within the page (so if you add multiple dynamic textboxes, each ID should be unique).

     

    ' Need to redeclare the textbox, as it has gone out of scope since the
    '   Page_Load event.
    Dim DescriptionBox1 As New TextBox
    
    ' assign the textbox the same ID as you did in page load.
    '  If you didn't set one, you will not be able to find the control.
    ' assumes strID was set to the correct ID elsewhere.
    DescriptionBox1.ID = strID
    
    ' find the control in the correct row and cell of the table
    '  how you find the values is up to you.
    DescriptionBox1 = MapTable.Rows(RowNo).Cells(ColNo).FindControl(DescriptionBox1.ID)
    
    If DescriptionBox1 Is Nothing Then
      Throw New Exception("Could not find the control")
    End If
    
    ' ... finish coding as necessary
     
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 30, 2008 1:57 PM

All replies

  • User955742345 posted

    If you aren't seeing the error message in Label2, then most likely your only problem is flushing.

    Typically writing to a device (i.e., hard drive) involves a buffer.  This buffer stores the data until the buffer is full, then the buffer is written to the device.  If the buffer is never filled, and the stream is closed, then your file may not have been written at all.  The filling of the buffer may occur several times while writing to a device-- if the data is several times larger than the buffer, than you may see the file but the end of the data may not have been written.

    To fix this problem, you must ALWAYS flush your streams when writing to devices.  This forces all the data in the buffer to be written to the device; do this BEFORE you close the stream.

     

    objStreamWriter.Flush()
    objStreamWriter.Close()
     
    Tuesday, April 29, 2008 12:20 AM
  • User-709459946 posted
    Dim DescriptionBox1 As New TextBox

    Dim MapSelect As New DropDownList

    this are local variable. they contain nothing.

    Tuesday, April 29, 2008 3:34 AM
  • User-794283032 posted

    so reck, u mean that i insert nothing to the textfie?. so, how can i retrieve data from textbox to be able to insert to textfile?. tQ

    Tuesday, April 29, 2008 3:40 AM
  • User955742345 posted

    I missed that part...

    Anyway, rasberry,  if you have already created the dropdownlist and textbox in your .aspx page, and given them an ID, then you can use those IDs in your code-behind (as long as this method is in your code-behind).

    Remove both your Dim ... As New DropDownList and Dim ... As New Textbox lines of code and use the IDs mentioned above to access properties.

    I.E.:

     

    <!-- .aspx code -->
    <asp:TextBox ID="txtName" runat="server" />
      
    ' code-behind
    '  assumes you typed something in txtName before posting back to 'save' the data
    Dim strName As String = txtName.Text
    
    ' ... now use strName to write the value to the text file.
     
    Tuesday, April 29, 2008 12:26 PM
  • User-794283032 posted

    Hi,

    ok, i tried to to declare ID for textbox suggested by p2goat in my Save() function. (Dim strDesc As String = DescriptionBox1.Text).

    but i got error msg DescriptionBox1 is not declared. I created the textbox in Page Load event as below:- 

    Dim DescriptionBox1 As New TextBox

    MapTable.Rows(RowNo).Cells(ColNo).Controls.Add(DescriptionBox1)

    DescriptionBox1.Text &= splitout(i + 1)

     

    So, in Save function when user click Save button, the value in textbox will be save to text file. can someone help me fix this problem?. tQ

    Tuesday, April 29, 2008 10:21 PM
  • User955742345 posted

    This would be easier if you used an ItemTemplate to define a custom column in a GridView.  Then you could easily identify and re-find the control.

    With the way you have chosen to do this, you will need to re-find your control in any subsequent page event handler.  (If you are only creating one dynamic textbox, all you need to do is move the textbox variable to the module-level, so all events can see it.  If you are creating more than one, follow instructions below.)

    In order to re-find your textbox, you need to locate the cell it is in, similar to where you added the control to the specific cell to begin with.  You will also need to give the text box an explicit ID, which should only occur once within the page (so if you add multiple dynamic textboxes, each ID should be unique).

     

    ' Need to redeclare the textbox, as it has gone out of scope since the
    '   Page_Load event.
    Dim DescriptionBox1 As New TextBox
    
    ' assign the textbox the same ID as you did in page load.
    '  If you didn't set one, you will not be able to find the control.
    ' assumes strID was set to the correct ID elsewhere.
    DescriptionBox1.ID = strID
    
    ' find the control in the correct row and cell of the table
    '  how you find the values is up to you.
    DescriptionBox1 = MapTable.Rows(RowNo).Cells(ColNo).FindControl(DescriptionBox1.ID)
    
    If DescriptionBox1 Is Nothing Then
      Throw New Exception("Could not find the control")
    End If
    
    ' ... finish coding as necessary
     
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 30, 2008 1:57 PM
  • User-794283032 posted

    thanks ps2goat, ur codes really help.

    but now, i have problem for example i added 2 rows & insert input into textbox to the rows. after click Save button, i only save the 1st row. input in 2nd row is not saved to the text file. how to fix this?. [:(]

    Wednesday, May 7, 2008 9:18 PM
  • User955742345 posted

    You need to keep track of which rows are new somehow, so that you can loop through ALL new rows and insert them.  Otherwise, limit what your user can do, i.e., make them click a button called 'Save and Add Another'.  This would then limit the user to adding one row at a time, allowing you to save in between new rows.

    Thursday, May 8, 2008 2:33 PM
  • User-794283032 posted

    Hi,

    Yes ps2goat, i agree with u. but the problem is i dunno how to keep track on new rows & save them.

    i like ur idea for button called 'Save and Add Another'. can u show show how to do this in codes?.

    i think my problem is i dunno how to give the rows the ID, and code it in Save function to retrieve which row ID i need to save. coz i just set my rows for one ID like codes below:-

    Protected Sub addRow_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles addRow.Click

    If HiddenField1.Value <> "" Then

    rows = Integer.Parse(HiddenField1.Value)

    For i As Integer = 0 To rows - 1

    ' application will come here when I click Add Row button!

    MapTable.Rows.Add(New TableRow) ' add a new row

    ' we are doing header - format differently - only ever has 2 columns

    MapTable.Rows(MapTable.Rows.Count - 1).Cells.Add(New TableCell)

    MapTable.Rows(MapTable.Rows.Count - 1).Cells.Add(New TableCell)

    Dim strdesc1 As New TextBox strdesc1.ID = "strdesc" + i.ToString()

    strdesc1.Text = "MyNewRow"

    MapTable.Rows(MapTable.Rows.Count - 1).Cells(0).Controls.Add(strdesc1)

    MapTable.Rows(MapTable.Rows.Count - 1).Cells(0).BorderStyle = BorderStyle.Groove

    Dim MapSelect1 As New DropDownList

    MapSelect1.ID = "map" + i.ToString()

    FillList(MapSelect1, Server.MapPath((("~/Google/") + Label1.Text + "/")))

    MapTable.Rows(MapTable.Rows.Count - 1).Cells(1).Controls.Add(MapSelect1)

    ------------------------------------------------------------------------------------------------------------

    Protected Sub Save_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Save.Click

    Try

    Dim FILENAME As String = Server.MapPath("MapDescription.txt")

    Dim objStreamWriter As New System.IO.StreamWriter(FILENAME, True)

    For i As Integer = 0 To rows - 1

    objStreamWriter.WriteLine("{0,10}{1,20}{2,25}", "Rows", "Projects", "Map")

    objStreamWriter.WriteLine(CStr(MapTable.Rows.Count) + "," + strdesc1.Text + "," + MapSelect1.SelectedItem.Value)

    Label2.Text = "Data Saved"

    objStreamWriter.Close()

    Next

    Catch err As Exception
    Label2.Text = "File saved failed. Reason is as follows " & err.ToString()

    Finally

    End Try

    Thursday, May 8, 2008 8:46 PM