Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
private static int GenerationId()
{
    OleDbCommand Command = new OleDbCommand();
    Command.Connection = ConnectionDB.OLEConnection;

    Command.CommandText = "Select MAX(Client_ID) from Client";
    ConnectionDB.OLEConnection.Open();


    int? Client_ID = (int?)CommandGenerationId.ExecuteScalar(); //i get exception here


    ConnectionDB.OLEConnection.Close();
    return (int)Client_ID;
}
Posted
Comments
Bernhard Hiller 25-Mar-13 7:36am    
By the way, the ID should be generated by the sql server - use an autoincrement column or something like that depending on your database system.
When you insert a new row into the table, you do not provide the id at all. You can query it afterwards with a SELECT @@Identity (or similar queries)

1 solution

There are always many ways of doing things but this is how I do it:

C#
Object tempVal = CommandGenerationId.ExecuteScalar();
Int32 returnVal;

if (tempVal != null)
    returnVal = Int32.Parse(tempVal);


Something along those lines. For me, this approach works better than trying to cram it into a single line.
 
Share this answer
 
Comments
Anderso0on 22-Mar-13 16:17pm    
CommandGenerationId.ExecuteScalar()does not return null, it return {} when find nothig
ZurdoDev 22-Mar-13 16:24pm    
If your query returns no records then tempVal will be null.

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