Click here to Skip to main content
15,895,667 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have an application, in the main form a button "AD NEW".

When I click the button, a new form is open, and on a textbox I want to show the that ID+1:

I use max() method.



C#
private void GetNewId()
        {
            int newID = 0;
            Program.Connection.CommandText = "SELECT MAX(ContractId) AS ContractId FROM Contracts";

            DataTable Table = new DataTable();
            Program.Connection.FillDataTable(Table, true);
            
            newID = Convert.ToInt32(Table.Rows[0]["ContractId"]);
            newID++;
            txtContractNumar.Text = newID.ToString();
        }


I'm looking for a better way. I search over the internet and I saw the method max() is not so good.

I'm using Access, but if possible I'm searching a method that work in mysql, ms sql, and access, if not a method for access.
Posted
Updated 29-Mar-11 14:49pm
v2

You do not ever do this in production code. In a multi-user system, your code WILL generate the same ContractId on multiple clients if they run this code at the same time (and they WILL!).

You let the database assign identity values in autoincrementing columns and use the databases @@SCOPE_IDENTITY equivilent to get back what that Id value is for that record.
 
Share this answer
 
Comments
aciobanita 29-Mar-11 23:26pm    
I try touse @@SCOPE_IDENTITY but in database access not work.
I do not know of one command/function (although see below) that will work for Access, MySql and SqlServer.

My Sql uses LAST_INSERT_ID() or @@IDENTITY (although there are reports that this is broken), Sql Server uses SCOPE_IDENTITY() or IDENT_CURRENT('table') (depending on the circumstances) or @@IDENTITY although once again this is deprecated (and possibly broken) and according to this article[^] (scroll down to the 'Access' heading) JET/OLEDB now also supports @@IDENTITY

This, from MSDN[^] (See the Remarks Section) should help you decide which to use for Sql Server.

So, provided you can confirm for yourself that @@IDENTITY works for all the systems you want to use, you might be able to use that.

Otherwise something like:
C#
public enum DBSystem
{
  Access,
  MySql,
  SqlServer
}

public string GetNextIDString(DBSystem system)
{
  string nextIDString = "SELECT ";
  switch (system)
  {
    case DBSystem.Access:
      nextIDString += "@@IDENTITY .............";  // the ...... represents the rest of the syntax
      break;
    case DBSystem.MySql
      nextIDString += "LAST_INSERT_ID(Contracts) .............";  // the ...... represents the rest of the syntax
      break;
    case DBSystem.SqlServer:
      nextIDString += "@@IDENTITY .............";  // the ...... represents the rest of the syntax
      // OR
      nextIDString += "SCOPE_IDENTITY .............";  // the ...... represents the rest of the syntax
      nextIDString += "IDENT_CURRENT(Contracts) .............";  // the ...... represents the rest of the syntax

      break;
  }

  return nextIDString;
}


I hope that some of this helps.
 
Share this answer
 
Comments
aciobanita 30-Mar-11 0:03am    
Ok,i use @@IDENTITY, but, i press the "Ad NEW", the nextIDString is "1", i save the data i database, and if i press AGAIN the "Ad New" - "nextIDString = the next value". After i close the application it's start over.
aciobanita 30-Mar-11 0:15am    
i observed, i need to inserd the new data,and then get the id.

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