473,729 Members | 2,405 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with stored procedure and retrieving inserted identity value

Hello!

I use a procedure to insert a new row into a table with an identity
column. The procedure has an output parameter which gives me the
inserted identity value. This worked well for a long time. Now the
identity value is over 700.000 and I get errors whiles retrieving the
inserted identitiy value. If I delete rows and reset the identity
everything works well again. So I think it is a data type problem.

My Procedure:

create procedure InsertProduct
@NEWID int output
as
begin
set nocount on
insert into PRODUCT(D_CREAT ED)values(getda te()+'')
set nocount off
select @NEWID = @@IDENTITY
end

My C# code:
SqlCommand comm = new SqlCommand("Ins ertProduct", sqlCon);
comm.CommandTyp e = CommandType.Sto redProcedure;
comm.Parameters .Add(new SqlParameter("@ NEWID",
System.Data.Sql DbType.Int)).Di rection =
System.Data.Par ameterDirection .Output;

try
{
SqlDataReader sqlRead = comm.ExecuteRea der();
object o = comm.Parameters["@NEWID"].Value;
//...
}
catch ( Exception ex ){ throw ex;}

The object o is alwaya System.DbNull. I also tried to use bigint.

Any hints are welcome

Ciao
Susanne
Jul 11 '06 #1
3 2849
Susanne Klemm (sk****@gmx.de) writes:
I use a procedure to insert a new row into a table with an identity
column. The procedure has an output parameter which gives me the
inserted identity value. This worked well for a long time. Now the
identity value is over 700.000 and I get errors whiles retrieving the
inserted identitiy value. If I delete rows and reset the identity
everything works well again. So I think it is a data type problem.
There is nothing magic around 700000. Overall, everything you have is 32-bit
integer, so there should be no reason for a clash.
create procedure InsertProduct
@NEWID int output
as
begin
set nocount on
insert into PRODUCT(D_CREAT ED)values(getda te()+'')
set nocount off
select @NEWID = @@IDENTITY
end
In this case, it's better to use scope_identity( ), which returns the most
recently generated identity value in the current scope. If PRODUCT has a
trigger which inserts data into a table with another IDENTITY column,
@@identity will return the value for that latter table.
--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Jul 11 '06 #2
Erland Sommarskog wrote:
Susanne Klemm (sk****@gmx.de) writes:
create procedure InsertProduct
@NEWID int output
as
begin
set nocount on
insert into PRODUCT(D_CREAT ED)values(getda te()+'')
set nocount off
select @NEWID = @@IDENTITY
end

In this case, it's better to use scope_identity( ), which returns the most
recently generated identity value in the current scope. If PRODUCT has a
trigger which inserts data into a table with another IDENTITY column,
@@identity will return the value for that latter table.
I tried scope_identity( ) but got no changes. But indeed there was a
trigger on the product table. After removing it both variants worked.

Thank you!
Susanne

Jul 11 '06 #3
rmk
select @NEWID = @@IDENTITY

should be changed to

SET @NEWID = SCOPE_IDENTITY( )
"Susanne Klemm" <sk****@gmx.dew rote in message
news:44******** *************@r eader.news.celo x.de...
Hello!

I use a procedure to insert a new row into a table with an identity
column. The procedure has an output parameter which gives me the
inserted identity value. This worked well for a long time. Now the
identity value is over 700.000 and I get errors whiles retrieving the
inserted identitiy value. If I delete rows and reset the identity
everything works well again. So I think it is a data type problem.

My Procedure:

create procedure InsertProduct
@NEWID int output
as
begin
set nocount on
insert into PRODUCT(D_CREAT ED)values(getda te()+'')
set nocount off
select @NEWID = @@IDENTITY
end

My C# code:
SqlCommand comm = new SqlCommand("Ins ertProduct", sqlCon);
comm.CommandTyp e = CommandType.Sto redProcedure;
comm.Parameters .Add(new SqlParameter("@ NEWID",
System.Data.Sql DbType.Int)).Di rection =
System.Data.Par ameterDirection .Output;

try
{
SqlDataReader sqlRead = comm.ExecuteRea der();
object o = comm.Parameters["@NEWID"].Value;
//...
}
catch ( Exception ex ){ throw ex;}

The object o is alwaya System.DbNull. I also tried to use bigint.

Any hints are welcome

Ciao
Susanne

Jul 11 '06 #4

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

Similar topics

2
4198
by: Tavish Muldoon | last post by:
What a pain trying to insert data into a table from a stored proc. My webform asks for 16 pieces of data - which then gets written to the database. I found this easier than the crap below (after the ***********). This was eaiser: get a list of all the parameters you want to pass - put them in a string:
3
4369
by: Jason Callas | last post by:
I have a stored procedure that runs as a step in a scheduled job. For some reason the job does not seem to finish when ran from the job but does fine when run from a window in SQL Query. I know the job is not working because the number of rows that are inserted into the table (see code) is considerably less than the manual runnning of it. I have included the code for the stored procedure, the output from the job, and the output from...
3
6111
by: DarthMacgyver | last post by:
Hello, I recently wrote a survey application. Each question is very similar. The first questions gives me a problem when there are multiple people taking the survey (The Database connection Timed out) I am using the Data Access Application Blocks as ASP.NET (using VB.NET) and SQL 2000. In there first question there can be up to 27 answers. So I figured instead of making 27 different trips to the database I woulc just concatenate my...
4
1793
by: Newbie | last post by:
hello! i'm new to sql server and having some problem getting the primary key or index (Reference column). opening up the design table, the primary key or index column has an identity seed number that may vary in time. the identity increment is 1. in my code, i'm trying to get the next value and showing it in a textbox (txtReference). but i'm getting the wrong value. example: if the last row in the table TaskOrder has a value of 150 in...
6
4024
by: Leon Shaw | last post by:
How do I implement a stored procedure to insert a new member in a database then return the primary key of that member back to the application to be use in another table?
17
2697
by: Rico | last post by:
Hello, I am in the midst of converting an Access back end to SQL Server Express. The front end program (converted to Access 2003) uses DAO throughout. In Access, when I use recordset.AddNew I can retrieve the autonum value for the new record. This doesn't occur with SQL Server, which of course causes an error (or at least in this code it does since there's an unhandled NULL value). Is there any way to retrieve this value when I add a...
6
2621
by: rn5a | last post by:
In a shopping cart app, when a user finalizes his order, records from a temporary table named 'TempCart' get inserted into another table name 'Orders' after which the records from 'TempCart' are deleted. 'TempCart' has the following columns: UserID int (no NULLs) Carpet int (NULLs allowed) Color int (NULLs allowed) Engine int (NULLs allowed) Total decimal(10, 2) (NULLs allowed)
12
2252
by: Light | last post by:
Hi all, I posted this question in the sqlserver.newusers group but I am not getting any response there so I am going to try it on the fine folks here:). I inherited some legacy ASP codes in my office. The original code's backend is using the SQL Server 2000 and I am testing to use it on the Express edition. And I run into the following problem.
8
1068
by: Tom P. | last post by:
I have a webpage that takes user data and sends it to a stored procedure that inserts a row in a table then returns the ID of that row for display. The stored procedure does an insert then it uses IDENT_CURRENT to get the identity of the row that was just inserted and selects the row back out to display. The dataset is not getting populated. The dataset has a DataTable that gets created and the DataTable has the columns named correctly
0
8921
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
8763
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,...
0
9427
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9284
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9148
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
6022
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4528
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...
1
3238
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
3
2165
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.