Click here to Skip to main content
15,908,112 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi iam trying to select sum of total in excel sheets and add it into a list box each one of them in a new line

here is my code

C#
private void btn_update_Click(object sender, EventArgs e)
    {

        for (int i = list_sheetnames.Items.Count -1; i >= 0; i--)
        {


            string connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + textBox_filename.Text + ";Extended Properties='Excel 12.0 XML;HDR=YES;';";

            string selectCmd = "Select SUM(Total) As Total_SUM From [" + list_sheetnames.Items[i] + "$]";

            using(OleDbConnection excelConn = new OleDbConnection(connString))
            {
                excelConn.Open(); 
                OleDbCommand command = new OleDbCommand(selectCmd, excelConn);
                OleDbDataAdapter da = new OleDbDataAdapter(command);

                DataTable sheetInfo = new DataTable();
                da.Fill(sheetInfo);

                //Do something with the data.
                //list_total.Items.Add(sheetInfo);
                list_total.DataSource = da.ToString();
            }
}

and i got this error

could you please help me to find the solution

i change my code into this

C#
List<string> lst = new List<string>();
                    foreach (DataRow r in sheetInfo.Rows)
                    {
                        string sheettotal = (string)r["Total_SUM"].ToString();
                        lst.Add(sheettotal);
                        
                    }
                    list_total.DataSource = lst;


and its working but only add the last total in the list box
Posted
Updated 26-Jan-16 20:01pm
v3
Comments
Richard MacCutchan 26-Jan-16 9:11am    
What error?
Developer It 27-Jan-16 0:41am    
this is the error
Additional information: Complex DataBinding accepts as a data source either an IList or an IListSource

1 solution

Quote:

C#
list_total.DataSource = da.ToString();


You're setting the data source of the list to a string containing "System.Data.OleDb.OleDbDataAdapter".

You need to set it to the data returned by the query instead:
C#
list_total.DataSource = sheetInfo;
 
Share this answer
 
Comments
Developer It 27-Jan-16 0:40am    
i try this but i get System.Data.DatarowView in the list view instead of the totals
Richard Deeming 27-Jan-16 7:47am    
Set the list's DisplayMember property to the name of the column you want to display.

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