title: How to get Guid from SqlDataReader uniqueidentifier
body: <p>SQL Server 2008 (version 10.50.2550).</p>
  
  <p>I have a select query to return a single column of type uniqueidentifier.</p>
  
  <p>I want to get this into a C# variable of type Guid.</p>
  
  <p>All of the following methods result in exceptions.</p>
  
  <p>reader is of type SqlDataReader.</p>
  
  <pre><code>using (var reader = command.ExecuteReader())
  {
  
      if (reader.Read())
      {       
          Guid guid = reader.GetGuid(reader.GetOrdinal("integ_schemehistoryId")); //1
  
          Guid guid = Guid.Parse((string)reader["integ_schemehistoryId"]); //2
  
          Guid guid = (Guid)reader["integ_schemehistoryId"]; //3
  
          Guid guid = new Guid((string)reader["integ_schemehistoryId"]); //4
  
          Guid guid = Guid.Parse(reader["integ_schemehistoryId"].ToString()); //5
      }
  
  }
  </code></pre>
  
  <p>Error for 1:</p>
  
  <pre><code>System.Data.SqlClient.SqlException (0x80131904): Conversion failed when converting from a character string to uniqueidentifier.
     at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
     at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
     at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
     at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
     at System.Data.SqlClient.SqlDataReader.HasMoreRows()
     at System.Data.SqlClient.SqlDataReader.ReadInternal(Boolean setTimeout)
     at System.Data.SqlClient.SqlDataReader.Read()
     at Simego.DataSync.DynamicColumns.DataSourceRowOverride.get_EpochSchemeHistoryID()
  </code></pre>
  
  <p>Error for 2:</p>
  
  <pre><code>System.InvalidCastException: Unable to cast object of type 'System.Guid' to type 'System.String'.
     at Simego.DataSync.DynamicColumns.DataSourceRowOverride.get_EpochSchemeHistoryID()
  </code></pre>
  
  <p>Error for 3:</p>
  
  <pre><code>System.Data.SqlClient.SqlException (0x80131904): Conversion failed when converting from a character string to uniqueidentifier.
     at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
     at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
     at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
     at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
     at System.Data.SqlClient.SqlDataReader.HasMoreRows()
     at System.Data.SqlClient.SqlDataReader.ReadInternal(Boolean setTimeout)
     at System.Data.SqlClient.SqlDataReader.Read()
     at Simego.DataSync.DynamicColumns.DataSourceRowOverride.get_EpochSchemeHistoryID()
  </code></pre>
  
  <p>Error for 4:</p>
  
  <pre><code>System.InvalidCastException: Unable to cast object of type 'System.Guid' to type 'System.String'.
     at Simego.DataSync.DynamicColumns.DataSourceRowOverride.get_EpochSchemeHistoryID()
  </code></pre>
  
  <p>Error for 5:</p>
  
  <pre><code>System.Data.SqlClient.SqlException (0x80131904): Conversion failed when converting from a character string to uniqueidentifier.
     at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
     at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
     at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
     at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
     at System.Data.SqlClient.SqlDataReader.HasMoreRows()
     at System.Data.SqlClient.SqlDataReader.ReadInternal(Boolean setTimeout)
     at System.Data.SqlClient.SqlDataReader.Read()
     at Simego.DataSync.DynamicColumns.DataSourceRowOverride.get_EpochSchemeHistoryID()
  </code></pre>