User input

Hi Guys,

I am just looking at c# in Visual Studio for the 1st time. I'm not much of a programmer to be honest - I know a little bit of Java and some php.

I was wondering if someone could give me a kick-start and provide the code to create a simple form which asks for the user first and last name and when you press submit it would then save that information, ideally in and access database but if that's too complicated for one answer, then just in a predetermined location such as in mydocuments folder or whatever.

I know I'm being lazy here but I hate working through beginner tutorials. I much prefer a working starting point which I can then develop from.
SpareheadoneAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Carl TawnSystems and Integration DeveloperCommented:
Web form or WinForm?
0
SpareheadoneAuthor Commented:
Windows Form please.
0
Carl TawnSystems and Integration DeveloperCommented:
OK. Create a form, drop a couple of textboxes on it, names the FirstNameText and LastNameText respectively. Add a Button and name it SaveButton.

Then add the following in the code-behind for the form, after the constructor, but before the end of the class:
        private void SaveButton_Click(object sender, EventArgs e)
        {
            Save(FirstNameText.Text, LastNameText.Text);
        }

        private void Save(string firstName, string lastName)
        {
            string path = System.IO.Path.Combine(Application.StartupPath, "Person_" + DateTime.Today.ToShortDateString().Replace('/', '_') + ".txt");
            System.IO.StreamWriter sw = new System.IO.StreamWriter(path);

            sw.Write(lastName + ", " + firstName);
            sw.Close();

            MessageBox.Show("Done");
        }

Open in new window

This will write to a file rather than a database.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

SpareheadoneAuthor Commented:
Great thanks.
0
SpareheadoneAuthor Commented:
Hi Carl,

I know I accepted this as the solution and subsequently closed the question but I can't get it to work and I wondered if I was missing something obvious.

I have set up as you said and in the (path) part of your code I just put "c:\\"

The form runs as expected but when I click the save button - no message box, nothing happens and the text file is not generated.

Sorry to be thick.
0
Carl TawnSystems and Integration DeveloperCommented:
Sounds like the click event isn't hooked up. Select the SaveButton in the designer, go to the properties pane and click the lightning bolt (events pane) and enter "SaveButton_Click" against the click event.
0
SpareheadoneAuthor Commented:
That's already there so that's not the problem unfortunately.
0
Carl TawnSystems and Integration DeveloperCommented:
Have you literally just put "C:\\" ? You need a filename on there as well if you haven't added one. Also, add a breakpoint to the SaveButton_Click procedure and run it under the debugger to see where it is getting to.
0
SpareheadoneAuthor Commented:
Ok my stupidity there - I hadn't put a file name because I had seen the ,.txt part of your code and assumed it was creating a file of firstname+secondname.txt.

So, now it's saying I don't have access permission to write the file. o, once again being thick - When I try to add to the exceptions list, there is no c#, only c++. How do I allow write permission for the program?
0
SpareheadoneAuthor Commented:
Sorted and working.

Thanks for the help and patience :)

P.S. I haven't spent all day on it, first chance I've had to have another look (just so you don't think I'm that stupid)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.