title: Why can't I save my Xbox 360 game in XNA?
body: <p>I'm having problems with saving my game and I've spent hours searching for solutions without any luck. I used this code that was written in someone's blog:</p>
  
  <pre><code>public class SaveandLoad
  {
      StorageDevice device;
      string containerName = "ChainedWingsContainer";
      string filename = "mysave.sav";
      public struct SaveGame
      {
          public int s_mission;
      }
  
      public void InitiateSave()
      {
          if (!Guide.IsVisible)
          {
              device = null;
              StorageDevice.BeginShowSelector(PlayerIndex.One, this.SaveToDevice, null);
          }
      }
  
      void SaveToDevice(IAsyncResult result)
      {
          device = StorageDevice.EndShowSelector(result);
          if (device != null &amp;&amp; device.IsConnected)
          {
              SaveGame SaveData = new SaveGame()
              {
                  s_mission = Game1.mission,
              };
              IAsyncResult r = device.BeginOpenContainer(containerName, null, null);
              result.AsyncWaitHandle.WaitOne();
              StorageContainer container = device.EndOpenContainer(r);
              if (container.FileExists(filename))
                   container.DeleteFile(filename);
              Stream stream = container.CreateFile(filename);
              XmlSerializer serializer = new XmlSerializer(typeof(SaveGame));
              serializer.Serialize(stream, SaveData);
              stream.Close();
              container.Dispose();
              result.AsyncWaitHandle.Close();
          }
      }
  
      public void InitiateLoad()
      {
          if (!Guide.IsVisible)
          {
              device = null;
              StorageDevice.BeginShowSelector(PlayerIndex.One, this.LoadFromDevice, null);
          }
      }
  
      void LoadFromDevice(IAsyncResult result)
      {
          device = StorageDevice.EndShowSelector(result);
          IAsyncResult r = device.BeginOpenContainer(containerName, null, null);
          result.AsyncWaitHandle.WaitOne();
          StorageContainer container = device.EndOpenContainer(r);
          result.AsyncWaitHandle.Close();
          if (container.FileExists(filename))
          {
              Stream stream = container.OpenFile(filename, FileMode.Open);
              XmlSerializer serializer = new XmlSerializer(typeof(SaveGame));
              SaveGame SaveData = (SaveGame)serializer.Deserialize(stream);
              stream.Close();
              container.Dispose();
              //Update the game based on the save game file
              Game1.mission = SaveData.s_mission;
          }
      }
  }
  </code></pre>
  
  <p>But whenever I run it, I get this message:</p>
  
  <p>An unhandled exception of type 'System.InvalidOperationException' occurred in Microsoft.Xna.Framework.Storage.dll</p>
  
  <p>Additional information: A new container cannot be opened until all previouses containers used by this PlayerIndex have been disposed.</p>
  
  <p>I've looked around for answers and most of the suggestions suggest to use the Using statement. So I used Using like so:</p>
  
  <pre><code>    void SaveToDevice(IAsyncResult result)
      {
          device = StorageDevice.EndShowSelector(result);
          if (device != null &amp;&amp; device.IsConnected)
          {
              SaveGame SaveData = new SaveGame()
              {
                  s_mission = Game1.mission,
              };
              IAsyncResult r = device.BeginOpenContainer(containerName, null, null);
              result.AsyncWaitHandle.WaitOne();
              using (StorageContainer container = device.EndOpenContainer(r))
              {
                  if (container.FileExists(filename))
                      container.DeleteFile(filename);
                  Stream stream = container.CreateFile(filename);
                  XmlSerializer serializer = new XmlSerializer(typeof(SaveGame));
                  serializer.Serialize(stream, SaveData);
                  stream.Close();
                  container.Dispose();
              }
              result.AsyncWaitHandle.Close();
          }
      }
  </code></pre>
  
  <p>But I still get the same result. What am I doing wrong? </p>