   [.net] Saving changes from a dataset to a database
  isn't it as simple as xxxTableAdapter.Update(xxxDataSet); ?or do I have to set it up something else as well?   
  this is a spin-off from my original question.when i run this code like this:for (int i = 1; i < listCount + 1; ++i)  { GameDBDataSet.PlayerRow addedPlayerRow = gameDBDataSet.Player.NewPlayerRow(); //addedPlayerRow.PlayerID = 2; addedPlayerRow.PlayerName = nameNode.Item(i-1).InnerText; addedPlayerRow.PlayerLevel = levelNode.Item(i-1).InnerText; addedPlayerRow.PlayerScore = int.Parse(scoreNode.Item(i-1).InnerText); addedPlayerRow.PlayerKillRatio = killRatioNode.Item(i-1).InnerText; addedPlayerRow.GameID = 1; gameDBDataSet.Player.Rows.Add(addedPlayerRow); playerTableAdapter.Update(gameDBDataSet);  }i got no errors, but when the same code like this:for (int i = 1; i < listCount + 1; ++i)  { GameDBDataSet.PlayerRow addedPlayerRow = gameDBDataSet.Player.NewPlayerRow(); //addedPlayerRow.PlayerID = 2; addedPlayerRow.PlayerName = nameNode.Item(i-1).InnerText; addedPlayerRow.PlayerLevel = levelNode.Item(i-1).InnerText; addedPlayerRow.PlayerScore = int.Parse(scoreNode.Item(i-1).InnerText); addedPlayerRow.PlayerKillRatio = killRatioNode.Item(i-1).InnerText; addedPlayerRow.GameID = 1; gameDBDataSet.Player.Rows.Add(addedPlayerRow);  }   playerTableAdapter.Update(gameDBDataSet);i get this error: "Column 'PlayerID' is constrained to be unique.  Value '2' is already present."so my question is: "why?"   ;  Ok. I give up. I've googled and MSDN'd. Nothing is working. everytime I run the code then look at my GameDB.mdf every value is NULL. so I don't know what the hell is going on.here's the complete code:private void loadXML()   {  rssStream = new System.IO.FileStream("game.xml", System.IO.FileMode.Open);  rssDoc = new System.Xml.XmlDocument();  rssDoc.Load(rssStream);  rssStream.Close();   }private void XML2DataSet()   {  GameDBDataSet.GameTypeRow addedGameTypeRow = gameDBDataSet.GameType.NewGameTypeRow();  //addedGameTypeRow.GameTypeID = 1;  addedGameTypeRow.GameTypeName = "Action";  gameDBDataSet.GameType.Rows.Add(addedGameTypeRow);  Validate();  gameTypeBindingSource.EndEdit();  int gametyperows = gameTypeTableAdapter.Update(gameDBDataSet);  gameDBDataSet.AcceptChanges();  System.Windows.Forms.MessageBox.Show(gametyperows.ToString());  GameDBDataSet.GameRow addedGameRow = gameDBDataSet.Game.NewGameRow();  //addedGameRow.GameID = 1;  addedGameRow.GameName = rssDoc.DocumentElement.Attributes.Item(0).Value.ToString();  addedGameRow.GameTypeID = 1;  gameDBDataSet.Game.Rows.Add(addedGameRow);  Validate();  gameBindingSource.EndEdit();  int gamerows = gameTableAdapter.Update(gameDBDataSet);  gameDBDataSet.AcceptChanges();  System.Windows.Forms.MessageBox.Show(gamerows.ToString());  /*  addedPlayerRow.PlayerName = rssDoc.DocumentElement.FirstChild.FirstChild.InnerText;  addedPlayerRow.PlayerLevel = rssDoc.DocumentElement.FirstChild.FirstChild.NextSibling.InnerText;  addedPlayerRow.PlayerScore = int.Parse(rssDoc.DocumentElement.FirstChild.FirstChild.NextSibling.NextSibling.InnerText);  addedPlayerRow.PlayerKillRatio = rssDoc.DocumentElement.FirstChild.FirstChild.NextSibling.NextSibling.NextSibling.InnerText;  */    System.Xml.XmlNodeList nameNode = rssDoc.SelectNodes("game/player/name");  System.Xml.XmlNodeList levelNode = rssDoc.SelectNodes("game/player/level");  System.Xml.XmlNodeList scoreNode = rssDoc.SelectNodes("game/player/score");  System.Xml.XmlNodeList killRatioNode = rssDoc.SelectNodes("game/player/kills");    int listCount = nameNode.Count;  for (int i = 1; i < listCount + 1; ++i)  { GameDBDataSet.PlayerRow addedPlayerRow = gameDBDataSet.Player.NewPlayerRow(); //addedPlayerRow.PlayerID = 2; addedPlayerRow.PlayerName = nameNode.Item(i-1).InnerText; addedPlayerRow.PlayerLevel = levelNode.Item(i-1).InnerText; addedPlayerRow.PlayerScore = int.Parse(scoreNode.Item(i-1).InnerText); addedPlayerRow.PlayerKillRatio = killRatioNode.Item(i-1).InnerText; addedPlayerRow.GameID = 1; gameDBDataSet.Player.Rows.Add(addedPlayerRow); Validate(); playerBindingSource.EndEdit(); playerTableAdapter.Update(gameDBDataSet); gameDBDataSet.AcceptChanges();  } }//end of XML2Dataset;  To allow your DataAdapter to update a row in a SQL (or Access) table, you need to specify the UpdateCommand on the DataAdapter. This can be done using 2 methods:1) If your SELECT statement is fairly simple i.e. no JOIN etc... you can create a new SqlCommandBuilder object and call its GetUpdateCommand method. This will generate a new object of type SqlCommand which you set as the UpdateCommand of the DataAdapter2) Create a new SqlCommand Object whose CommandText property contains you SQL Update statement. Set the UpdateCommand of the DataAdapter to this new SqlCommandThe same goes for DELETE and INSERT statements.Once you did this the call to DataAdapter.Update should work fineN.B. I haven't tried this useing a connection to Access yet but it works this way using SQL Server 2000. Note that I'm talking about .NET 1.1. Maybe they changed something in .NET 2.0 if that's what you're using.Also note that I haven't read your code, so if you already did this then I'm afraid I don't know what's happening. Well maybe I'd knew if I had read the code :-)   ;  I'm using .NET 2.0.   ;  The TableAdapter still needs the insert, update and delete queries, otherwise it doesn't know how to update the database. Assuming the TableAdapter is on a form - select Edit Queries in Dataset Designer from the TableAdapter's context menu, right click on the TableAdapter and select AddQuery, step through the wizard for each of the three queries.   ;Quote:Original post by Alpha_ProgDesthis is a spin-off from my original question.when i run this code like this:*** Source Snippet Removed ***i got no errors, but when the same code like this:*** Source Snippet Removed ***i get this error: "Column 'PlayerID' is constrained to be unique.  Value '2' is already present."so my question is: "why?"The database has a primary key column PlayerID that is (or should be) a unique autoincrement column. Your dataset does not follow these constraints and so when you add multiple players with the same id, the dataset adds the row but the database will not allow you to do it. I recommend having the database autoincrement +1 starting at 1 and the dataset autoincrement -1 starting at -1, this way you avoid primary key conflicts. If you don't want autoincrement columns then it is up to you to make sure that the keys don't conflict when the rows are added.Also, you do not need to call AcceptChanges after an Update since it is done automatically. Beyond that I can't really help since I don't know any of the code for your derived classes that you are using here.;  This is all the code:using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;namespace lesson10b{    public partial class XMLViewer : Form    { private System.IO.FileStream rssStream;   private System.Xml.XmlDocument rssDoc; public XMLViewer()   {  InitializeComponent();  loadXML();   }   private void XMLViewer_Load(object sender, EventArgs e)   {  // TODO: This line of code loads data into the 'gameDBDataSet.Player' table. You can move, or remove it, as needed.  this.playerTableAdapter.Fill(this.gameDBDataSet.Player);  // TODO: This line of code loads data into the 'gameDBDataSet.Game' table. You can move, or remove it, as needed.  this.gameTableAdapter.Fill(this.gameDBDataSet.Game);  // TODO: This line of code loads data into the 'gameDBDataSet.GameType' table. You can move, or remove it, as needed.  this.gameTypeTableAdapter.Fill(this.gameDBDataSet.GameType);  XML2DataSet();   }   private void toolStripButton1_Click(object sender, EventArgs e)   {  addGameType();   }   private void toolStripButton2_Click(object sender, EventArgs e)   {  editGameType();   }   private void toolStripButton3_Click(object sender, EventArgs e)   {  addGame();   }   private void toolStripButton4_Click(object sender, EventArgs e)   {  editGame();   }   private void gameTypeBindingNavigatorSaveItem_Click(object sender, EventArgs e)   {  this.Validate();  this.gameTypeBindingSource.EndEdit();  this.gameTypeTableAdapter.Update(this.gameDBDataSet.GameType);   }   private void addGameType()   {  GameType newGT = new GameType();  newGT.FolderID = 0;  newGT.FolderName = "";  newGT.IsEditable = false;  newGT.ShowDialog();   }   private void addGame()   {  Game newGame = new Game();  newGame.ShowDialog();   }   private void editGameType()   {  GameType newGT = new GameType();  newGT.ShowDialog();   }   private void editGame()   {  Game newGame = new Game();  newGame.ShowDialog();   }   private void loadXML()   {  rssStream = new System.IO.FileStream("game.xml", System.IO.FileMode.Open);  rssDoc = new System.Xml.XmlDocument();  rssDoc.Load(rssStream);  rssStream.Close();   }   private void XML2DataSet()   {  GameDBDataSet.GameTypeRow addedGameTypeRow = gameDBDataSet.GameType.NewGameTypeRow();  //addedGameTypeRow.GameTypeID = 1;  addedGameTypeRow.GameTypeName = "Action";  this.gameDBDataSet.GameType.Rows.Add(addedGameTypeRow);  this.Validate();  //this.gameTypeBindingSource.EndEdit();  int gametyperows = this.gameTypeTableAdapter.Update(this.gameDBDataSet);  this.gameDBDataSet.AcceptChanges();  System.Windows.Forms.MessageBox.Show(gametyperows.ToString());  GameDBDataSet.GameRow addedGameRow = this.gameDBDataSet.Game.NewGameRow();  //addedGameRow.GameID = 1;  addedGameRow.GameName = rssDoc.DocumentElement.Attributes.Item(0).Value.ToString();  addedGameRow.GameTypeID = 1;  this.gameDBDataSet.Game.Rows.Add(addedGameRow);  this.Validate();  //this.gameBindingSource.EndEdit();  int gamerows = this.gameTableAdapter.Update(this.gameDBDataSet);  this.gameDBDataSet.AcceptChanges();  System.Windows.Forms.MessageBox.Show(gamerows.ToString());  /*  addedPlayerRow.PlayerName = rssDoc.DocumentElement.FirstChild.FirstChild.InnerText;  addedPlayerRow.PlayerLevel = rssDoc.DocumentElement.FirstChild.FirstChild.NextSibling.InnerText;  addedPlayerRow.PlayerScore = int.Parse(rssDoc.DocumentElement.FirstChild.FirstChild.NextSibling.NextSibling.InnerText);  addedPlayerRow.PlayerKillRatio = rssDoc.DocumentElement.FirstChild.FirstChild.NextSibling.NextSibling.NextSibling.InnerText;  */    System.Xml.XmlNodeList nameNode = rssDoc.SelectNodes("game/player/name");  System.Xml.XmlNodeList levelNode = rssDoc.SelectNodes("game/player/level");  System.Xml.XmlNodeList scoreNode = rssDoc.SelectNodes("game/player/score");  System.Xml.XmlNodeList killRatioNode = rssDoc.SelectNodes("game/player/kills");    int listCount = nameNode.Count;  for (int i = 1; i < listCount + 1; ++i)  { GameDBDataSet.PlayerRow addedPlayerRow = this.gameDBDataSet.Player.NewPlayerRow(); //addedPlayerRow.PlayerID = 2; addedPlayerRow.PlayerName = nameNode.Item(i-1).InnerText; addedPlayerRow.PlayerLevel = levelNode.Item(i-1).InnerText; addedPlayerRow.PlayerScore = int.Parse(scoreNode.Item(i-1).InnerText); addedPlayerRow.PlayerKillRatio = killRatioNode.Item(i-1).InnerText; addedPlayerRow.GameID = 1; this.gameDBDataSet.Player.Rows.Add(addedPlayerRow); this.Validate(); //this.playerBindingSource.EndEdit(); this.playerTableAdapter.Update(this.gameDBDataSet); this.gameDBDataSet.AcceptChanges();  }  }    }};  somebody please help me..... [bawling]   ;Quote:Original post by Alpha_ProgDesthis is a spin-off from my original question.when i run this code like this:*** Source Snippet Removed ***i got no errors, but when the same code like this:*** Source Snippet Removed ***i get this error: "Column 'PlayerID' is constrained to be unique.  Value '2' is already present."so my question is: "why?"As the anonymous poster stated, it's likely that your DataSet schema isn't the same as your database schema for that table.  You will need to ensure that the playerID column in the table is setup the same as in the database (e.g. auto-increment, primary key, etc...)  As for why, in your second loop you're adding all your rows before updating.  This is usually the correct way to do it.  Without an auto-incrementing key, in the second pass of the loop it'll add the same primary key again and thus you will get your error (if there aren't any unique constraints in your dataset table, then the error will be thrown from the Update method).  The reason the first loop works is that the update is refreshing the primary key ID column after the update to the database is performed, so if you keep passing in 2 as the key value, Update() will update that key value AFTER it's sent the row to the database (usually by using SELECT @@IDENTITY or some such) and thus you will receive a unique key.  So the solution is to ensure you have a auto-incrementing primary key/unique key for PlayerID in your DataSet schema.Hope this helps.   