Hello:
I would like to read in a text file and have it populate some objects in my program. Also I would like to change or delete some lines in the text file. Anyone know where I can find some sample code to help me do this? (Also I would like to add a new line or delete a line).
The following is what I would like to have in my test file:
User,Password,Sysytem,Database
Scott,Scott1212,HPV64,Devel
Scott,Scott1212,HPV64,Live
Bill,Bill3434,HPV64,Devel
Bill,Bill3434,HPV64,Live
Joe,Joe5656,VMW7,Devel
Joe,Joe5656,VMW7,Live
The objects I have on my form are:
textBoxUser_ID
textBoxPassword
comboBoxServer
comboBoxDatabase
Sam HobbsPosted Feb 23, 2010, 10:53 PM
Sam HobbsPosted Mar 5, 2010, 11:47 AM
I do agree there is no value in continuing this.
theLizardPosted Mar 4, 2010, 2:54 PM
It seems that Giorgio is not entirely sure about anything and I do not criticize him for this as he is learning what my problem is with all of this, dare I say it, crap, is that so many different methods were thrown at Giorgio along with misinformation that for a learner it would be confusing and daunting to deal with the different methodologies to achieve a simple task.
The question was asked in different ways in different threads, this also went to the heart of the matter where it appeared that Georgio did not know what he wanted and how to achieve it.
Georgio goes of on a tangent learning things that are not necessary to learn if it is known that it is NOT going to be done that way at all.
It is like a heart surgeon practicing on a healthy patient replacing a healty heart with a mechanical one just to see how the mechanical one would works.
What a waste of time and effort, this would be best utilized learning the methods and procedures of the tools you know you will be using.
I have at all times in this thread and others relating to same, maintained the one methodology, and tyhat is using a stream reader to read in was or should have been a controled CSV file.
Never once did I say learn THIS but you should know that THIS will be useless and that you should learn THAT but by the way you can do it THIS way if you used THIS or THAT but on the other hand you could youe STREAM READER / WRITER which I said in the first place.
Now obviously Georgio likes to be bounced around a room like a rubber ball, that is his choice and I will not deny him his rights to do that but you are offering tuition and you are bouncing him around.
Sometimes it is best to leave crumbs for people to follow, if Georgio is really keen to learn then he will pick up the crumbs and follow through with his own ideas and solutions which could ultimately be one of the methods you or I or anyone else suggested doing this may actually produce a really good programmer and this is the aim is it not!
Now Sam, I have had a number of businesses over the years, one of those businesses was a software development house where we developed administrative applications dealing with educational institutions, our clients were state and federal government as well as other very high profile international organizations, I have been out of this business for some time now but it is still operational and still dealing with the organizations mentioned.
The best solution for Georgio's needs is HIS choice not yours, not mine, whatever he is comfortable with is fine by me, I would not be upset by the fact that he did not choose my method.
Now let this be the end.
One last piece of advice, be consistent with your advice to others and help them learn, let them find out about histories of how and what is happening in programming let them ask specific questions and simply point in the right direction don't spoon feed them.
Sam HobbsPosted Mar 4, 2010, 3:36 AM
theLizardPosted Mar 3, 2010, 7:44 PM
I think just about everything has been suggested by now so what will be next? I can hardly wait for your next suggestion.
Talk about adding layer upon layer upon layer, reminds me of Sarah Lea, makes great cakes but useless programs.
Sam HobbsPosted Mar 3, 2010, 7:19 PM
Sam HobbsPosted Feb 25, 2010, 10:44 PM
GustavoPosted Feb 25, 2010, 6:36 PM
YES... I would be intersted in that. Sounds a lot better that doing it with a txt file. I would like to see the code.
Sam HobbsPosted Feb 25, 2010, 6:32 PM
What you can do is to read the CSV file into a DataTable, then bind a DataGridView to the DataTable. Or you can put the data into a DataGridView without a DataTable; I posted some code for that a few days ago. I think the question asked about how to put a two-dimensional array into a DataGridView. Are you interested in that?
GustavoPosted Feb 25, 2010, 1:49 PM
I got this issue working well with the txt type sample code.
However, its a pain to edit the txt file. I have been surfing on how I can open an excel file. So this way I can change it easier outside of my application. I found 2 connectionStrings but neither one works. LOL
Sam HobbsPosted Feb 25, 2010, 1:43 PM
In my opinion, there should be a standard for CSV files that is supported by the CLI such as .Net and by languages such as C++. The VisualBasic DLL is the closest to that in Microsoft software.
GustavoPosted Feb 25, 2010, 7:32 AM
With the "using System.Data.Odbc", seems I have the same issue. It does not work when I build with 'All CPU' but it works great with 'x86'.
I went back and forth a few times to be sure.
I will now try the: "Microsoft.VisualBasic.FileIO"
theLizardPosted Feb 25, 2010, 1:55 AM
Having quotes is not a big deal, all it means is that you could not use String.Split, you would have to do the splitting yourself and that is NOT difficult.
Dealing with the other things is really not that big a deal either.
Before String.Split or anything else that you can use came along, these little jobs had to be done by the programmer and you would be amazed at how much a person can learn about programming by doing these little tasks.
String manipulation is a very important part of programming don't you think?
Pulling apart strings and analyzing the components like html tags is probably how browsers work, eg,
CSV's are no diferent.
Sam HobbsPosted Feb 25, 2010, 12:48 AM
theLizardPosted Feb 25, 2010, 12:09 AM
How much more difficult can CSV get?
Comma Separated Values like a,b,c,d is a simple data structure is it not?
How could you make CSV more complicated!!!!
Sam HobbsPosted Feb 24, 2010, 11:50 PM
theLizardPosted Feb 24, 2010, 10:46 PM
class lineElement
{
public string[] elements = new string[0];
}
//your function to open and read the file..
lineElement[] e = new lineElement[0];
StreamReader sr = new StreamReader(dlgOpen.FileName);
string l = "";
while((l = sr.ReadLine()) != null)
{
//make sure you do some error checking like null strings etc..
Array.Resize(ref e, e.Length +1);
e[e.Length-1] = new lineElement();
Array.Resize(ref e[e.Length-1].elements, getCount(",", l));
e[e.Length-1].elements = l.Split(new char[] {','});
}
//a function to get how many delimiters in the line read from the file
private int getCount(string delim, string l)
{
int count = 0;
while (l.IndexOf(delim) > 0)
{
l = l.Remove(l.IndexOf(delim), 1);
count++;
}
return(count);
}
The above will allow you to read a file with mixed lengths of delimited values
it will read this -- User,Password,Sysytem,Database
as well as this Scott,Scott1212,HPV64
or this Scott,Scott1212
or this Bill,Bill3434,HPV64,Devel,User,Password,Sytem,Database
without any problem and withonce you have the array you can do what you want, add, edit, delete its not that hard to do...
Ps. No Odbc, No Jet, No data adapters to bloat the system just simple string manipulation..
GustavoPosted Feb 24, 2010, 4:44 PM
I looked around and it seems I do have the Msjet40.dll
But I do get the error posted above for the code:
da.Fill(dt);
I would really like to use your solution... If I can get rid of this error.
GustavoPosted Feb 24, 2010, 2:43 PM
I tried to test your sample and I can not get it to work. Seems from the information that I found by surfing is that the Jet.OLEDB is not supported on a x64 machine.
I get the error: The 'Microsoft.Jet.OLEDB.4.0' provider is not registered on the local machine.
Any suggestions?
Sam HobbsPosted Feb 24, 2010, 12:57 PM
The important thing is that if your text file is in the format of a CSV then you can read it into a DataTable quite easily.
GustavoPosted Feb 24, 2010, 9:41 AM
Thanks, I will look at sample.
I usually try to spent 1-2 hours searching for my solution, untill I post. I tried to search but could not find anything that could help me. I must of not used the correct key words to find it. Thanks... will look at your sample.
GustavoPosted Feb 23, 2010, 4:22 PM
Mahesh ChandPosted Feb 23, 2010, 4:17 PM
TextReader and TextWriter in C#
You may also want to look into object serialization and deserialization. Search this site for these keywords.