Question

I have access database and i try to add users into my database with c# windows form app.

When i add new users, it says User successfully added, and when i go to DataConnections - dataBase.accdb - tables - employees (in c#), new user is added and is there.

But when i go to my project document/bin/debug/database, there is no new user there. Why is that? Why do i have new user in c# and why no user in debug/database and how do i fix this?

Here is my code

private void button1_Click(object sender, EventArgs e)
{
    try
    {
        OleDbConnection myConnection = new OleDbConnection("//CONNECTION PATH);
        OleDbCommand cmd = new OleDbCommand();
        cmd.Connection = myConnection;
        cmd.CommandText = "Insert into Employees (Name, LastName, UserName, Password, E_mail, Address)" + "values(@Name, @LastName, @UserName, @Password, @E_mail, @Address)";

        cmd.Parameters.AddWithValue("@Name", name.Text);
        cmd.Parameters.AddWithValue("@LastName", lastName.Text);
        cmd.Parameters.AddWithValue("@UserName", userName.Text);
        cmd.Parameters.AddWithValue("@Password", pass.Text);
        cmd.Parameters.AddWithValue("@E_mail", eMail.Text);
        cmd.Parameters.AddWithValue("@Address", address.Text);

        myConnection.Open();
        cmd.ExecuteNonQuery();
        MessageBox.Show("User successfully added.");
        myConnection.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}
Was it helpful?

Solution

Keep your Access database file dataBase.accdb in some other folder path rather than document/bin/debug/database. Since after every build new copy of database file get copy in bin folder hence last changes get lost after every successful build.
Check simillar SO question with .mdf database file .

OTHER TIPS

Replace

cmd.CommandText = "Insert into Employees (Name, LastName, UserName, Password, E_mail, Address)" + "values(@Name, @LastName, @UserName, @GPassword, @E_mail, @Address,)";

with

cmd.CommandText = "Insert into Employees (Name, LastName, UserName, Password, E_mail, Address) values(@Name, @LastName, @UserName, @GPassword, @E_mail, @Address)";
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top