title: Datagridview show only last record although I have more rows when compare currentreading excel and invoice table
body: <p><strong>Problem</strong></p>
  
  <p>Datagridview show only last record although I have more rows when  currentreading excel is less than currentreading  invoice table .</p>
  
  <p>Wrong result in else statement of btn_import click inside for loop count</p>
  
  <p><strong>Details</strong></p>
  
  <p>I have excelsheet as following :</p>
  
  <pre><code>unitcode   CurrentReading
  12          2000
  14          4500
  </code></pre>
  
  <p>and I have table Invoice in database in sql server 2012 has following fields</p>
  
  <pre><code>serial     unitcode       CurrentReading   year  month
    1           12                 1000      2018   3
    3           14                 5000      2018   3
  </code></pre>
  
  <p>so that how to display unit code 14 in datagridview .</p>
  
  <p>Actually what i need is when currentreading for unit code in excel sheet is less than currentreading
  in table invoice display the values in datagridview .
  the formula as following</p>
  
  <p>btn import that import from excel
  </p>
  
  <pre><code>private void btn_import_Click(object sender, EventArgs e)
  {
      for (int i = 0; i &lt; dt.Rows.Count; i++)                               
      {
          System.Data.DataTable dt = new System.Data.DataTable();
          dt = Showdataprint();
          decimal LastMeterReading = ConvertToDecimal(GetLastReading(dt.Rows[i]["UnitCode"].ToString()));
          if (!string.IsNullOrEmpty(dt.Rows[i]["CurrentMeterReading"].ToString()) &amp;&amp; ConvertToDecimal(dt.Rows[i]["CurrentMeterReading"]) &gt; 0 &amp;&amp; ConvertToDecimal(dt.Rows[i]["CurrentMeterReading"]) &gt; ConvertToDecimal(LastMeterReading))
          {
              //insert to table invoice
          }
          else
          {
              System.Data.DataTable dtErrorRows = new DataTable();
              DataView  dv2 = new DataView(dt);
              dv2.RowFilter = "(CurrentMeterReading &lt; " + LastMeterReading + ")" + "and" + "(UnitCode = " + Utilities.ObjectConverter.ConvertToInteger(dt.Rows[i]["UnitCode"]) + ")";
              dv2.RowStateFilter = DataViewRowState.CurrentRows;
              int a = dv2.Count;
              if (dv2.Count &gt; 0)
              {
                  dtErrorRows = dv2.ToTable();
              }                                       
          }
      }
      Grid.DataSource = dtErrorRows;
  }
  </code></pre>
  
  <p>function get data from excel
  </p>
  
  <pre><code>public System.Data.DataTable Showdataprint()
  {
      string connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\";", txtpath.Text);
      OleDbConnection con = new OleDbConnection(connectionString);
      con.Open();
      string str = @"SELECT    [UnitCode],[CurrentMeterReading] FROM  [Sheet5$] ";
      OleDbCommand com = new OleDbCommand();
      com = new OleDbCommand(str, con);
      OleDbDataAdapter oledbda = new OleDbDataAdapter();
      oledbda = new OleDbDataAdapter(com);
      DataSet ds = new DataSet();
      ds = new DataSet();
      oledbda.Fill(ds, "[Sheet5$]");
      con.Close();
      System.Data.DataTable dt = new System.Data.DataTable();
      dt = ds.Tables["[Sheet5$]"];
      return dt;
  }
  </code></pre>
  
  <p>For selecting currentreading from Invoice Table :
  </p>
  
  <pre><code>private decimal GetLastReading(string UnitCode)
  {
      string sqlquery = "";
      sqlquery = @"select isnull((select top 1 CurrentMeterReading from WAHInvoice where UnitCode=" + UnitCode + " order by year , Serial desc),0)";
      object nullableValue = DataAccess.ExecuteScalar(sqlquery);
      decimal myValue;
      if (nullableValue == null || nullableValue == DBNull.Value)
      {
          myValue = 0;
      }
      else
      {
          decimal.TryParse(nullableValue.ToString(), out myValue);
      }
      return Utilities.ObjectConverter.ConvertToDecimal(DataAccess.ExecuteScalar(sqlquery));
  }
  </code></pre>
  
  <p>Expected Result in datagridview</p>
  
  <pre><code>unitcode   CurrentReading
  
  14          4500
  </code></pre>