473,473 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Getting Identity from SP

I'm trying to get Identity back from a stored procedure in SQL Server. When
I execute the SP in Management Studio @NewID is returned in the first
row/first columm. but when I run the code below, executescaler returns
null. I have pasted both the code and the SP below.
anyone know what is going wrong here?

public static string InsertOrderSP(ref int OrderID, string FranchiseID,
int PrimaryVendorID, string Status, string Customer)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString =
(string)HttpContext.Current.Session["ConnectionString"];
int rowsAffected;
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "NewInsertCommand";
cmd.Parameters.Add(new SqlParameter("@FranchiseID", FranchiseID));
cmd.Parameters.Add(new SqlParameter("@PrimaryVendorID",
PrimaryVendorID));
cmd.Parameters.Add(new SqlParameter("@Status", Status));
cmd.Parameters.Add(new SqlParameter("@DateEntered",
DateTime.Today));
cmd.Parameters.Add(new SqlParameter("@Customer", Customer));
cmd.Parameters.Add(new SqlParameter("@NewID", OrderID));
try
{
conn.Open();
//rowsAffected = cmd.ExecuteNonQuery();
object myobject = new object();
myobject = cmd.ExecuteScalar(); <-------------this is
returning null
---------------------------------------------------------
Stored Procedure:
ALTER PROCEDURE [dbo].[NewInsertCommand]
(
@FranchiseID nvarchar(16),
@PrimaryVendorID int,
@Status nvarchar(15),
@DateEntered datetime,
@Customer nvarchar(50),
@NewID int OUTPUT
)
AS
begin tran
SET NOCOUNT OFF;
INSERT INTO [Order] ([FranchiseID], [PrimaryVendorID], [Status],
[DateEntered], [Customer])
VALUES (@FranchiseID, @PrimaryVendorID, @Status, @DateEntered, @Customer);
set @NewID = @@identity
commit tran

--
Regards,
Gary Blakely
Jan 7 '07 #1
1 847
"GaryDean" <Ga******@newsgroups.nospamwrote in message
news:OL****************@TK2MSFTNGP06.phx.gbl...

set @NewID = @@identity
commit tran
SELECT @NewID
Jan 7 '07 #2

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

Similar topics

2
by: Benny | last post by:
Dear All, Suppose in the program a record is added to a table whose primary key is a identity field. If I really want to get the lastest value for that field after the insertion, is it the best...
2
by: Ramesh | last post by:
hi, I have created COM when i try to use that instance in C#.net Wep application i am getting following error message. DBUsingCom is my Component name and BusinessObject as a Class name.can...
2
by: Brian Henry | last post by:
im wokring on an intranet site, and i need the username of the current user logged in so it displays their information, how do i find out the user name thats logged into the system so i can use it...
4
by: Nick | last post by:
I'm developing an intranet app, and want to get the current user name for logging purposes. I've turned off anonymous access, and turned on windows authentication in the IIS config. The apps...
7
by: Oenone | last post by:
Sorry for the cross-post but I've really no idea where this is best suited. I've an ADO.NET application which connects to a SQL Server database. I have spent the entire morning trying to get it...
2
by: Wayne Wengert | last post by:
I have a VB.NET app in which I am using the following code to add a new row. ds.Tables("UnitsTable").Rows.Add(dr) ' Add the new row da.InsertCommand = cmdBuilder.GetInsertCommand da.Update(ds,...
13
by: Steve Enzer | last post by:
How can I get the login name of the user currently logged into Windows as a string? Thanks, Steve Enzer
41
by: pb648174 | last post by:
In a multi-user environment, I would like to get a list of Ids generated, similar to: declare @LastId int select @LastId = Max(Id) From TableMania INSERT INTO TableMania (ColumnA, ColumnB)...
1
by: Brad Eck | last post by:
In Access, newID returns a unique for the table. In SQL Server, newid() returns a GUID - unique in the world. I do not need or desire that complexity. Is there a way to get a simple unique int on...
3
by: Steve | last post by:
Hi; We just migrated to SqlServer 2005. When I import tables from from SqlServer database into another the identity fields get switched off from being identities. How can I prevent that...
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
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,...
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
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...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
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...
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: 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 ...

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.