title: Reading data from Excel First Sheet - C#
body: <p>There are 2 Excel Sheets - FileA &amp; FileB. </p>
  
  <p>Only one sheet in FileA. It has data &amp; is named 'Sep'. No Hidden sheets for sure.</p>
  
  <p>FileB has 3 sheets. The first one has data &amp; is named 'Sheet1'.</p>
  
  <p>Testing with FileA, I wrote this code and it works fine. Problem occured with FileB. When checking the FileA schema in code, I see that 'SEP' showed up as Row<a href="https://i.stack.imgur.com/Y20bW.jpg" rel="nofollow noreferrer">2</a>. So, wrote this to get the firstsheet's name</p>
  
  <p><code>string FirstSheetName = dtSchema.Rows[2]["TABLE_NAME"].ToString();</code></p>
  
  <p>But for FileB, Sheet1 shows up as first row in schema.</p>
  
  <ol>
  <li>Why is this different for different sheets.</li>
  <li><p>What should I write, so that i always get the data from firstsheet that has data.</p>
  
  <pre><code> private void GetDataFromExcel()
       {
         DataSet dsExcel = null;
          if (true)
           {
              OleDbCommand cmdSelect = null;
              OleDbDataAdapter daXls = null;
              OleDbConnection xlsCSV = null;            
              DataTable dtSchema = null;
              try
               {
                xlsCSV = new OleDbConnection(ExcelFileConnString);
  
        if (xlsCSV.State == ConnectionState.Closed)
         {
           xlsCSV.Open();
         }
  
        dtSchema = xlsCSV.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, 
                                                    null); 
        //Screenshots of dtSchema attached.                   
        if (dtSchema.Rows.Count &gt; 0)
         {
           string FirstSheetName = dtSchema.Rows[2]["TABLE_NAME"]
                                        .ToString();
           cmdSelect = new OleDbCommand("select * from [" + 
                                                FirstSheetName + "]", 
                                                xlsCSV);
           daXls = new OleDbDataAdapter();
           daXls.SelectCommand = cmdSelect;
           dsExcel = new DataSet();
           daXls.Fill(dsExcel);
           xlsCSV.Close();
         }
  
       int count = dsExcel.Tables[0].Rows.Count;
      }
      catch (Exception)
       {
  
       }
      finally
       {
         if (xlsCSV != null &amp;&amp; xlsCSV.State == ConnectionState.Open)
           {
             xlsCSV.Close();
             xlsCSV.Dispose();
           }
         if (cmdSelect != null)
           {
             cmdSelect.Dispose();
           }
       }
      }        
  }
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/cmVTU.jpg" alt="enter image description here"> <img src="https://i.stack.imgur.com/Y20bW.jpg" alt="enter image description here"></p></li>
  </ol>