Login and register for course database, need help
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Login and register for course database, need help

  1. #1
    Join Date
    Dec 2011
    Posts
    1

    Login and register for course database, need help

    I'm taking a C# beginners course at my university this semester, and for my final project I'm trying to make a login and register for my "course database". I got the login working recently, but ever since I added the register form to the project the login stopped working and instead gave me errors. I have tried without avail to get login working again, but I have had no such luck. I was hoping you guys could help me out.

    Below is what I've got so far.

    --

    This is the login portion. I use streamreader to read login information from text files in the debug folder of my visual c# project. If the login is successful, then the database form is displayed (which I have not shown here). If not, then invalid input is displayed.

    --

    namespace CourseDataFinal3
    {
    public partial class MainForm : Form
    {
    public MainForm()
    {
    InitializeComponent();
    }
    private int userID;
    private string password;

    private void CheckText()
    {
    try
    {

    StreamReader loginFile;
    loginFile = File.OpenText("logininfo.txt");

    int.TryParse(loginFile.ReadToEnd(), out userID);

    loginFile.Close();

    StreamReader passwordFile;
    passwordFile = File.OpenText("passwordInfo.txt");

    password = passwordFile.ReadToEnd();

    passwordFile.Close();

    }
    catch
    {
    MessageBox.Show("Error in reading data");
    }


    }




    private void loginButton_Click(object sender, EventArgs e)
    {
    CheckText();
    Form1 myDataForm = new Form1();
    int userInput;
    string passwordInput;
    int.TryParse(userLoginText.Text, out userInput);
    passwordInput = passwordText.ToString();

    if (userInput == userID && passwordInput == password)
    {
    myDataForm.ShowDialog();
    }

    else
    {
    MessageBox.Show("Invalid input");
    }


    }

    private void registerButton_Click(object sender, EventArgs e)
    {
    RegisterForm myRegForm = new RegisterForm();
    myRegForm.ShowDialog();
    this.Hide();
    }

    }
    }

    --

    And this is the register portion. It uses streamwriter to write to the same files, which the login form reads from

    --

    namespace CourseDataFinal3
    {
    public partial class RegisterForm : Form
    {
    public RegisterForm()
    {
    InitializeComponent();
    }

    private void cancelButton_Click(object sender, EventArgs e)
    {
    this.Close();
    }

    private void regButton_Click(object sender, EventArgs e)
    {
    try
    {
    //Commented out for future reference:
    //RegisterForm myRegForm = new RegisterForm();

    //Errors or items still not working:
    //Still registers blanks, error msg will not show

    StreamWriter loginFile;
    loginFile = File.AppendText("logininfo.txt");
    loginFile.WriteLine(usernameTextBox.Text);
    loginFile.Close();
    StreamWriter passwordFile;
    passwordFile = File.AppendText("passwordInfo.txt");
    passwordFile.WriteLine(passTextBox.Text);
    passwordFile.Close();

    MessageBox.Show("Thank you for registering!");
    usernameTextBox.Text = "";
    passTextBox.Text = "";
    usernameTextBox.Focus();

    }
    catch
    {
    MessageBox.Show("Please fill out both; a username and a password.");
    }
    }
    }
    }

    Any idea's?

  2. #2
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,006

    Re: Login and register for course database, need help

    Hi,

    So a few comments. First, in your regbutton click method, you have:

    Code:
    try
    {
        //Processing
    }
    catch
    {
        //Display error message
    }
    The error message will display when an exception is thrown. Since the name and password fields being blank does not throw an exception, the message will never display. Instead you should remove the Try/Catch block and instead do checking at the start of the method:

    Code:
    if( usernameTextBox.Text = "" || passTextBox.Text = "" )
    {
        MessageBox.Show("Error!");
        return;
    }
    Second, you have chosen an odd design to store username and passwords of multiple registrants. The database strategy you have chosen is using a "flat file" (which is fine for these purposes), but you seem to be storing related fields in two different files. A better strategy would be to create a single file (logindata.txt) that hold pairs of usernames and passwords, seperated by a comma, one to a line (this is called "CSV"). That is with format.

    Code:
    Bob,password1
    Sally,kittensaresocute
    Boss,foolishmortals
    Then you can read in the lines (instead of the while file) into an array using
    Code:
    string[] userpasscombos = File.ReadAllLines(logindata.txt);
    (Note that your current strategy of using File.ReadToEnd(string) to read the entire contents into a single string variable is the source of at least one of your problems; your login checking subroutine does not seem to contemplate the possibility of multiple registrants).

    And perform a loop to check to see if usernameTextBox.Text + "," + passTextBox.Text is contained in any element of string[] userpasscombos.

    Aye?

    Also please use [code] and [/code] tags around code to preserve formatting.
    Last edited by BioPhysEngr; December 18th, 2011 at 05:50 PM. Reason: typo
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center