0

This is my first project on C#, and I'm trying to implement a profile section to my program. I want the user to be able to put in their information, like name, address, ect, and be able to save the information the put in, and use it later on. Right now, I can only have them save one profile, and I'm lost on how to add more profiles with the click of a button. Here is my code currently to save their information:

        public void button4_Click(object sender, EventArgs e)
    {
        profile1Name = txtProfile1Name.Text;
        email = txtEmail.Text;
        password = txtPassword.Text;
        fullName = txtFullName.Text;
        txtEmail.Text = Properties.Settings.Default.Email;
        Properties.Settings.Default.ProfileName = txtProfile1Name.Text;
        Properties.Settings.Default.Email = txtEmail.Text;
        Properties.Settings.Default.Password = txtPassword.Text;
        Properties.Settings.Default.FullName = txtFullName.Text;
        Properties.Settings.Default.Save();
        MessageBox.Show($"Profile saved.");
    }

    public void LoadSettings()
    {
        profile1Name = Properties.Settings.Default.ProfileName;
        email = Properties.Settings.Default.Email;
        password = Properties.Settings.Default.Password;
        fullName = Properties.Settings.Default.FullName;
    }

I have the user input their information into a text box and click save. I also have another form where users can input information, I'm just not sure how to change the names of the strings that they create.

I'm new to c#, so any other advice would be useful as well. Thank you in advance for the help :)

1 Answers1

0

It is a broad topic, but I believe I can still give you an idea for you to know how it works or at least "What all to google!"

Saving a user information into the properties is not the conventional way of "storing the user information". You will need to add an external database in order to store and retrieve multiple user information.

In order to add an external database, You will have to create a table consisting of fields that will store Username, Password, Age etc. you have options for what database you are using eg: Microsoft Access, SQL, Oracle.

Lets say you are creating a database using Microsoft Access, Firstly you will have to create and open a new MS Access File, Click on the Create Tab and select Table. Add the required fields(Full name, username, email, password In your case) as shown below:

enter image description here

Further, You will need to link your database to the app, for this you will need to:

  • Import OLEdb library that handles database manipulation commands
  • Code on the button that creates a new profile
  • Create a connection string for your database and your app (Connection strings tell the app where your database is located)
  • Finally add SQL query that will add user information to the database.

Which will look something like this:

Imports System.Data.OleDb    //Preferably placed before the form load code ie. Top of the page 
Dim connection As OleDbConnection = New OleDbConnection //Code below, including this is written in the "create profile" button block
            connection.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Application.StartupPath + "\LoginDB.accdb" //Here, LoginDB is the name of your MS Access file.
            connection.Open()
            Dim updatequery As OleDbCommand = New OleDbCommand("INSERT INTO LoginTable (FullName,ProfileName,Email,Password) VALUES(@name1,@name2,@email,@pass)", connection) //LoginTable being the name of the table created inside MS Access file.
            updatequery.Parameters.Add("@name1", OleDbType.VarChar).Value = TextBox1.Text
            updatequery.Parameters.Add("@name2", OleDbType.VarChar).Value = TextBox2.Text
            updatequery.Parameters.Add("@email", OleDbType.Numeric).Value = TextBox3.Text
            updatequery.Parameters.Add("@pass", OleDbType.Numeric).Value = TextBox4.Text
            updatequery.ExecuteNonQuery()
            connection.Close()

About Saving information on the properties page of the application, Think of it as more like having one admin profile in properties that can be accessed throughout the app even when the external user database is deleted

  • Will doing this store their information local to their computer? – Caden Buckelew Nov 09 '18 at 21:35
  • I believe the database should be binded with the exe of the app. so whoever has the app also has the database. My app was more about a college model. like a prototype. so worked out for me. I f you are going to share the app, I believe you have to store on online databases such as SQL – asiffarhankhan Nov 11 '18 at 20:55