school registration system

Joined
Oct 25, 2014
Messages
12
Programming Experience
Beginner
Hello i want to know if there's any possible way to write all the data that is stored in a c# widows form application to a text file. for example here we have different text boxes and a radio button and other sorts of data. what sort of code would be used if i wanted to save all this data in a .txt file automatically.

secondly we are suppose to allot a roll number to each student which should increase on its own with an increment of 1 (the roll number should appear when the data is stored in a txt file). also that when the text file is being stored the data shouldn't be overwrited.
im a beginner to c#. ive alony made several console applications and this project was assigned to me. please help

slide3.png

i dont know if this will be usefeul but i googled all day and heres what i found regarding c# writing to a notepad file.

C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Compose a string that consists of three lines.
            string lines = "First line.\r\nSecond line.\r\nThird line.";
            string data = "dataaaaaaaaaaaaaaaaaaaaaaa";
            // Write the string to a file.
            System.IO.StreamWriter file = new System.IO.StreamWriter("d:\\test.txt");
            file.WriteLine("{0} {1} {2}", data1,lines,data);

            file.Close();
        }
    }
}

this code was writing the desired textbox to the notepad file but output was like this

System.Windows.Forms.TextBox, Text: HELOELLEOLO.

PLEASSEEEEEE HELP
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,272
Location
Sydney, Australia
Programming Experience
10+
Firstly, if you have two unrelated questions (being part of the same project does not mean related) then please ask them in two separate threads. One thread per topic and one topic per thread please.

With regards to your first issue, you don't want to write a TextBox to the file; you want to write the CONTENTS of the TextBox to the file. How do you usually get the contents of a TextBox? As a clue, it's basically the same as the way you set the contents.

With regards to your second issue, you would need to read the file at the start and get the last value currently recorded. You can assign that to a variable and simply increment it by 1 for each record you save. If the file doesn't exist or is empty at the start then you initialise the variable to 1.
 
Joined
Oct 25, 2014
Messages
12
Programming Experience
Beginner
Sorry I didnt know how to post in the forum.. ill be careful from now onwards. :)
anyways about the topic can u please help in what sort of code will be used in this project. I do know that we have to store the data that will be written in the textbox and not the actual text box. I don't have any idea from where to start or where to end.
 
Joined
Oct 25, 2014
Messages
12
Programming Experience
Beginner
Im sorry if I sound dumb but no I didnt find anything useful. The only piece of code that I understood and found helpful is posted in the thread. If the answer is in google then ill give it another try. Thanks
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,272
Location
Sydney, Australia
Programming Experience
10+
You are expecting to just find the perfect piece of code to copy and paste. That's the problem. What you should be doing is understanding the principles and then writing your own code. You should be using the VS Help menu first of all but, even if you Google, you should still be able to find the MSDN documentation for the TextBox class. If you read that then you will find out all the members it has, including the one that text it contains, and and possibly code examples as well.
 

wim sturkenboom

Well-known member
Joined
Aug 6, 2014
Messages
85
Location
Roodepoort, South Africa
Programming Experience
10+
Hello i want to know if there's any possible way to write all the data that is stored in a c# widows form application to a text file. for example here we have different text boxes and a radio button and other sorts of data. what sort of code would be used if i wanted to save all this data in a .txt file automatically.

secondly we are suppose to allot a roll number to each student which should increase on its own with an increment of 1 (the roll number should appear when the data is stored in a txt file). also that when the text file is being stored the data shouldn't be overwrited.

Is writing to file used to the store data so information for a student can later be retrieved and displayed / modified / deleted?
 
Joined
Oct 25, 2014
Messages
12
Programming Experience
Beginner
Rep

Okay i googled all night and i was able to come up with this code
C#:
  private void button1_Click(object sender, EventArgs e)
        {



            {
                DateTime B_Day = dateTimePicker1.Value;
                DateTime Today = DateTime.Today;

                int Age = Today.Year - B_Day.Year;
                if (B_Day > Today.AddYears(-Age))
                    agee.Text = Age.ToString();
            }

            {
                string[] line = {"NAME\n" , c_name.Text} ;
                File.AppendAllLines(@"D:\test.txt", line);
            }

            {
                string[] line1 = { "FATHER NAME\n", f_name.Text };
                File.AppendAllLines(@"D:\test.txt", line1);
            }

            {
                string[] line2 = { "ADDRESS\n", adress.Text };
                File.AppendAllLines(@"D:\test.txt", line2);
            }

            {
                string[] line3 = { "PHONE\n", phone.Text };
                File.AppendAllLines(@"D:\test.txt", line3);
            }

            {
                string[] line4 = { "AGE\n", agee.Text };
                File.AppendAllLines(@"D:\test.txt", line4);
            }

            {
                string[] line4 = { "___________________________________________________________________________________\n\n" };
                File.AppendAllLines(@"D:\test.txt", line4);
            }
        }



What it does is that it is saving the data to the text file.

aaaa.jpg

now here are my questions
1. if i want to compress the code, as u can see it is very long and after every text box i have to write the structure of line again. can i add this to a single line ?
2.i have a datetimepicker option also in the program. can that also be written in the note pad file somehow .
3. ive this date of birth to age converter embedded in the program but the dob is only converted into age when the button is pressed. can it be changed so that it refreshes once a new dob is chosen.
Untitledssss.jpg
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,272
Location
Sydney, Australia
Programming Experience
10+
1. if i want to compress the code, as u can see it is very long and after every text box i have to write the structure of line again. can i add this to a single line ?
What does File.AppendAllLines do? It appends all the lines contained in an array to a file. If you want to append more than two lines to a file in one go then put more than two lines into your array. If you want to append all the lines to the file in one go then put all the lines into your array in one go.
2.i have a datetimepicker option also in the program. can that also be written in the note pad file somehow .
Of course it can. The most relevant property of a DateTimePicker is Value, which is a DateTime representing the date and/or time selected in the control. Like every object, a DateTime has a ToString method.
3. ive this date of birth to age converter embedded in the program but the dob is only converted into age when the button is pressed. can it be changed so that it refreshes once a new dob is chosen.
If you put code in the Click event handler of a Button then it's going to be executed when that Button is clicked. If you want the code executed some other time then you have to put it in the handler for an event that is raised some other time. What event is raised at the time that you want to execute that code? If it's a change in the DateTimePicker that you want to react to then it should be fairly clear that it's an event of the DateTimePicker that you need to handle. That means that your first order of business is to read the documentation for the DateTimePicker class to see what events it has and which of those, if any, satisfies your requirement.
 
Joined
Oct 25, 2014
Messages
12
Programming Experience
Beginner
Okay THANKS A LOTT solved the 3 problems above. now all i need is to assign a roll number to each student entry without the use a database.
 
Top Bottom