473,748 Members | 9,641 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Get Auto id when inserting record into ms Access

Hey all,

I am accessing an ms access database using .NET and C#. Like so,

/* Create the database connection. */
connection = new OleDbConnection ("Provider=Micr osoft.Jet.OLEDB .4.0;Data
Source=" + Name);
connection.Open ();

And all is good.

My "ticket" table has the setup: auto number (PK), name and event_id.

I am successfully inserting records into the "ticket" table, BUT HOW can I
get the auto number of my PK that MS Access assigns???

I really need this PK!!!

for (int i = 0; i < j; ++i)
{
OleDbCommand c = new OleDbCommand("i nsert into ticket (name, event)
values('" + Customer + "', '" + ID + "')", connection);
c.ExecuteNonQue ry();
}

Thankyou all!
Nov 17 '05 #1
3 16519
What I am after is something like:
http://support.microsoft.com/default...b;en-us;816112

But simpler!

Do I really need a OleDbDataAdapte r and event handler?

I just want something simple...

Ideas, comments?

"James Alba" <a@a.com> wrote in message
news:#a******** ******@TK2MSFTN GP12.phx.gbl...
Hey all,

I am accessing an ms access database using .NET and C#. Like so,

/* Create the database connection. */
connection = new OleDbConnection ("Provider=Micr osoft.Jet.OLEDB .4.0;Data
Source=" + Name);
connection.Open ();

And all is good.

My "ticket" table has the setup: auto number (PK), name and event_id.

I am successfully inserting records into the "ticket" table, BUT HOW can I
get the auto number of my PK that MS Access assigns???

I really need this PK!!!

for (int i = 0; i < j; ++i)
{
OleDbCommand c = new OleDbCommand("i nsert into ticket (name, event)
values('" + Customer + "', '" + ID + "')", connection);
c.ExecuteNonQue ry();
}

Thankyou all!

Nov 17 '05 #2
dd
First thing crosses my mind is max(ID).
The last record inserted should get the max number in the auto number field.

-Duy

James Alba wrote:
What I am after is something like:
http://support.microsoft.com/default...b;en-us;816112

But simpler!

Do I really need a OleDbDataAdapte r and event handler?

I just want something simple...

Ideas, comments?

"James Alba" <a@a.com> wrote in message
news:#a******** ******@TK2MSFTN GP12.phx.gbl...
Hey all,

I am accessing an ms access database using .NET and C#. Like so,

/* Create the database connection. */
connection = new OleDbConnection ("Provider=Micr osoft.Jet.OLEDB .4.0;Data
Source=" + Name);
connection.Op en();

And all is good.

My "ticket" table has the setup: auto number (PK), name and event_id.

I am successfully inserting records into the "ticket" table, BUT HOW can I
get the auto number of my PK that MS Access assigns???

I really need this PK!!!

for (int i = 0; i < j; ++i)
{
OleDbComman d c = new OleDbCommand("i nsert into ticket (name, event)
values('" + Customer + "', '" + ID + "')", connection);
c.ExecuteNonQ uery();
}

Thankyou all!


Nov 17 '05 #3
I just did this morning. I was't terribly concerned with elegance because I
know once proof of concept gets signed off, we will move it to SqlServer
where stored procedures make this easy.

Anyway, what I do is use Access Queries(Access' version of Sql sprocs)
I have one for the insert that take the parameters of the insert, say
_userName and _email
then another to fetch the ID where UserName = _username and Email = _email.

Since I have already set up the command object with the parameters, it's a
matter of calling ExecuteNonQuery for the update, then ExecuteReader for the
ID changing the Command object's CommandText to reflect the correct query.

It works. Not the prettiest thing, but works.
"James Alba" <a@a.com> wrote in message
news:uI******** ******@TK2MSFTN GP15.phx.gbl...
What I am after is something like:
http://support.microsoft.com/default...b;en-us;816112

But simpler!

Do I really need a OleDbDataAdapte r and event handler?

I just want something simple...

Ideas, comments?

"James Alba" <a@a.com> wrote in message
news:#a******** ******@TK2MSFTN GP12.phx.gbl...
Hey all,

I am accessing an ms access database using .NET and C#. Like so,

/* Create the database connection. */
connection = new OleDbConnection ("Provider=Micr osoft.Jet.OLEDB .4.0;Data
Source=" + Name);
connection.Open ();

And all is good.

My "ticket" table has the setup: auto number (PK), name and event_id.

I am successfully inserting records into the "ticket" table, BUT HOW can I get the auto number of my PK that MS Access assigns???

I really need this PK!!!

for (int i = 0; i < j; ++i)
{
OleDbCommand c = new OleDbCommand("i nsert into ticket (name, event)
values('" + Customer + "', '" + ID + "')", connection);
c.ExecuteNonQue ry();
}

Thankyou all!


Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
6919
by: Joachim Klassen | last post by:
Hi all, first apologies if this question looks the same as another one I recently posted - its a different thing but for the same szenario:-). We are having performance problems when inserting/deleting rows from a large table. My scenario: Table (lets call it FACT1) with 1000 million rows distributed on 12
1
1681
by: Jeff | last post by:
Hello all, I have an SQL Insert statement I'm trying to run via ExecuteNonQuery. It works fine so long as there are no nulls values. Here is the statement...(its a shortened version of what i actually have) MySQL = "Insert into Activity (ActivityName) Values ('" + GetString(2) + "')"; Here is the JIT Error..."Data is Null. This method or property cannot be called on Null Values"
0
1285
by: Nick | last post by:
How do I go about setting a default value for a row when inserting a new record with the DetailsView ? Effectively I need to access the underlying data source and set a column to be a default value of my choosing. Nick
4
1329
by: thebison | last post by:
Hi all, I hope someone can help with this relatively simple problem. I am building a timesheet application using ASP.NET C# with Visual Studio 2003.As it is only a protoype application, my database has been made in MSDE. I have a 'Create Resource' page where new employees are added to the database. On my data-entry form I capture all the standard details for the Resource table. I have two drop down boxes which are data bound and
1
3726
by: ing42 | last post by:
I have a problem with inserting records into table when an indexed view is based on it. Table has text field (without it there is no problem, but I need it). Here is a sample code: USE test GO CREATE TABLE dbo.aTable ( INT NOT NULL
5
1782
by: rdemyan via AccessMonster.com | last post by:
I have a table with about 80 fields. I'm using an import process to populate the table. It works fine, except for the following: Users generally don't specify values for a lot of numerical fields on the spreadsheet. I set the Access table up so that all the numerical values would have a default value of zero. I thought that when the record was appended to the table that the numerical fields that do not have values would be...
4
5593
by: Manikandan | last post by:
Hi, I'm inserting a datetime values into sql server 2000 from c# SQL server table details Table name:date_test columnname datatype No int date_t DateTime C# coding
2
1809
by: Question123 | last post by:
Hi i have one database table Table1.which contains almost 20000000 recordes. record to this table are inserted through storedprocedure. storedprocedure takes parameter as "value", Beginningdate, Endate . which will insert one record for each day between Beginning date and EndDate. Before inserting record i check is record exsist for date,if exsist i will update value otherwise insert new record.
1
1959
by: DanielLauJJ | last post by:
When inserting a record into a table, I want SQL Server to generate a number automatically for the Primary Key. (e.g. OrderID is 1, 2, 3 and so on) How to do it? (This behavior is similar to the "Auto-number" in Microsoft Access) http://raymondlamsk.blogspot.com/2008/03/faq-ms-sql-server-how-to-generate_24.html
0
8989
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8828
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9319
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9243
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4599
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4869
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3309
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2213
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.