0

I have a Visual Studio C# Forms project. In that project is SmartCon.mdf. As you can see below the DataSet is created.

Database file and DataSet

For some reason the data is not saving. When I add a player to the dataset my code is as follows.

SmartConDataSet.PlayersRow newPlayer = myData.Players.NewPlayersRow();
newPlayer.Id = Guid.NewGuid().ToString();
newPlayer.Guid = newGuid;
newPlayer.FirstSeen = DateTime.Now;
newPlayer.Kills = 0;
newPlayer.Deaths = 0;
newPlayer.TotalScore = 0;
myData.Players.AddPlayersRow(newPlayer);

SmartConDataSet.NamesRow newName = myData.Names.NewNamesRow();
newName.Id = Guid.NewGuid().ToString();
newName.Name = Name;
newName.PlayerId = newPlayer.Guid;
newName.Active = 1;
myData.Names.AddNamesRow(newName);
//Update the database
myData.AcceptChanges();

Why isn't my data saving to the database?

UPDATE: I can confirm the data is getting added to the DataSet, but not transfering to the Database.

UPDATE 2: I may need a connection string to the database????

UPDATE 3:

var playerTableAdapter = new SmartConDataSetTableAdapters.PlayersTableAdapter();
        SmartConDataSet.PlayersRow newPlayer = myData.Players.NewPlayersRow();
        newPlayer.Id = Guid.NewGuid().ToString();
        newPlayer.Guid = newGuid;
        newPlayer.FirstSeen = DateTime.Now;
        newPlayer.Kills = 0;
        newPlayer.Deaths = 0;
        newPlayer.TotalScore = 0;
        playerTableAdapter.Insert(newPlayer.Guid, newPlayer.Kills, newPlayer.Deaths, newPlayer.Id, newPlayer.FirstSeen, newPlayer.FirstSeen, 0);

        //Add the players name to the name table
        var namesTableAdapter = new SmartConDataSetTableAdapters.NamesTableAdapter();
        SmartConDataSet.NamesRow newName = myData.Names.NewNamesRow();
        newName.Id = Guid.NewGuid().ToString();
        newName.Name = Name;
        newName.PlayerId = newPlayer.Id;
        newName.Active = 1;
        var result = namesTableAdapter.Insert(newName.Id, newName.PlayerId, newName.Name, 1);

From my research, a DataSet cannot directly make changed to the database. See this thread...

Does instantiating a DataSet object automatically create a connection to a SQL service-based database for CRUD operations?

However my database is still not being updated with the new rows.

UPDATE 4: According to the link in update 3, the connection string was wrong as well. everything works great, HOWEVER, My connection string is now a path on my computer rather than a relative path. If I place this on someone else computer it wont work as their file path will not be the same as mine.

Community
  • 1
  • 1
Tom Hanson
  • 873
  • 10
  • 39
  • The data is being added to the SQL Server database (the mdf file). Look for the data in the database. – jdweng Jul 06 '16 at 10:04
  • I did, I opened the database file in VS and there's no data – Tom Hanson Jul 06 '16 at 10:13
  • Check location of file. Right click ICON. The DataSet should have DataTables and you should be adding rows to the Table. Make sure the DataSet has tables and you are adding to the correct table. – jdweng Jul 06 '16 at 11:41

1 Answers1

0

The database is not updated using these methods. you need to use TableAdapter.Insert Update etc

Tom Hanson
  • 873
  • 10
  • 39