ahmed elbarbary

ahmed elbarbary

  • NA
  • 1.6k
  • 254.5k

How to display records in datagridview in case of currentrea

Apr 24 2018 7:15 PM
How to display records in datagridview in case of currentreading in excel is less than current reading in invoice table for same unitcode
I have excelsheet as following
  1. unitcode   CurrentReading  
  2. 12                 2000  
  3. 14                 4500 
and I have table Invoice in database in sql server 2012 has following fields
  1. serial unitcode CurrentReading   year  month  
  2. 1            12            1000                 2018     3  
  3. 3            14            5000                 2018     3 
so that how to display unit code 14 in datagridview .
 
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
if(currentreading in excel for unitcode < currentreading in table invoice for unitcode
display these wrong values in dat gridview as
unitcode currentrading
first function get data from excel
  1. public System.Data.DataTable Showdataprint()  
  2. {  
  3. 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);  
  4.   
  5. OleDbConnection con = new OleDbConnection(connectionString);  
  6. con.Open();
  7. string str = @"SELECT [UnitCode],[CurrentMeterReading] FROM [Sheet5$] ";  
  8. OleDbCommand com = new OleDbCommand();  
  9. com = new OleDbCommand(str, con);  
  10. OleDbDataAdapter oledbda = new OleDbDataAdapter();  
  11. oledbda = new OleDbDataAdapter(com);  
  12. DataSet ds = new DataSet();  
  13. ds = new DataSet();  
  14. oledbda.Fill(ds, "[Sheet5$]");  
  15. con.Close();  
  16. System.Data.DataTable dt = new System.Data.DataTable();  
  17. dt = ds.Tables["[Sheet5$]"];  
  18. return dt;
  19. }  
  20. for selecting currentreading from Invoice Table  
  21. public System.Data.DataTable GetDataPreviousMonthes(string UnitCode)  
  22. {  
  23. sqlquery = @"select top 1 CurrentMeterReading,RequiredAmount,CurrentReadingDate,LastReadingDate,UnitCode from WAHInvoice where UnitCode=" + UnitCode + " order by year,Serial desc";  
  24. System.Data.DataTable tb = DataAccess.ExecuteDataTable(sqlquery);  
  25. return tb;  
  26. }  
  27. public static DataTable ExecuteDataTable(string sql, DbParameter[] dbprmParameters = null)  
  28. {  
  29. return ExecuteDataTable(sql, null, dbprmParameters);  
  30. }  
  31. public static DataTable ExecuteDataTable(string sql, IDbConnection dbConnection, DbParameter[] @params = null)  
  32. {  
  33. if (sql == ""return new DataTable();  
  34. DataSet ds = new DataSet();
  35. #region New Connection
  36. #endregion
  37. #region Old Connection  
  38. lock (synObj)  
  39. {
  40. sql = AnalyizeBooleanFields(sql);  
  41. cmd.CommandText = sql;  
  42. cmd.Parameters.Clear();  
  43.   
  44. if (@params != null)  
  45. {  
  46. for (int i = 0; i < @params.Length; i++)  
  47. {  
  48. cmd.Parameters.Add(@params[i]);  
  49. }  
  50. }  
  51. if (dbConnection == null)  
  52. {  
  53. if (WithTransaction)  
  54. dbConnection = BeginTransaction();  
  55. else  
  56. dbConnection = InitializeConnection();  
  57. }  
  58. if (dbConnection.State != ConnectionState.Open) dbConnection.Open();  
  59. if (WithTransaction) cmd.Transaction = _transaction;  
  60. cmd.Connection = dbConnection;  
  61. IDbDataAdapter dbCurrentDataAdapter = InitializeDataAdapter();  
  62. dbCurrentDataAdapter.SelectCommand = cmd;  
  63. dbCurrentDataAdapter.Fill(ds);  
  64.   
  65. if (!WithTransaction) dbConnection.Close();  
  66. }  
  67.   
  68. return ds.Tables[0];  
  69. #endregion  

Now How to compare currentreading from excel and currentreading from invoice table
and if currentreading in excel is less than invoice table
show record in datagridview
as example above
unitcode CurrentReading
14 4500

Answers (1)