1

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);
    }
}
fkr
  • 155
  • 3
  • 9
  • 21
  • your insert query is having syntax error ... at last remove "," befor ")" – C Sharper Jan 14 '14 at 11:50
  • it's no syntax error, the **" , "** is just a mistake because i have to translate info from my language to english so you guys can understand what is what. It works otherwise – fkr Jan 14 '14 at 11:53
  • where is your dataBase.accdb is placed? – C Sharper Jan 14 '14 at 11:54
  • My original database is placed elsewhere **(documents/college/rgis/project/3 )** than the project **(documents/visual2013/projects)**... is this the issue? – fkr Jan 14 '14 at 11:58

2 Answers2

1

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 .

Community
  • 1
  • 1
Ankush Madankar
  • 3,689
  • 4
  • 40
  • 74
  • My original database file(the one i created in access) isn't located in bin/debug/database...My original database is placed elsewhere **(documents/college/rgis/project/3 )** than the project **(documents/visual2013/projects)**... but i still have a database in my debug...dunno why – fkr Jan 14 '14 at 12:00
  • @fkr Check your connection string, which path you mention in it? Is it `/bin` folder location or `documents/college/rgis/project/3` folder location? After every successful build you might getting changes rollback upto your original database file, isnt it? – Ankush Madankar Jan 14 '14 at 12:02
  • Yeah i get changes to my original folder file. Which is more important now? **Debug** or **original folder path** ? Sorry im real newb here because im new to databases – fkr Jan 14 '14 at 12:03
  • @fkr What is your `//CONNECTION PATH` in this case? – Ankush Madankar Jan 14 '14 at 12:07
  • **"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Matic\\Documents\\FERI\\2.LETNIK\\1.SEMESTER\\RPS\\VAJA3\\bazaPodatkov.accdb"** – fkr Jan 14 '14 at 12:08
  • @fkr Original path. Actually we mention `\bin` path location in connectionString when we need to deploye application. – Ankush Madankar Jan 14 '14 at 12:10
  • So i just change it from original location to **\bin** location and everything will work? – fkr Jan 14 '14 at 12:12
  • @fkr Ok, Now you find your change in same original database location file, Check it. Also check from where you fetching data i.e. displying data, now it show your original database file location. – Ankush Madankar Jan 14 '14 at 12:13
  • i changed the location from **original path** to **DEBUG** path. Now it saves everything to Debug location, but not to Original location. – fkr Jan 14 '14 at 12:23
  • @fkr Mention same connectionString while inserting data and displaying data from database, in this case it should be `Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Matic\\Documents\\FERI\\2.LETNIK\\1.SEMESTER\\RPS\\VAJA3\\bazaPodatkov.‌​accdb` i.e. your original database file location. – Ankush Madankar Jan 14 '14 at 12:24
  • @fkr From this code only at single location you can able to save data, rather at `bin` or at original file location. – Ankush Madankar Jan 14 '14 at 12:32
  • Ok thank you very much for your help and information. You've been very helpful. Any tips how to auto generate ID? – fkr Jan 14 '14 at 12:50
  • @fkr Make it(`column ID`) Identity (in Ms-Access it is ["Auto Number"](http://office.microsoft.com/en-in/access-help/fields-that-generate-numbers-automatically-in-access-HA001055067.aspx) ) and Primary key(For unique identification). Same column gereate ID automatically as rows inserted in database. – Ankush Madankar Jan 14 '14 at 12:59
  • @AnkushMadankar, ok i've done that and it works. But will this work when user tries to edit data? Will this change his ID or will it stay the same. Again thank you very much for your help – fkr Jan 14 '14 at 13:09
  • @fkr Ofcourse it will work, you suppose to not expose your `ID` column on UI(Windows form). `ID` column only use for internal perpose i.e. in code to check if record present or not in dataTable. You dont even need to mention `ID` column while inserting new row in dataTable. If it is `Identity` in table then while updating it throws exeption. – Ankush Madankar Jan 14 '14 at 13:21
  • @AnkushMadankar, so when i write the update query, i should include WHERE ID = @'' ? or WHERE ID = @'?' – fkr Jan 14 '14 at 13:30
  • @fkr `where ID = ?`. Since `ID` is of type `Int64`. Now try it your own ;) and if you really stuck some where in code then [Stack Overflow](http://stackoverflow.com/questions/ask) is here and dont forget to mark my suggested solution as an "Answer"! – Ankush Madankar Jan 14 '14 at 13:37
  • @AnkushMadankar yeah again thank you for your time, tips and help. As im new to stackowerflow, i don't rly know how to mark solution as "answer" yet . (haha). I pressed the checkmark and now it's green. If this isn't it, please let me now how to do it. Have a good day. Regards – fkr Jan 14 '14 at 13:44
0

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)";
Rajeev Kumar
  • 4,901
  • 8
  • 48
  • 83