Click here to Skip to main content
15,921,941 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to get sum of one column using filter of another column but get a blank string in datatable. Newbie here.. Please help.

What I have tried:

da = new OleDbDataAdapter("select sum("+ sum_src.Text +") as summ from [Sheet1$] where [" + criteria_src.Text + "] = [" + col_row.Text + "]", conn);
Posted
Updated 13-Jan-20 6:43am

The query seems a bit add. Do you mean to compare values in an excel file to a value provided by the user? If that is the case then the comparison should probably be something else, something like
da = new OleDbDataAdapter("select sum("+ sum_src.Text +") as summ from [Sheet1$] where [" + criteria_src.Text + "] = [Conditioncolumn]", conn); 

Also the column to be summed probably would need to be named, something like
da = new OleDbDataAdapter("select sum([ColumnToBeSummed]) as summ from [Sheet1$] where [" + criteria_src.Text + "] = [Conditioncolumn]", conn);
 
Share this answer
 
Comments
cotdarz 13-Jan-20 13:32pm    
All the three combobox are to be summoned by user from selected excel_file . The first and second combobox loads all the column_names and the third combobox loads the column_data from the second column name selected. I have reached up to this and the column_names in the sheet does not contain spaces.

All i want to achieve is "select sum(amount) from [Sheet1$] where customer=John"
{with the three user selected combobox}

#OriginalGriff Thanks for all the superb advice, I will learn the debugging! #Wendelius wonderful comment!
Wendelius 13-Jan-20 13:36pm    
In that case, when you refer the columns in the SQL statement, use the brackets around them. At the moment the summary column is without brackets. Like in your example
select sum([amount]) from [Sheet1$] where [customer]='John'"

Also make sure that those columns exist in the sheet
cotdarz 13-Jan-20 14:04pm    
Thank you. I have done it. I was missing single quote on the 'John' part and added bracket to [John] instead :D
Wendelius 13-Jan-20 23:00pm    
Glad it got solved!
First off, be aware that that is very dangerous: Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

Secondly, you need to start looking at the actual content of your variables to work out what is going on here, particularly the content of criteria_src.Text and the column data it references, and the same for col_row.Text and it's data. We can't do that for you - we just don't have any access to the variables, or the data they reference.

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it then a quick Google for "Visual Studio debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!

I'd start by returning the actual rows instead of the sum: that should give you a clue as to what is actually going on.
 
Share this answer
 
Comments
Wendelius 13-Jan-20 13:39pm    
OP's compliments: #OriginalGriff Thanks for all the superb advice, I will learn the debugging!

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