title: How to store a sql result to a variable in C# using Dapper.NET
body: <p>I'm using <a href="/questions/tagged/dapper" class="post-tag" title="show questions tagged 'dapper'" rel="tag">dapper</a> in my project, a beautiful tool for storing the <code>SQL query</code> results to a <code>List</code> and this working good.</p>
  
  <p>I wrote a <code>SQL query</code> to fetch a record from database and store the result in a variable, I tried using <strong>dapper</strong> but stuck up with an error.  I've pasted the corresponding code.</p>
  
  <blockquote>
    <p><strong>Exception:</strong> Unable to cast object of type
    'System.Collections.Generic.List`1[Dapper.SqlMapper+DapperRow]' to
    type 'System.IConvertible'.</p>
  </blockquote>
  
  <pre><code>try
  {
    using(var connection = ...)
    {
     connection.Open();
     const string masterSelectQuery = "SELECT Id as [fileId], FileName as [fileName], Frequency as [frequency], Scheduled_Time as scheduledTime FROM MASTER_TABLE";
     masterTableList = connection.Query&lt;MasterTableAttributes&gt;(masterSelectQuery).ToList();//Working fine
  
     const string lastProcessedTimeQuery = "SELECT TOP 1 file_transfered_time as [lastProcessedTime] FROM PROCESS_LOGS ORDER BY file_transfered_time DESC";
     DateTime lastProcessedTime = Convert.ToDateTime(connection.Query(lastProcessedTimeQuery)); //Here it fails
    }    
  }
  </code></pre>
  
  <p>To overcome this error I used <code>SQLCommand</code> as follows</p>
  
  <pre><code>using (command = new SqlCommand(lastProcessedTimeQuery, connection))
  {
    DateTime lastProcessedTime = (DateTime)command.ExecuteScalar();//working fine  
  }
  </code></pre>
  
  <p>I am making a mistake in using dapper, can anyone please guide me?</p>
  
  <p>Thanks in advance.</p>