title: C# - Using database table to populate variables
body: <p><strong>EDIT: Thank you everyone, I figured out how to get it to work now! Details below...</strong></p>
  
  <p>I'm kind of a newbie to C#, and I'm trying to teach myself the language by programming a really simple RPG game.</p>
  
  <p>Right now, I'm at the point where I want to start adding different enemies to fight (up until now I just used a single one hardcoded in for testing).</p>
  
  <p>I've started setting up a database with enemy info (one column for name, one for HP, one for common stats and attacks, etc.). I have it so that when you start combat with an enemy, the player is able to select a creature from a dropdown, and whichever creature he has will set a variable called "EnemyID".</p>
  
  <p>What I want to do is use that EnemyID variable to correspond to a row in my database, then pull the value of each column into variables that I can then reference during combat.</p>
  
  <p>Is this something that's possible to do? If so, could someone explain the method to me in relatively simple terms? Even just a small example of how to import row data from any kind of database will do, I'm good at understanding code once I see it in use a couple of times.</p>
  
  <p>(Oh yeah, if it matters, I'm using Visual Studio Express 2013, and my database is a SQL Server Express 2014 database.)</p>
  
  <p>Thanks in advance!</p>
  
  <p>EDIT:</p>
  
  <p>After finding a simple tutorial for ADO.NET, and following a suggestion from one of the posters, I've come up with the following code.</p>
  
  <pre><code> public void DataPoll()
      {
          SqlConnection MonDat = new SqlConnection("Data Source=(local);
                Initial Catalog=TestDatabase;Integrated Security=SSPI");
          SqlDataReader rdr = null;
          try
          {
              MonDat.Open();
              SqlCommand cmd = new SqlCommand(
                  "select * from Monsters where Id = EnemyID", MonDat);
              rdr = cmd.ExecuteReader();
              while (rdr.Read())
              {
                  EnemyIDBuffer = (int)rdr["Id"];
                  EnemyName = (string)rdr["Name"];
                  EnemyHPBase = (int)rdr["HP"];
                  EnemyAtkBase = (int)rdr["Atk"];
                  EnemyDefBase = (int)rdr["Def"];
                  EnemyMagBase = (int)rdr["Mag"];
                  PrimAtk = (string)rdr["PrimAtk"];
                  SecoAtk = (string)rdr["SecoAtk"];
                  TertAtk = (string)rdr["TertAtk"];
                  RareAtk = (string)rdr["RareAtk"];
              }
          }
          finally
          {
              if (rdr != null)
              {
                  rdr.Close();
              }
              if (MonDat != null)
              {
                  MonDat.Close();
              }
          }
      }
  </code></pre>
  
  <p>However, when I try to run it, my program stalls and crashes. I'm guessing I have something configured wrong (I just took script from the tutorial and tweaked it slightly). Can anyone give me a hand figuring out where I went wrong?</p>
  
  <p>EnemyID is a variable I used to assign what enemy is fought, based on a menu selection. I'm trying to use that variable to generate the row ID to pull the rest of the row data from, but I think that might be causing an issue. </p>
  
  <p>EDIT2: It took me longer than it really should have, but I figured it out. I had to change my code a little tiny bit.</p>
  
  <pre><code>                SqlCommand cmd = new SqlCommand(
                  "select * from Monsters where Id = " + EnemyID, MonDat);
  </code></pre>
  
  <p>I have a habit of forgetting that you're able to join statements like this. I made a new project that only polled data and threw it into my variables, and from there put it into text boxes, and with this method I was able to poll two different sets of enemy stats by assigning different EnemyID values to two different buttons. Proof of concept, right there.</p>
  
  <p>Thanks to both people who replied, both suggestions were equally useful to getting this working. :)</p>