Click here to Skip to main content
15,888,065 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Problem

Datagridview show only last record although I have more rows when currentreading excel is less than currentreading invoice table .

wrong result in else statement of btn_import click inside for loop count
I need to modify code in else to accept list not show only one record .

Details

I have excelsheet as following :

unitcode CurrentReading
12 2000
14 4500

and I have table Invoice in database in sql server 2012 has following fields

serial unitcode CurrentReading year month
1 12 1000 2018 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

btn import that import from excel

C#
btn_import click
{
for (int i = 0; i < 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()) && ConvertToDecimal(dt.Rows[i]["CurrentMeterReading"]) > 0 && ConvertToDecimal(dt.Rows[i]["CurrentMeterReading"]) > ConvertToDecimal(LastMeterReading))
  {
//insert to table invoice
  }
else
{
System.Data.DataTable dtErrorRows = new DataTable();
 DataView  dv2 = new DataView(dt);
 dv2.RowFilter = "(CurrentMeterReading < " + LastMeterReading + ")" + "and" + "(UnitCode = " + Utilities.ObjectConverter.ConvertToInteger(dt.Rows[i]["UnitCode"]) + ")";
 dv2.RowStateFilter = DataViewRowState.CurrentRows;
 int a = dv2.Count;
 if (dv2.Count > 0)
   {
dtErrorRows = dv2.ToTable();
   }                                       
}
}
Grid.DataSource = dtErrorRows;
}
 function get data from excel
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;


    }

for selecting currentreading from Invoice Table :

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));
    }

Expected Result in datagridview

unitcode CurrentReading

14 4500

What I have tried:

Datagridview show only last record although I have more rows when compare currentreading excel and invoice table
Posted
Updated 24-Apr-18 17:31pm

1 solution

At least few things to notice

When you loop through the rows from Excel, you set a new data table and a new dataview on each iteration when the amount is less than what is found in the database. This causes the situation where only the last row remains.

You should use a single data table and in the loop, just insert the desired rows to this table. After the loop use the data table for binding etc

Another thing is, when you query the database yo concatenate the values directly into the SQL statement. This leaves you open to SQL injections and introduces possible conversion problems. For more info, see SQL injection - Wikipedia[^]

The proper way would be to use SqlParameter Class (System.Data.SqlClient)[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900