473,387 Members | 1,481 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,387 software developers and data experts.

Trying to get Primary Key using RowUpdated but get System.DBNull instead

Hello.

I'm trying to get the primary key for a newly inserted row using
ADO.NET and SqlServer. The convention seems to be to do this with the
RowUpdated event, i.e.

_sqlConnectionToTickerTextDb.Open();
SqlCommand cmdGetPK = new SqlCommand("SELECT @@IDENTITY",
_sqlConnectionToTickerTextDb);
object pkObject = cmdGetPK.ExecuteScalar();
int pkNunber = Int32.Parse((pkObject).ToString());
e.Row[_tickerIDColumnName] = pkNunber;
e.Row.AcceptChanges();
_sqlConnectionToTickerTextDb.Close();
However, I am getting the value System.DBNull instead of the primary
key.
I''ve tried replacing @@IDENTITY with SCOPE_IDENTITY() but the same
happens.
Is there a setting that I need to alter in the database or am I
approaching this in the wrong way?

I've also noticed that most people dont use .Open and .Close() in
coding the above, yet my app complains if I don't.

Is there an easier more practical way of getting the new primary key
e.g. by looking at the attributes of the SqlRowUpdatedEventArgs
parameter?

Greg.

Mar 6 '06 #1
4 1994
It looks from your code sample like you are trying to get @@IDENTITY
independently of the query or stored proc context it was produced in.
@@IDENTITY goes out of scope when the query or stored proce that inserts a
new record is done. So if you want to get this, you can either have the
@@IDENTITY Value be a return value from the sproc, or you can get it as a
value from ExecuteScalar with Select @@IDENTITY

Hope that helps.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Greg" wrote:
Hello.

I'm trying to get the primary key for a newly inserted row using
ADO.NET and SqlServer. The convention seems to be to do this with the
RowUpdated event, i.e.

_sqlConnectionToTickerTextDb.Open();
SqlCommand cmdGetPK = new SqlCommand("SELECT @@IDENTITY",
_sqlConnectionToTickerTextDb);
object pkObject = cmdGetPK.ExecuteScalar();
int pkNunber = Int32.Parse((pkObject).ToString());
e.Row[_tickerIDColumnName] = pkNunber;
e.Row.AcceptChanges();
_sqlConnectionToTickerTextDb.Close();
However, I am getting the value System.DBNull instead of the primary
key.
I''ve tried replacing @@IDENTITY with SCOPE_IDENTITY() but the same
happens.
Is there a setting that I need to alter in the database or am I
approaching this in the wrong way?

I've also noticed that most people dont use .Open and .Close() in
coding the above, yet my app complains if I don't.

Is there an easier more practical way of getting the new primary key
e.g. by looking at the attributes of the SqlRowUpdatedEventArgs
parameter?

Greg.

Mar 6 '06 #2
Thanks a lot Peter.

Slightly confused though - your second suggestion of "you can get it
as a
value from ExecuteScalar with Select @@IDENTITY" - I thought that this
is what I had done. What do I need to change?

Thanks for your help.

Greg.

Mar 6 '06 #3
Greg,
Let's make it even clearer, then:
This SQL that you supplied :

sqlConnectionToTickerTextDb.Open();
SqlCommand cmdGetPK = new SqlCommand("SELECT @@IDENTITY",
_sqlConnectionToTickerTextDb);
object pkObject = cmdGetPK.ExecuteScalar();
int pkNunber = Int32.Parse((pkObject).ToString());

--Appears not to insert any new row at all, therefore there is no @@IDENTITY
in the context to return. If it was a stored proc:

CREATE PROC InsertTickerText
@TickerText varchar(500)
AS
INSERT INTO myTickerText
(TickerText) VALUES (@TickerText)
SELECT @@IDENTITY
-- then you would have an @@ IDENTITY To return.

Hope that makes sense.
Peter
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Greg" wrote:
Thanks a lot Peter.

Slightly confused though - your second suggestion of "you can get it
as a
value from ExecuteScalar with Select @@IDENTITY" - I thought that this
is what I had done. What do I need to change?

Thanks for your help.

Greg.

Mar 6 '06 #4
Great, thanks a lot!

Mar 6 '06 #5

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

Similar topics

4
by: Brian Brane | last post by:
I have properties that wrap DataRow columns as in: public int aNumber { get{ return m_DataRow; } set{ m_DataRow = value; } } If the column happens to contain DBNull, I get a cast exception...
5
by: jaYPee | last post by:
i have successfully added a combobox to my datagrid by setting their datasource from one of my table. here's my code... Dim grdColStyle6 As New DataGridComboBoxColumn() With grdColStyle6...
10
by: Bob | last post by:
I'm sure there's a good reason, I just haven't been able to think of it - why didn't MS allow "DBNull" values to simply be a null (Nothing)? Bob
6
by: cj | last post by:
in the command window I get: ? result {System.DBNull} : {System.DBNull} I need to check for this in code. I then tested in the command window if result = system.DBNull 'DBNull' is a type...
0
by: Henry | last post by:
I am trying to create a TreeView control that works with an ADO Dataset DataTable or the new BindingSource stuff in .NET 2.0 to build a Treeview that is populated. This is what I came up with...
5
by: Learner | last post by:
Hello, Here is the code snippet I got strucked at. I am unable to convert the below line of code to its equavalent vb.net code. could some one please help me with this? static public...
11
by: Greg P | last post by:
I'm using VB 2005, and have drug and dropped datagridviews onto my form. The default code that is generated for inserts tries to insert a value for the auto generated primary key. Below is the...
0
by: lgalumbres | last post by:
Hello, I have a DataGridView control with a DataGridViewComboBoxColumn that allows users to choose selections from a list. The DataGridView is bound to a DataTable object and the...
1
by: rsteph | last post by:
I've got some product information pages, with images and text, all setup within a table. I'm trying to add a small image in the upper right hand corner of the content div (where all the important...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.