?
Solved

c# msaccess return newly inserted rows id

Posted on 2014-03-14
4
Medium Priority
?
252 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
  • 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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
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: …
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

840 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