Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
Is there better way to read and write a text instead of doing it this way? Can I not do something else instead of giving the user the ability to write to text file or like what i did with this one is give it a unique extension. Is this the only wy of using save file and open file options.
 Dim FILE_NAME As String = FileDataStorage.OpenFileTextBox1.Text
        Try
            If System.IO.File.Exists(FILE_NAME) = True Then
                Dim objWriter As New System.IO.StreamWriter(FILE_NAME)
 
objWriter.WriteLine	(	Form1	.	GTextBox1	.Text	)
objWriter.WriteLine	(	Form1	.	GTextBox2	.Text	)
objWriter.WriteLine	(	Form1	.	GTextBox3	.Text	)
objWriter.WriteLine	(	Form1	.	GTextBox4	.Text	)
objWriter.WriteLine	(	Form1	.	GTextBox5	.Text	)
objWriter.WriteLine	(	Form1	.	GTextBox6	.Text	)
objWriter.WriteLine	(	Form1	.	GTextBox7	.Text	)
objWriter.WriteLine	(	Form1	.	GTextBox8	.Text	)
objWriter.WriteLine	(	Form1	.	GTextBox9	.Text	)
objWriter.WriteLine	(	Form1	.	GTextBox10	.Text	)
objWriter.WriteLine	(	Form1	.	GTextBox11	.Text	)
objWriter.WriteLine	(	Form1	.	ComboBox1	.Text	)
objWriter.WriteLine	(	Form1	.	ComboBox2	.Text	)
objWriter.WriteLine	(	Form1	.	ComboBox3	.Text	)
objWriter.WriteLine	(	Form1	.	ComboBox4	.Text	)
objWriter.WriteLine	(	Form1	.	ComboBox5	.Text	)
objWriter.WriteLine	(	Form1	.	ComboBox6	.Text	)
objWriter.WriteLine	(	Form1	.	ComboBox7	.Text	)
objWriter.WriteLine	(	Form1	.	ComboBox8	.Text	)
objWriter.WriteLine	(	Form1	.	ComboBox9	.Text	)
objWriter.WriteLine	(	Form1	.	ComboBox10	.Text	)
objWriter.WriteLine	(	Form1	.	ComboBox11	.Text	)
objWriter.WriteLine	(	Form1	.	ComboBox12	.Text	)
objWriter.WriteLine	(	Form1	.	ComboBox13	.Text	)
objWriter.WriteLine	(	Form1	.	ComboBox14	.Text	)
objWriter.WriteLine	(	Form1	.	ComboBox15	.Text	)
objWriter.WriteLine	(	Form1	.	ComboBox16	.Text	)
objWriter.WriteLine	(	Form1	.	ComboBox17	.Text	)
objWriter.WriteLine	(	Form1	.	ComboBox18	.Text	)
objWriter.WriteLine	(	Form1	.	ComboBox19	.Text	)
objWriter.WriteLine	(	Form1	.	ComboBox20	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox1	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox2	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox3	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox4	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox5	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox6	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox7	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox8	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox9	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox10	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox11	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox12	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox13	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox14	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox15	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox16	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox17	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox18	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox19	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox20	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox21	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox22	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox23	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox24	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox25	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox26	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox27	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox28	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox29	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox30	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox31	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox32	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox33	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox34	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox35	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox36	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox37	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox38	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox39	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox40	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox41	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox42	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox43	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox44	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox45	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox46	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox47	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox48	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox49	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox50	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox51	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox52	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox53	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox54	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox55	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox56	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox57	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox58	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox59	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox60	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox61	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox62	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox63	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox64	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox65	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox66	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox67	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox68	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox69	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox70	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox71	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox72	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox73	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox74	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox75	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox76	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox77	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox78	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox79	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox80	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox81	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox82	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox83	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox84	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox85	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox86	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox87	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox88	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox89	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox90	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox91	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox92	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox93	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox94	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox95	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox96	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox97	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox98	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox99	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox100	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox101	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox102	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox103	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox104	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox105	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox106	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox107	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox108	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox109	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox110	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox111	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox112	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox113	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox114	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox115	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox116	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox117	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox118	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox119	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox120	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox121	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox122	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox123	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox124	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox125	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox126	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox127	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox128	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox129	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox130	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox131	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox132	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox133	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox134	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox135	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox136	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox137	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox138	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox139	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox140	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox141	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox142	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox143	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox144	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox145	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox146	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox147	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox148	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox149	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox150	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox151	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox152	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox153	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox154	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox155	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox156	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox157	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox158	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox159	.Text	)
objWriter.WriteLine	(	Form2	.	TextBox160	.Text	)
objWriter.WriteLine	(	Form2	.	ComboBox1	.Text	)
  
objWriter.Close()
                MsgBox("Data written to file")
            Else
                'Do Nothing
            End If
        Catch ex As Exception
        End Try
 
</pr<pre lang="vb">
e>
Posted 4-Feb-13 13:14pm
icecode346
Comments
Sergey Alexandrovich Kryukov at 4-Feb-13 18:22pm
   
If you ever came to the idea of writing such "code", and even showing it, I cannot imagine that any advice can help. Unlearn what you have learned. Honestly...
This is not abuse, this is super abuse...
—SA
Mike Meinz at 4-Feb-13 18:43pm
   
What is the business function you are trying to accomplish in doing this? Why would I as a user of your application want to do this?
PIEBALDconsult at 4-Feb-13 18:46pm
   
I'd probably use XML.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Are you looking just to have persistent data? If so, there are a number of options.
 
If you have many users, I recommend looking into using a relational database, such as SqlLite, SqlServer, or MySql.
 
If you have a single user, this may not be necessary. You may just need to export data to a file. There are a number of data formats that would typically suffice for your application. Common ones include CSV and XML.
 
However, if you are asking more along the lines of making your code a bit less...repetitive, I would recommend using iteration; something like this:
 
For Each ctrl As Control In Form1.Controls
    If ctrl.GetType() = GetType(TextBox) Or ctrl.GetType() = GetType(ComboBox) Then
        objWriter.WriteLine(ctrl.Text)
    End If
Next
 
You could use the same code for Form2, or even iterate over a number of Forms.
 
Of course, you may want to replace the WriteLine code with a different function, depending on whether you change how you export the data.
  Permalink  
Comments
icecode at 4-Feb-13 22:05pm
   
Thank you everyone for your help. This application was suppose to be a way my company can replace a spreadsheet. And as I feared the idea is just plain ridiculous for me to do. The code works but as everyone can see it is painful. My company has many applications like this in sorts but I coding is very hush hush amongst the software team which I understand. I made applications before but none this painfully put together. Every reference tool out there discusses save/open file process as what I posted above and has been what I used in the past for smaller applications. I want to do a database instead but I am very in experience in that realm. Lets say my applications are child like in sense of a generic programmer ( Very rookie like)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

I would data bind my controls to an XML data source. I would attach and event to the last control, an on change or something like that which would save my entire data source.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 304
1 Sergey Alexandrovich Kryukov 255
2 Shweta N Mishra 216
3 Maciej Los 210
4 PIEBALDconsult 174
0 OriginalGriff 7,660
1 Sergey Alexandrovich Kryukov 7,072
2 DamithSL 5,586
3 Manas Bhardwaj 4,946
4 Maciej Los 4,665


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 4 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100