473,385 Members | 2,274 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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=Microsoft.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("insert into ticket (name, event)
values('" + Customer + "', '" + ID + "')", connection);
c.ExecuteNonQuery();
}

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

But simpler!

Do I really need a OleDbDataAdapter and event handler?

I just want something simple...

Ideas, comments?

"James Alba" <a@a.com> wrote in message
news:#a**************@TK2MSFTNGP12.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=Microsoft.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("insert into ticket (name, event)
values('" + Customer + "', '" + ID + "')", connection);
c.ExecuteNonQuery();
}

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 OleDbDataAdapter and event handler?

I just want something simple...

Ideas, comments?

"James Alba" <a@a.com> wrote in message
news:#a**************@TK2MSFTNGP12.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=Microsoft.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("insert into ticket (name, event)
values('" + Customer + "', '" + ID + "')", connection);
c.ExecuteNonQuery();
}

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**************@TK2MSFTNGP15.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 OleDbDataAdapter and event handler?

I just want something simple...

Ideas, comments?

"James Alba" <a@a.com> wrote in message
news:#a**************@TK2MSFTNGP12.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=Microsoft.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("insert into ticket (name, event)
values('" + Customer + "', '" + ID + "')", connection);
c.ExecuteNonQuery();
}

Thankyou all!


Nov 17 '05 #4

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

Similar topics

3
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...
1
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...
0
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...
4
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...
1
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...
5
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...
4
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 ...
2
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,...
1
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...

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.