title: Cast string to enum
body: <p>I have a method in C# that calls a stored procedure and fills out a <code>DataTable</code> with values. I have problem in a line that the return value from my stored procedure is a string whereas the property that I am using to keep the value in my C# comes from a class that the type is an enum. I try to cast but I keep getting this error: </p>
  
  <blockquote>
    <p>Specified cast is not valid.</p>
  </blockquote>
  
  <p>This is my method to call the stored procedure:</p>
  
  <pre><code>public void GetOrder()
  {
      ConnectionStringSettings connectionString = ConfigurationManager.ConnectionStrings["T_DB"];
      string conString = connectionString.ConnectionString;
  
      DataSet ds = new DataSet();
  
      using ( SqlConnection con = new SqlConnection(conString))
      {
          SqlCommand cmd = new SqlCommand("LH_Get_order", con);
          cmd.Parameters.AddWithValue("@onum", 45642);
          cmd.CommandType = CommandType.StoredProcedure;
  
          SqlDataAdapter ad = new SqlDataAdapter(cmd);
          ad.Fill(ds);
  
          if (ds.Tables[0].Rows.Count &gt; 0)
          {
              this.BatchId = ds.Tables[0].Rows[0]["gbo_batch_id"].ToString();
              this.ExternalRefId = ds.Tables[0].Rows[0]["o_num"].ToString();
              this.LoanId = ds.Tables[0].Rows[0]["o_loan"].ToString();
              this.OrderType = (eOrderType)ds.Tables[0].Rows[0]["OrderType"]; //problem
          }
      }
  }
  </code></pre>
  
  <p>Problem occurs on this line:</p>
  
  <pre><code>this.OrderType = (eOrderType)ds.Tables[0].Rows[0]["OrderType"];
  </code></pre>
  
  <p>This is my <code>OrderType</code> property:</p>
  
  <pre><code>public eOrderType OrderType
  {
      get { return _OrderType; }
      set { _OrderType = value; }
  }
  </code></pre>
  
  <p>and this is <code>eOrderType</code> enum:</p>
  
  <pre><code>public enum eOrderType : int {
  
      [System.Runtime.Serialization.EnumMemberAttribute(Value="CVA BPO")]
      CVABPO = 1,
  
      [System.Runtime.Serialization.EnumMemberAttribute()]
      ExteriorBPO = 2,
  
      [System.Runtime.Serialization.EnumMemberAttribute()]
      Inspection = 3,
  
      [System.Runtime.Serialization.EnumMemberAttribute()]
      RepairEstimate = 4,
  }
  </code></pre>