473,385 Members | 1,400 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.

Is there anyway to get the row number?

I have this code:

DataRow row = docAttachTable.NewRow();
docAttachTable.Rows.Add(row);

What I want to know is the primary key number (it's auto-generated).
Is there anyway to do that?

thanks in advance,
chance.

Mar 30 '07 #1
3 1994
Chance,

Until you commit the row to the database, and reselect the id back from
the database, you won't be able to know.

That's one of the problems of using auto-generated ids, and it's because
of this that I stay away from them.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"chance" <ch****@crwmail.comwrote in message
news:11**********************@y80g2000hsf.googlegr oups.com...
>I have this code:

DataRow row = docAttachTable.NewRow();
docAttachTable.Rows.Add(row);

What I want to know is the primary key number (it's auto-generated).
Is there anyway to do that?

thanks in advance,
chance.

Mar 30 '07 #2
OD
Since the data are not sent to the database, the autoinc key is not
generated. So first you must post the data to the DB. Next you have to
query the DB to get the line and know the ID.. But of course, as you
don't know the ID, you can't query the DB to get it :-)
So, there is mainly two ways to get out of this problem :

1) you add a string field in your table in which you put a new GUID
(using the framework it is simple to generate one), then you can query
the DB based on this GUID and you can know the ID... Of course it seems
a bit stupid to waste a string field for that purpose and also it is
not very smart to add a GUID if there is already an autoinc primary
key... But in some cases, this solution is not bad.
Another way to use this solution and avoid the GUID field is when your
table as a second possible primary key (a Candidate key). In this case
this is a very good way to select the record and read the autoinc and
this solution becomes the best and simplest one.

2) the most used solution is to avoid autoinc field... steps :

a) create a generator in your DB
b) create a stored procedure to return next value
c) optional : add a trigger to the table to insert a new value (from
the stored proc) when the field is null
d) when you wan programmaticaly set the ID yourself, you call the
stored procedure in your code and set the ID field as any other fields.

--
OD___
www.e-naxos.com
Mar 30 '07 #3
Create a stored procedure on SQL Sever for your insert;

ALTER PROCEDURE INSERTFORMAT

@formatID INT OUTPUT,

@formatName VARCHAR(25),

@formatProgram VARCHAR(25)

AS

INSERT INTO FORMATS

(FORMATNAME,

FORMATPROGRAM,

FORMATUPDATEDBY)

VALUES (@formatName,

@formatProgram,

SUSER_SNAME())

SET @formatID = @@IDENTITY

RETURN

Call the store procedure from within your code using parameters;

//function to insert new record and return it's row identifier (autoinc)

public int InsertFormat(FormatsDetails fmt)

{

SqlConnection con = new SqlConnection(connectionString);

SqlCommand cmd = new SqlCommand("InsertFormat", con);

cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.Add(new SqlParameter("@formatName", SqlDbType.VarChar,
25));

cmd.Parameters["@formatName"].Value = fmt.FormatName;

cmd.Parameters.Add(new SqlParameter("@formatProgram",
SqlDbType.VarChar, 25));

cmd.Parameters["@formatProgram"].Value = fmt.FormatProgram;

cmd.Parameters.Add(new SqlParameter("@formatID", SqlDbType.Int, 4));

cmd.Parameters["@formatID"].Direction = ParameterDirection.Output;

try

{

con.Open();

cmd.ExecuteNonQuery();

return (int)cmd.Parameters["@formatID"].Value;

}

catch (SqlException err)

{

// add error handling code here

throw new ApplicationException("Data Insert Error");

}

finally

{

con.Close();

}

}


Mar 30 '07 #4

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

Similar topics

26
by: Michael Strorm | last post by:
Hi! I posted a message a while back asking for project suggestions, and decided to go with the idea of creating an adventure game (although it was never intended to be a 'proper' game, rather an...
1
by: Richard Holliingsworth | last post by:
Hello: The group has new 'standards' for online forms and I need to change the 'style' of my existing forms (A2002). Can I do this? I can't find a way using the GUI or the properties. ...
40
by: Abby | last post by:
My .dat file will contain information like below. /////////// First 0x04 0x05 0x06 Second 0x07
3
by: charliewest | last post by:
Programming ASP.NET w/ C#, i have a DropDownList control that when selected, fires the SelectedItemChanged event. The "values" for this control are numbers (although stored as strings). For...
0
by: John Dolinka | last post by:
Is there anyway to get an incremental build number from vb.net everytime you do an incremental build, like app.revision use to? The following line of code generates a wierd number, but not an...
1
by: Terry Olsen | last post by:
I need a way to programmatically tell if a row is visible or has scrolled out of view. Any Ideas? Thanks!
5
by: tommytx | last post by:
Is there any capability of decisions in htaccess. I know you can use in special situations, but I need something like: If a then b etc like in php. In other words I want to redirect the...
6
by: massic80 | last post by:
Hi, it's me again! I have a strange problem with a form I gotta (dynamically) insert in a site, and copied/pasted the code to a blank page, to make it easier to try it out. I was using the $...
14
by: fuchsia555 | last post by:
Is there anyway to hide php error messages? Because i have in my php code error but it redirecting after 1 sec and shows my hosting path and php error number, but this error doesn't effect anything.
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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...

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.