Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using an SQLite database from a C#/WPF application. I have a table that has a column that is declared as INTEGER UNIQUE.

I execute the SQL script below from my usual ExecuteReader() code and I get one single result, which is what I would expect. The problem is that the result is always zero. Does anyone know what I may be doing wrong?

SQL
SELECT max( enquiry_job_number ) FROM EnquiryJobs;


What I have tried:

Looking in the debugger, it appears that the column does not exist. Even if I add an AS clause to the SQL (shown below), the named column SQLiteDataReader.GetOrdinal( columnName ) returns -1.

SQL
SELECT max( enquiry_job_number ) FROM EnquiryJobs AS "ABC";
Posted
Updated 29-Nov-16 6:03am
v2
Comments
ZurdoDev 29-Nov-16 11:36am    
ExecuteScalar() would likely be a better option. It returns the first column of the first row. Otherwise, post the code for ExecuteReader and the code around it to see what you might be doing wrong.
Patrick Skelton 29-Nov-16 11:46am    
Outstanding! That works perfectly. That might have occurred to me before Christmas if I were lucky. Thank you.

In addition to RyanDev's excellent suggestion, it's also worth noting that if you use this SQL:
SQL
SELECT max( enquiry_job_number ) FROM EnquiryJobs;
You cannot access the data from a DataReader like this:
C#
SQLiteDataReader.GetOrdinal( columnName )
Becaus eyou haven;'t given the data column a name at all.
In future, try naming the column when you return it:
SQL
SELECT max( enquiry_job_number ) AS MaxJobNo FROM EnquiryJobs;
And you can access the column by name:
C#
int max = (int) myDataReader["MaxJobNo"];
 
Share this answer
 
Comments
Patrick Skelton 29-Nov-16 12:27pm    
Thank you for that. I do normally name return values. I wrongly assumed that the name would be the same as the column name.
OriginalGriff 29-Nov-16 12:29pm    
Not when you apply an aggregate function!
As mentioned in comments use ExecuteScalar instead since it returns the first column of the first row.

And to use DataReader see Solution 1.
 
Share this answer
 
v2
Comments
OriginalGriff 29-Nov-16 12:30pm    
I'm glad you posted that as a solution - it gave me a chance to upvote it!
:thumbsup: !
Patrick Skelton 29-Nov-16 12:32pm    
One problem I am having with ExecuteScalar() is that when it is first run on an empty database, I am not getting null. I get an object that simply shows an empty set of brackets in the debugger. I'm not sure what this is or, more importantly, what if() statement I can write to catch this first run.
ZurdoDev 29-Nov-16 12:38pm    
Object temp = cmd.ExecuteScalar();
if (temp != null){
String jobNo = temp.ToString();
}
Patrick Skelton 29-Nov-16 13:38pm    
I'm afraid that doesn't appear to work. I test against null, get false, but then get an exception when I try to cast the result to an int. Do I need to test for DbNull or something? (As you've no doubt gathered, I'm no SQL expert.)
ZurdoDev 29-Nov-16 13:52pm    
What is the result of temp.ToString()?

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