Solved

Finding the autoid number of a sql insert query (.net frameowork)

Posted on 2003-11-05
2
869 Views
Last Modified: 2007-12-19
is it possible to find the autoincrement id of a record that has been inserted without doing another call to the database. I need to be able to do this in asp.net.

Here is an example:
The table consists of three columns; id (autoincrement), username, password

Dim oConn As sqlConnection
Dim oCmd As sqlCommand
Dim SQLQuery As String

SQLQuery = "INSERT INTO TB_Names "
SQLQuery = " (username, password) " & _
                   " Values('kevin','password')

oCmd = new SqlCommand(SQLQuery)
oCmd.Connection = oConn
oConn.Open()
oCmd.ExecuteNonQuery()
oCmd.Connection.Close()

surely there should be something like oCmd.max(id)
0
Comment
Question by:ipokkel
2 Comments
 
LVL 28

Assisted Solution

by:sybe
sybe earned 125 total points
ID: 9685277
Right after the insert do this:

sSQL = "Select @@IDENTITY"
oRS = oConn.Execute(sSQL)
iNewId = oRS(0).Value
oRS.Close
0
 
LVL 6

Accepted Solution

by:
Programming_Gal earned 125 total points
ID: 9685369
http://www.codeproject.com/aspnet/fileupload.asp

This one is in C#, should be able to convert easily. ;p

private int WriteToDB(string strName, string strType, ref byte[] Buffer)
{
      int nFileID = 0;

      // Create connection
      OleDbConnection dbConn = new OleDbConnection(GetConnectionString());

      // Create Adapter
      OleDbDataAdapter dbAdapt = new OleDbDataAdapter("SELECT * FROM tblFile", dbConn);
                  
      // We need this to get an ID back from the database
      dbAdapt.MissingSchemaAction = MissingSchemaAction.AddWithKey;
                  
      // Create and initialize CommandBuilder
      OleDbCommandBuilder dbCB = new OleDbCommandBuilder(dbAdapt);

      // Open Connection
      dbConn.Open();
                              
      // New DataSet
      DataSet dbSet = new DataSet();
                              
      // Populate DataSet with data
      dbAdapt.Fill(dbSet, "tblFile");

      // Get reference to our table
      DataTable dbTable = dbSet.Tables["tblFile"];

      // Create new row
      DataRow dbRow = dbTable.NewRow();

      // Store data in the row
      dbRow["FileName"] = strName;
      dbRow["FileSize"] = Buffer.Length;
      dbRow["ContentType"] = strType;
      dbRow["FileData"] = Buffer;

      // Add row back to table
      dbTable.Rows.Add(dbRow);

      // Update data source
      dbAdapt.Update(dbSet, "tblFile");

      // Get newFileID
      if( !dbRow.IsNull("FileID") )
            nFileID = (int)dbRow["FileID"];    //<<<<<<<<This is the auto ID
                  
      // Close connection
      dbConn.Close();

      // Return FileID
      return nFileID;
}
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now