Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

c# msaccess return newly inserted rows id

Posted on 2014-03-14
4
Medium Priority
?
250 Views
Last Modified: 2014-03-25
Hi

I have a networked database, with a number of users


I have a situation where I need to insert a record into a table, and return the autonumber ID generated by access, so that the user can add records to another table using the id from the first table as the foreign key.

I have used the following sql statement

"Select @@Identity from " + table;

which works most times, however on the odd occasion it appears to be returning either nothing or the id from the previous record.

Is there any way to ensure that the query returns the correct value each time?

here is my code to insert data

  public int InsertData(string table, string[] fields, string[] values)
        {
            int id = 0;
            OleDbConnection con = new OleDbConnection(connectionstring);
            string query2 = "Select @@Identity from " + table;
            con.Open();
            string myFields = "";
            string myValues = "";

            foreach (string f in fields)
            {
                myFields += f + ", ";
                myValues += "?,";

            }

            if (myFields.Length > 0)
            {
                myFields = myFields.Substring(0, myFields.Length - 2);
                myValues = myValues.Substring(0, myValues.Length - 1);
            }

            OleDbCommand cmd = new System.Data.OleDb.OleDbCommand("INSERT INTO " + table + " (" + myFields + ") Values (" + myValues + ") ", con);

            foreach (string v in values)
            {
                cmd.Parameters.AddWithValue("?",v);
            }

            cmd.CommandType = CommandType.Text;
            cmd.ExecuteNonQuery();
            cmd.CommandText = query2;
            id = Convert.ToInt32(cmd.ExecuteScalar());
            
            con.Close();


            return id; 
        }

Open in new window

0
Comment
Question by:cycledude
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 64

Assisted Solution

by:Fernando Soto
Fernando Soto earned 1500 total points
ID: 39930296
Hi cycledude;

According to this Microsoft article you will need two OleDbCommand connections. The article shows how to do this.

HOW TO: Retrieve the Identity Value While Inserting Records into Access Database By Using Visual C# .NET
0
 

Author Comment

by:cycledude
ID: 39930406
thanks Fernando

Any ideas how I can get that to work with my code?
0
 

Accepted Solution

by:
cycledude earned 0 total points
ID: 39941813
in the end I had to use a timestamp when entering the row, then use a while loop to repeat the process of acquiring the id every 1 second until the correct id was found.
0
 

Author Closing Comment

by:cycledude
ID: 39952707
thanks for the assist
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

618 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