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

Getting a return value from a sql command

K B
Hi, I'm using the following and the insert portion works fine but I'm
not getting the return value (TicketID) which is the identify column of
the table. I can't figure out what I'm missing; all examples I see use
stored procs but I need to use a string command for this.

Dim sSQL As String = "INSERT INTO eTicket(Priority, Category)
VALUES(@iPriority, @iCategory)"
Dim cmd As DbCommand = db.GetSqlStringCommand(sSQL)
db.AddInParameter(cmd, "@iPriority", SqlDbType.Int, iPriority)
db.AddInParameter(cmd, "@iCategory", SqlDbType.Int, iCategory)
db.AddOutParameter(cmd, "@TicketID", SqlDbType.Int, 4)
db.ExecuteNonQuery(cmd)
*******INSERT COMPLETES SUCCESSFULLY******

Dim iTicketID As Integer = db.GetParameterValue(cmd, "TicketID")

Am I doing something wrong in the sql command string?

Thanks,
KB

*** Sent via Developersdex http://www.developersdex.com ***
Jun 19 '06 #1
2 3224
V
Hi KB,

As far as I know, the Insert Statement does not return the Identity
Column as a value, and that is why your code will not work.

- Vaibhav

K B wrote:
Hi, I'm using the following and the insert portion works fine but I'm
not getting the return value (TicketID) which is the identify column of
the table. I can't figure out what I'm missing; all examples I see use
stored procs but I need to use a string command for this.

Dim sSQL As String = "INSERT INTO eTicket(Priority, Category)
VALUES(@iPriority, @iCategory)"
Dim cmd As DbCommand = db.GetSqlStringCommand(sSQL)
db.AddInParameter(cmd, "@iPriority", SqlDbType.Int, iPriority)
db.AddInParameter(cmd, "@iCategory", SqlDbType.Int, iCategory)
db.AddOutParameter(cmd, "@TicketID", SqlDbType.Int, 4)
db.ExecuteNonQuery(cmd)
*******INSERT COMPLETES SUCCESSFULLY******

Dim iTicketID As Integer = db.GetParameterValue(cmd, "TicketID")

Am I doing something wrong in the sql command string?

Thanks,
KB

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


Jun 19 '06 #2
try:

Dim sSQL As String = "INSERT INTO eTicket(Priority, Category)
VALUES(@iPriority, @iCategory)
select scope_identity() as TicketID"

Dim cmd As DbCommand = db.GetSqlStringCommand(sSQL)
db.AddInParameter(cmd, "@iPriority", SqlDbType.Int, iPriority)
db.AddInParameter(cmd, "@iCategory", SqlDbType.Int, iCategory)

Dim iTicketID As Integer = db.ExecuteScaler(cmd) 'needs a cast but i do not
remember vb syntax

-- bruce (sqlwork.com)
"K B" <ka**********@comcast.net> wrote in message
news:um**************@TK2MSFTNGP04.phx.gbl...
Hi, I'm using the following and the insert portion works fine but I'm
not getting the return value (TicketID) which is the identify column of
the table. I can't figure out what I'm missing; all examples I see use
stored procs but I need to use a string command for this.

Dim sSQL As String = "INSERT INTO eTicket(Priority, Category)
VALUES(@iPriority, @iCategory)"
Dim cmd As DbCommand = db.GetSqlStringCommand(sSQL)
db.AddInParameter(cmd, "@iPriority", SqlDbType.Int, iPriority)
db.AddInParameter(cmd, "@iCategory", SqlDbType.Int, iCategory)
db.AddOutParameter(cmd, "@TicketID", SqlDbType.Int, 4)
db.ExecuteNonQuery(cmd)
*******INSERT COMPLETES SUCCESSFULLY******

Dim iTicketID As Integer = db.GetParameterValue(cmd, "TicketID")

Am I doing something wrong in the sql command string?

Thanks,
KB

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

Jun 19 '06 #3

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

Similar topics

3
by: Vipul Pathak | last post by:
Hello Friends ! I have the Following Code, that Executes a Stored Procedure and Attempt to read a Returned Integer Value from the StoredProc. But It gives Error ... ADODB.Command (0x800A0BB9)...
1
by: Greg Smith | last post by:
I am trying to write a SQL query that will return a record count to my C# application. I wrote the following query: ALTER PROCEDURE up_justification_duplicate AS SELECT COUNT(*) FROM...
2
by: Sam | last post by:
In our C++ program, we are using the system call to execute another C++ program synchronously. The program executed by system runs without error and returns back a 0. Under conditions we cannot...
3
by: tshad | last post by:
I am trying to set up a class to handle my database accesses. I can't seem to figure out how to get the return value from my dataReader from these routines (most of which I got elsewhere). They...
3
by: Geraldine Hobley | last post by:
Hello, I have a stored procedure that returns a value but does not select any rows as shown below How can I access the value of this return value from the database using datasets or dataadapters. ...
0
by: ruju00 | last post by:
I am getting an error in Login() method of the following class FtpConnection public class FtpConnection { public class FtpException : Exception { public FtpException(string message) :...
36
by: Chuck Faranda | last post by:
I'm trying to debug my first C program (firmware for PIC MCU). The problem is getting serial data back from my device. My get commands have to be sent twice for the PIC to respond properly with...
5
by: noLoveLusT | last post by:
hi everyone i am very very new to the sql server (2 days actually and ) so far i learned creating SPs etc but couldnt workout how to get return value from my prodecure my sp as follows...
1
by: Mike Lester | last post by:
I have a need for a stored procedure to return a recordset AND an output parameter that contains the count of records in the recordset. I can get either but not both. (ie. if there is a select...
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: 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: 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?
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
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,...

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.