473,461 Members | 1,515 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Retrieve new row identity in C#

Hi,
I'm having a bit of difficulty in getting the identity of a new row
using the SCOPE_IDENTITY() function in SQLServer 2000.

I've added the following code: 'set @LastID = SCOPE_IDENTITY()' to the
end of the select statement in the Stored Procedure, and have the
following code in the aspx.cs file:

SqlCommand cmdEntry;
SqlParameter prmEntryID = new
SqlParameter("RETURN_VALUE",SqlDbType.Int);
prmEntryID.Direction = ParameterDirection.ReturnValue;

cmdEntry = new SqlCommand("kb_Add_Entry", conn);
cmdEntry.CommandType = CommandType.StoredProcedure;
cmdEntry.Parameters.Add ("@kb_entry_description", txtDescription.Text);
cmdEntry.Parameters.Add ("@kb_entry_resolution", txtResolution.Text);
cmdEntry.Parameters.Add ("@kb_entry_maincategory",
ddlCategories.SelectedItem.Value);
cmdEntry.Parameters.Add ("@kb_entry_enteredby", "Mark");
cmdEntry.Parameters.Add ("@kb_entry_source", txtSource.Text);
cmdEntry.Parameters.Add ("@kb_entry_keywords", txtKeywords.Text);
cmdEntry.Parameters.Add (prmEntryID);
conn.Open();
cmdEntry.ExecuteNonQuery();
string strEntryID =
cmdEntry.Parameters["RETURN_VALUE"].Value.ToString();
conn.Close();

However, when I run the code, I get the following error: 'Error
converting data type nvarchar to int.'

Can anyone help?

Thanks in advance,
Mark

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #1
3 1690
hi,

if you are running an stored procedure then jusr return it :

in the SP:
insert into XXX values YYY
SELECT @@IDENTITY

in the CS code:
int id = Convert.toInt32( cmd.ExecutyScalar() );

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Mark" <an*******@devdex.com> wrote in message
news:ex**************@tk2msftngp13.phx.gbl...
Hi,
I'm having a bit of difficulty in getting the identity of a new row
using the SCOPE_IDENTITY() function in SQLServer 2000.

I've added the following code: 'set @LastID = SCOPE_IDENTITY()' to the
end of the select statement in the Stored Procedure, and have the
following code in the aspx.cs file:

SqlCommand cmdEntry;
SqlParameter prmEntryID = new
SqlParameter("RETURN_VALUE",SqlDbType.Int);
prmEntryID.Direction = ParameterDirection.ReturnValue;

cmdEntry = new SqlCommand("kb_Add_Entry", conn);
cmdEntry.CommandType = CommandType.StoredProcedure;
cmdEntry.Parameters.Add ("@kb_entry_description", txtDescription.Text);
cmdEntry.Parameters.Add ("@kb_entry_resolution", txtResolution.Text);
cmdEntry.Parameters.Add ("@kb_entry_maincategory",
ddlCategories.SelectedItem.Value);
cmdEntry.Parameters.Add ("@kb_entry_enteredby", "Mark");
cmdEntry.Parameters.Add ("@kb_entry_source", txtSource.Text);
cmdEntry.Parameters.Add ("@kb_entry_keywords", txtKeywords.Text);
cmdEntry.Parameters.Add (prmEntryID);
conn.Open();
cmdEntry.ExecuteNonQuery();
string strEntryID =
cmdEntry.Parameters["RETURN_VALUE"].Value.ToString();
conn.Close();

However, when I run the code, I get the following error: 'Error
converting data type nvarchar to int.'

Can anyone help?

Thanks in advance,
Mark

*** Sent via Developersdex http://www.developersdex.com ***

Nov 17 '05 #2
Hi,
Thanks for replying. Unfortunately I'm still getting the same error
message, this time on the intEntryID =
Convert.ToInt32(cmdEntry.ExecuteScalar()); line...

Mark

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #3
hi

well I assume that the PK was an integer

do tihs:
object o = cmd.ExecutyScalar();
and put a breakpoint after that line, you will see the type of o , and you
could use the correct method/cast
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Mark" <an*******@devdex.com> wrote in message
news:%2******************@TK2MSFTNGP09.phx.gbl...
Hi,
Thanks for replying. Unfortunately I'm still getting the same error
message, this time on the intEntryID =
Convert.ToInt32(cmdEntry.ExecuteScalar()); line...

Mark

*** Sent via Developersdex http://www.developersdex.com ***

Nov 17 '05 #4

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

Similar topics

1
by: Alberto | last post by:
The following example works, but the retrival of the returned identity does not work 'Set Command Object Propertie objCommand.ActiveConnection = GV_objConnectio objCommand.CommandText =...
2
by: WhiteEagl | last post by:
Hello, I would need some help with this identity column problem in SQLServer. I have a database with two tables. The Parent table has an Identity column. Parent (ParentID IDENTITY, Name)...
1
by: mg | last post by:
Can someone give me C# code that will connect me to Active Directory, let me give Active Directory a user's name and retrieve his/her e-mail address. I need this for a WebForm.
1
by: loreille | last post by:
To insert a record in a Ms Access database and be able to retrieve the newly created ID (autonumber) I used the code below (code 1). Now, the problem is that this is not very secure and that, if...
4
by: jay | last post by:
I am using the dataset object to add a row to a sql server database in vb.net code, as follows: dim drow as DataRow dim cmdBld as new SqlCommandBuilder(mySqlDataAdapter) ds.tables(0).NewRow()...
9
by: Jeremy | last post by:
I have a situation where i am trying to run two query's and retrieve one record from each query, then using Union combine them into one recordset. The First Query is strait forward and should just...
13
by: kev | last post by:
Hi all, I have created a database for equipments. I have a form to register the equipment meaning filling in all the particulars (ID, serial, type, location etc). I have two buttons at the end...
2
by: feets | last post by:
OK first time poster, so hello everyone in advance. Right i'm sure this is a simple problem to solve, but I'm just getting the hang of SQL 2000. What I've got is a form where I input the values and...
2
by: =?Utf-8?B?SmVmZlAtPg==?= | last post by:
I want to retrieve an image file (TIF) convert it to a bmp file and enable sizing. I know how to do the converting & sizing part, and store any atributes to file location in the webconfig, what...
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
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...
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,...
0
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...
1
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.