title: Retrieving total count from database to c#
body: <p>This query is been executed in database.</p>
  
  <pre><code> select COUNT(*) from Patient_Data where  DummyValue = 'Y';
  
   104
  </code></pre>
  
  <p>I have to retrieve this <code>number (104)</code> from <code>database</code> to <code>asp.net with c#</code> so that when the count becomes zero I have to <code>disable a button</code>, How to <code>retrieve</code> this number from database into the code. It should be stored as <code>integer</code>.</p>
  
  <p>I have tried these line of code in c#</p>
  
  <pre><code>    using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString))
      {
          using (SqlCommand cmd = new SqlCommand("select COUNT(*) as PatientCount from Patient_Data where  DummyValue = 'Y' ", cn))
          {
              try
              {
  
                  cn.Open();
  
                  using (SqlDataReader rdr = cmd.ExecuteReader())
                  {
                      int Patcount;
                      if (rdr.Read())
                      {
                          //Code Required
  
                      }
                  }
              }
              catch (Exception ex)
              {
  
                  // handle errors here
              }
  
          }
  
  
      }
  </code></pre>