0

I have been trying to add user data to a simple database table, and have got this code to work. When I use this code, when I call this method and try to use the data, it works.

    public static bool Attempt_CreateUser(string username, string password)
    {
        try
        {
            DataTableAdapters.UsersTableAdapter usersTableAdapter = new DataTableAdapters.UsersTableAdapter();
            usersTableAdapter.InsertData(username, password);

            return true;
        }
        catch { return false; }
    }

The problem is that when I close the application and reopen it, the data that I added previously does not work again. When I check the database, the data has not been added.

How might I go about fixing this?

Stewbob
  • 16,759
  • 9
  • 63
  • 107
  • It is missig commit in your database apparently – pnet Jan 11 '19 at 18:55
  • @pnet I thought that this might be the case, but I couldn't work out how to do the commit (this is the first time that I have tried to implement a database properly into my solution, as I normally just use text files and convert the data in them to a double string array. Could you possibly show me code to do this??? It would be greatly appreciated :) – Bradley Hastings Jan 11 '19 at 19:01
  • Is the access database file part of your project? Is the database file getting copied to the output folder and replaced whenever you build? – D Stanley Jan 11 '19 at 19:24
  • @DStanley It is. I didn't realise that it even did that. Would excluding the original database (that is in the project folder) from the project stop this overwriting to happen? I'm going to try it for myself, and hope for the best [fingers crossed] Edit: I tried it, and it broke the program, as it just deleted the data.mdb file in the bin directory – Bradley Hastings Jan 11 '19 at 19:30
  • Or set the "Copy to Output" property to "Copy if newer". That way you can update the _structure_ from within the project and integrate it with source control. The risk is if you update the structure you also lose the data (unless you copy the data back to the source file) – D Stanley Jan 11 '19 at 19:33
  • https://learn.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2010/0c6xyb66(v=vs.100) – D Stanley Jan 11 '19 at 19:34
  • @DStanley Thank you so much. That worked! At least now I know that if you want to add stuff to the database between builds, and I want to add those elements inside of the application that I'm building, I can do just that. Many thanks!!! – Bradley Hastings Jan 11 '19 at 19:37
  • Glad it helped. I've found a reasonably close duplicate in order to close out your question. – D Stanley Jan 11 '19 at 19:41

1 Answers1

0

Maybe you can try this way

public static bool Attempt_CreateUser(string username, string password)
    {
        try
        {
            DataTableAdapters.UsersTableAdapter usersTableAdapter = new DataTableAdapters.UsersTableAdapter();
            usersTableAdapter.InsertData(username, password);

            return true;
        }
        catch { return false; }

       RefreshDataSet(); 

    }

private void RefreshDataSet()
{
   this.MyTableAdapter.Fill(this.MyDataSet.MyTable);
}

Try this

pnet
  • 258
  • 1
  • 17
  • Thanks, but the problem is fixed now. The problem, it turns out, wasn't that the database wasn't keeping the changes, just that between builds (which was how I was running the application) I was overwriting the database with the old one, which wasn't being updated. Thank you for the solution, and am sure that this would have worked had the problem been what I originally thought it was :) – Bradley Hastings Jan 11 '19 at 19:40