Click here to Skip to main content
15,923,197 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi I would like something like this:

C#
public int FindMaxId(string TableName)
     {
       int i =0;
        if ('db.'+TableName+'.ToList().Count() > 0' )
           i = db. TableName.Max(d => d.id);
        return i ;
     }


i know it's wrong .i wanna pass the name of table , and it returns max id. for any table.
Posted
Comments
Bernhard Hiller 2-Sep-13 2:32am    
Such a function has a bad smell: what do you need it for? Do you want to create the "next insert id" yourself?
Member 8454063 2-Sep-13 8:01am    
yes .your guess is true. i'm coding entity framwork
Bernhard Hiller 2-Sep-13 8:16am    
Do not do that! That's a job for the database engine. Only there can concurrency (two users inserting at the same time) be handled correctly. With SQL Server or MS Access, use the Identity or AutoIncrement specifiers for the corresponding column, with Oracle ... bad luck, you'll need a sequence and a trigger.

1 solution

Hi...
this code may help you
C#
Public GetMaxid()
{
 con.Open();
            OleDbCommand cmd1 = new OleDbCommand("Select Max(ProductGroupCode) as maxid from ProductGroup_master", con);

            OleDbDataReader oledbrd1 = cmd1.ExecuteReader();

            oledbrd1.Read();
            if (Convert.IsDBNull(oledbrd1["maxid"]))//check if db is NULL 
            {
                strid1 = 1;
                txt_pgrpcode.Text = "1";
            }
            else
            {
                strid1 = Convert.ToInt32(oledbrd1["maxid"]) + 1;
                txt_pgrpcode.Text = strid1.ToString();
            }
            oledbrd1.Close();
}




Hpaay Coding :) :)
 
Share this answer
 
Comments
Member 8454063 2-Sep-13 9:04am    
thanks . but i use enttiy framwork

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