title: C# SqlDataReader for int
body: <p>I am confused by the following issue;</p>
  
  <p>I am trying to retrieve a record (column 'EmployeeId' (int)) using a SqlDataReader.</p>
  
  <p>When I run the query directly on the server, I got the record correctly. When I run the same procedure to get another column (string) I got the record correctly.</p>
  
  <p>I assume the <strong>issue is due to the fact that I try to get an int</strong>, the program skips the          </p>
  
  <pre><code>while (mySqlDbDataReader.Read())
  </code></pre>
  
  <p>and goes directly to </p>
  
  <pre><code>catch (Exception eMsg1)
              {
  </code></pre>
  
  <p>Here is the full procedure</p>
  
  <pre><code>public string getUserId()
  {
      //Check user details against login in Db
      //Local variables to capture values from Db
      string varId = "";
  
      //Connection string
      conn = sqlDbConnection.GetConnection();
      SqlCommand newCmd = conn.CreateCommand();
      newCmd.Connection = conn;
      newCmd.CommandType = CommandType.Text;
  
      //Query
      userCredentials connectedUser = new userCredentials();
      newCmd.CommandText = "SELECT EmployeeId FROM tblEmployees WHERE WinLogin='" + connectedUser.getWindowsLogin() + "';";
  
      //Connect
      newCmd.Connection = conn;
      conn.Open();
  
      //Utilisation du try-catch permettant de fermer la connexion même en cas de plantage
      try
      {
          SqlDataReader mySqlDbDataReader = newCmd.ExecuteReader();
          while (mySqlDbDataReader.Read())
          {
              //Tester que le résultat n’est pas NULL afin d’éviter un plantage au moment du crash
              if (mySqlDbDataReader["EmployeeId"] != System.DBNull.Value)
              {
                  //Récupérer le nom à l’aide d’un cast
                  varId = (string)mySqlDbDataReader["EmployeeId"];                        
              }
              else
              {
                  varId = "Unknown";                       
              }
          }
      }
      catch (Exception eMsg1)
      {
          //In the event of trouble, display error msg
          //MessageBox.Show("Error while executing query: " + eMsg1.Message);
          Console.WriteLine(eMsg1);
  
          //Control
          MessageBox.Show("Empty");
      }
      finally
      {
          //Close DB connection
          conn.Close();
      }
  
      return varId;
  }`
  </code></pre>
  
  <p>Thanks for your help!</p>
  
  <p>Brice</p>