Click here to Skip to main content
15,746,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear all,
I am doing a Windows Form where I had to auto generate a Job code which is unique.
This I had done by selecting the last row added from the database and incrementing jobpk(auto generating primary key) with one and concatenating with a string code.

I had code as below, but the query returns an exception at
C#
dAdapter.Fill(ds, "tbljobrdataview");

Syntax error in ORDER BY clause.

My code is:
C#
public void newjobecodenoget(){
           OleDbConnection oleDbConnection1 = new System.Data.OleDb.OleDbConnection(connString);
           oleDbConnection1.Open();


           String query = "Select jobpk from jobcodemastertable ORDER BY jobpk DESC LIMIT 1";
           OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, connString);


           DataSet ds = new DataSet();

           dAdapter.Fill(ds, "tbljobrdataview");
           Int32  S = int.Parse(ds.Tables[0].Rows[0][0].ToString());
           S++;
            MessageBox.Show("" +S);
            String jobcodeno = "NFT" + S.ToString();
            txtjobcode.Text = jobcodeno.ToString();

       }

I call this function at constructor and I am using Access database.
jobpk is primary key in long integer format.

Can anyone help to sort out my mistake?

I have tried using quotes('') to jobpk in query but same result
Posted
Updated 18-Feb-12 8:43am
v2
Comments
André Kraak 18-Feb-12 14:43pm    
Edited question:
Added pre tags
Spelling/Grammar

If I am not mistaken Access does not know the LIMIT keyword.

Instead of using
SQL
Select jobpk from jobcodemastertable ORDER BY jobpk DESC LIMIT 1

try using
SQL
SELECT TOP 1 jobpk FROM jobcodemastertable ORDER BY jobpk DESC

instead.
 
Share this answer
 
Try this
SQL
"Select Top(1) jobpk from jobcodemastertable ORDER BY jobpk DESC"


i'm not sure i try in sqlserver 2005 but
C#
"Select jobpk from jobcodemastertable ORDER BY jobpk DESC LIMIT 1"

not work but i can use
SQL
"Select Top(1) jobpk from jobcodemastertable ORDER BY jobpk DESC"
 
Share this answer
 
v2
Hi, try below query

SQL
String query = "Select Max(jobpk) from jobcodemastertable"


or

SQL
String query = "Select top 1 jobpk from jobcodemastertable order by jobpk DESC"
 
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