-2

i need to create an application which allows a student per se to key in his username,admin no and gender..(1)how can i save the data that the user has keyed in so to collect all the datas of the students for the (2)teacher to search the information of a student? this program is to be wriiten using c#..thanks

Erick Robertson
  • 32,125
  • 13
  • 69
  • 98
Jun Jie
  • 33
  • 1
  • 5
  • You mean like persisting it to a database? – Tyler Treat Dec 10 '10 at 01:21
  • C# has awesome XML support too. It's really easy to serialize a class. I.e. take a class, and store all it's attributes in an xml file. Using tags such as XmlAttribute, XmlArray, XmlArrayItem and XmlRoot you can make .Net do all the heavy lifting for you by saving your data to Xml, and loading it again. – Liz Dec 10 '10 at 01:26
  • on the application i have to put in 3 textboxes..these are the textboxes i need to insert in the name,admin no and gender of the students..how shld i go about it in saving the information when i click on the save button on my application? many thks – Jun Jie Dec 10 '10 at 01:30
  • It's also possible that your instructor doesn't actually want you to "save" the data to the hard drive (which is necessary if you want the data to be available again next time you run the program), but instead just wants you to put the data into a collection (like an array). In which case just create an class to hold your fields, and use a List, then there is the question of UI to display the list, and filter/search it. But that's really a separate issue, and depends on what UI framework you're using, WPF or WinForms. – Paul Wheeler Dec 10 '10 at 04:30

3 Answers3

2

If by "save" you mean read from/write to disk, there are several ways to do this.

  1. You could use a flat file, reading and writing each entry as a line of text in a file, for this you would use StreamReader and StreamWriter, and you'd need a delimiter (such as a comma to separate between the fields (username, admin no, gender). String.Split and String.Join (or String.Format) would be useful here. This method has the drawback of being inefficient if you have lots of numeric fields, and you have to watch out for delimiter characters in your string fields.
  2. You could use a binary file, this is a bit more advanced, you'd use BinaryReader/BinaryWriter, and you'd have to think a bit harder about file layout. I'm not going to go into this.
  3. You could use an Object Serialization Framework. In other words, each entry is represented as an Object, and that Object conforms to some serialization interface. See either IXmlSerializable, the XmlSerializer class and associated code attributes ([XmlType], [XmlAttribute], etc.), or the [Serializable] code attribute and BinaryFormatter class. Some of these frameworks are kind of old and clunky, and this method seems a bit heavy weight for your homework assignment.
  4. You could use a database, like Microsoft SQL, and either ADO.Net, or a native library like SqlConnection/SqlReader. This would require an understanding of databases, which you probably don't have yet, and would also be over kill for this task.

The bottom line, you probably what to save data as a text file and use StreamReader/StreamWriter.

For searching, since this is just a homework assignment, you should probably just read each line from the file and do a String.Contains for whatever term or terms the "teacher" enters, then print out all the lines that match.

As a side note, how is it that you have absolutely no idea where to start on this assignment? Did you sleep through class, or did you not understand a word your instructor said?

Paul Wheeler
  • 18,988
  • 3
  • 28
  • 41
  • Hi Paul,apprecicate your wonderful help,i will be looking into using SQL which would be a major challenage for me..thks! – Jun Jie Dec 10 '10 at 01:51
  • Okay, good luck. Keep in mind that if you are new to computer programming, databases like MS SQL, SQLite, and PostgreSQL are complicated applications that are not part of C# itself. You might be biting off more than you can chew as a novice, and your instructor may just be expecting a solution using simple text files. – Paul Wheeler Dec 10 '10 at 04:02
0

Make your life easy! Use a text file and save the data in comma-separated values(CSV) format.

field1,field2,field3,field4
Arash N
  • 324
  • 1
  • 2
  • 10
0

There are a number of options: you could save the data as simple plain text, like in a CSV, or you could do something more advanced like store it in a database. Since it's for a homework assignment, I'm guessing you should probably go with saving it to a CSV. It's easy to do and will work for your purposes. Check out some code examples of how to do this:

http://knab.ws/blog/index.php?/archives/3-CSV-file-parser-and-writer-in-C-Part-1.html

Tyler Treat
  • 14,640
  • 15
  • 80
  • 115
  • many thks Tyler..i m still trying hard to work on it cos it is still quite tough for me as a novice user for c# – Jun Jie Dec 10 '10 at 01:50