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

SCOPE_IDENTITY()

I was using this fine

sqlIDQuery = "SELECT SectionID FROM SECTIONS WHERE
(SectionID=SCOPE_IDENTITY());"

UNTIL. . . .

I added a parameter, now it fails, it doesent matter if I use the parameter
or not, when I add a param, it falls over, Any Ideas ?

I know this is not the right place to post, but I know you guys are good !!
--
Best Regards

The Inimitable Mr Newbie º¿º
Jan 15 '06 #1
4 4640
Hi,

Could you please post the query with the parameter in it so we can
help figure out the problem?

Ken
------------------------

"Mr Newbie" wrote:
I was using this fine

sqlIDQuery = "SELECT SectionID FROM SECTIONS WHERE
(SectionID=SCOPE_IDENTITY());"

UNTIL. . . .

I added a parameter, now it fails, it doesent matter if I use the parameter
or not, when I add a param, it falls over, Any Ideas ?

I know this is not the right place to post, but I know you guys are good !!
--
Best Regards

The Inimitable Mr Newbie º¿º

Jan 15 '06 #2
The following
code successfully inserts a record in the Sections table, but the
scope_identity() returns DbNull. If I remove the parameter
'pSectionName' and replace it with dummy value, it works fine.
Alternatively, if I use @@IDENTITY with or withour the parameter, that
works fine too.

It seems there is a problem with using scope_identity() when parameters
are involved, but I do need to use parameters. Does anyone know why this
would happen and how to circumvent it ?
Dim sqlConnection As New SqlConnection(getConnectionString())

Dim sqlString As String
Dim result As Integer

Dim pSectionName As New SqlParameter("@pSectionName",
SqlDbType.NVarChar)
pSectionName.Value = sectionRow.SectionName

sqlString = "INSERT INTO SECTIONS " & _
"VALUES (" & _
" '" & sectionRow.ArticleID.ToString & "'," & _
" @pSectionName ," & _
" '" & sectionRow.SectionNumber.ToString & "'," & _
" '" & sectionRow.SectionFollowing.ToString & "'," & _
" '" & sectionRow.Attachments.ToString & "'," & _
" '" & sectionRow._Text & "'," & _
" ''," & _
" '" & sectionRow.pictureName & "'," & _
" '" & sectionRow.pictureType & "'," & _
" '" & sectionRow.pictureFilePath & "'," & _
" '" & sectionRow.SectionType & "');"

Dim sqlIDQuery As String
sqlIDQuery = "SELECT scope_identity();"

Dim sqlCommand As New SqlCommand(sqlString)
sqlCommand.Connection = sqlConnection

'Add Parameters
sqlCommand.Parameters.Add(pSectionName)

Dim SectionID As Integer
Try
sqlConnection.Open()
sqlCommand.ExecuteNonQuery() '***** THIS WORKS FINE AND
INSERTS RECORD.

sqlCommand.CommandText = sqlIDQuery
SectionID = CType(sqlCommand.ExecuteScalar, Integer) ' ****
FAILS HERE WITH AN EXCEPTION.

Catch ex As Exception
SectionID = 0
Finally
sqlConnection.Close()
End Try

Return SectionID
--
Best Regards

The Inimitable Mr Newbie º¿º

--
Best Regards

The Inimitable Mr Newbie º¿º
"Ken Tucker [MVP]" <Ke**********@discussions.microsoft.com> wrote in message
news:65**********************************@microsof t.com... Hi,

Could you please post the query with the parameter in it so we
can
help figure out the problem?

Ken
------------------------

"Mr Newbie" wrote:
I was using this fine

sqlIDQuery = "SELECT SectionID FROM SECTIONS WHERE
(SectionID=SCOPE_IDENTITY());"

UNTIL. . . .

I added a parameter, now it fails, it doesent matter if I use the
parameter
or not, when I add a param, it falls over, Any Ideas ?

I know this is not the right place to post, but I know you guys are good
!!
--
Best Regards

The Inimitable Mr Newbie º¿º

Jan 15 '06 #3
Hi,

When you use sqlCommand.executenonquery you have a parameter defined
and use it.

At this line you change the command text for the sql command.

sqlCommand.CommandText = sqlIDQuery
The new command text does not have a parameter but you still have a
parameter defined. TThis causes your error. Try clearing the parameters
before you run the command the second time.

sqlCommand.Parameters.Clear()
SectionID = CType(sqlCommand.ExecuteScalar, Integer) ' ****

Ken
--------------------
"Mr Newbie" <he**@now.com> wrote in message
news:uG**************@TK2MSFTNGP14.phx.gbl...
The following
code successfully inserts a record in the Sections table, but the
scope_identity() returns DbNull. If I remove the parameter
'pSectionName' and replace it with dummy value, it works fine.
Alternatively, if I use @@IDENTITY with or withour the parameter, that
works fine too.

It seems there is a problem with using scope_identity() when parameters
are involved, but I do need to use parameters. Does anyone know why this
would happen and how to circumvent it ?
Dim sqlConnection As New SqlConnection(getConnectionString())

Dim sqlString As String
Dim result As Integer

Dim pSectionName As New SqlParameter("@pSectionName",
SqlDbType.NVarChar)
pSectionName.Value = sectionRow.SectionName

sqlString = "INSERT INTO SECTIONS " & _
"VALUES (" & _
" '" & sectionRow.ArticleID.ToString & "'," & _
" @pSectionName ," & _
" '" & sectionRow.SectionNumber.ToString & "'," & _
" '" & sectionRow.SectionFollowing.ToString & "'," & _
" '" & sectionRow.Attachments.ToString & "'," & _
" '" & sectionRow._Text & "'," & _
" ''," & _
" '" & sectionRow.pictureName & "'," & _
" '" & sectionRow.pictureType & "'," & _
" '" & sectionRow.pictureFilePath & "'," & _
" '" & sectionRow.SectionType & "');"

Dim sqlIDQuery As String
sqlIDQuery = "SELECT scope_identity();"

Dim sqlCommand As New SqlCommand(sqlString)
sqlCommand.Connection = sqlConnection

'Add Parameters
sqlCommand.Parameters.Add(pSectionName)

Dim SectionID As Integer
Try
sqlConnection.Open()
sqlCommand.ExecuteNonQuery() '***** THIS WORKS FINE AND
INSERTS RECORD.

sqlCommand.CommandText = sqlIDQuery
SectionID = CType(sqlCommand.ExecuteScalar, Integer) ' ****
FAILS HERE WITH AN EXCEPTION.

Catch ex As Exception
SectionID = 0
Finally
sqlConnection.Close()
End Try

Return SectionID
--
Best Regards

The Inimitable Mr Newbie º¿º


--
Best Regards

The Inimitable Mr Newbie º¿º
"Ken Tucker [MVP]" <Ke**********@discussions.microsoft.com> wrote in
message news:65**********************************@microsof t.com...
Hi,

Could you please post the query with the parameter in it so we
can
help figure out the problem?

Ken
------------------------

"Mr Newbie" wrote:
I was using this fine

sqlIDQuery = "SELECT SectionID FROM SECTIONS WHERE
(SectionID=SCOPE_IDENTITY());"

UNTIL. . . .

I added a parameter, now it fails, it doesent matter if I use the
parameter
or not, when I add a param, it falls over, Any Ideas ?

I know this is not the right place to post, but I know you guys are good
!!
--
Best Regards

The Inimitable Mr Newbie º¿º


Jan 16 '06 #4
Ive tried that ken. I have also tried creating a completely new sqlCommandID
sqlCommand object which shares the same connection but has its own SELECT
scope_identity() string and that doesent work either, it's really odd.! Try
it yourself with a single field table for brevity and you'll see what I
mean.

--
Best Regards

The Inimitable Mr Newbie º¿º
"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:ON**************@tk2msftngp13.phx.gbl...
Hi,

When you use sqlCommand.executenonquery you have a parameter
defined and use it.

At this line you change the command text for the sql command.

sqlCommand.CommandText = sqlIDQuery
The new command text does not have a parameter but you still have a
parameter defined. TThis causes your error. Try clearing the parameters
before you run the command the second time.

sqlCommand.Parameters.Clear()
SectionID = CType(sqlCommand.ExecuteScalar, Integer) ' ****

Ken
--------------------
"Mr Newbie" <he**@now.com> wrote in message
news:uG**************@TK2MSFTNGP14.phx.gbl...
The following
code successfully inserts a record in the Sections table, but the
scope_identity() returns DbNull. If I remove the parameter
'pSectionName' and replace it with dummy value, it works fine.
Alternatively, if I use @@IDENTITY with or withour the parameter, that
works fine too.

It seems there is a problem with using scope_identity() when parameters
are involved, but I do need to use parameters. Does anyone know why this
would happen and how to circumvent it ?
Dim sqlConnection As New SqlConnection(getConnectionString())

Dim sqlString As String
Dim result As Integer

Dim pSectionName As New SqlParameter("@pSectionName",
SqlDbType.NVarChar)
pSectionName.Value = sectionRow.SectionName

sqlString = "INSERT INTO SECTIONS " & _
"VALUES (" & _
" '" & sectionRow.ArticleID.ToString & "'," & _
" @pSectionName ," & _
" '" & sectionRow.SectionNumber.ToString & "'," & _
" '" & sectionRow.SectionFollowing.ToString & "'," &
_
" '" & sectionRow.Attachments.ToString & "'," & _
" '" & sectionRow._Text & "'," & _
" ''," & _
" '" & sectionRow.pictureName & "'," & _
" '" & sectionRow.pictureType & "'," & _
" '" & sectionRow.pictureFilePath & "'," & _
" '" & sectionRow.SectionType & "');"

Dim sqlIDQuery As String
sqlIDQuery = "SELECT scope_identity();"

Dim sqlCommand As New SqlCommand(sqlString)
sqlCommand.Connection = sqlConnection

'Add Parameters
sqlCommand.Parameters.Add(pSectionName)

Dim SectionID As Integer
Try
sqlConnection.Open()
sqlCommand.ExecuteNonQuery() '***** THIS WORKS FINE AND
INSERTS RECORD.

sqlCommand.CommandText = sqlIDQuery
SectionID = CType(sqlCommand.ExecuteScalar, Integer) ' ****
FAILS HERE WITH AN EXCEPTION.

Catch ex As Exception
SectionID = 0
Finally
sqlConnection.Close()
End Try

Return SectionID
--
Best Regards

The Inimitable Mr Newbie º¿º


--
Best Regards

The Inimitable Mr Newbie º¿º
"Ken Tucker [MVP]" <Ke**********@discussions.microsoft.com> wrote in
message news:65**********************************@microsof t.com...
Hi,

Could you please post the query with the parameter in it so we
can
help figure out the problem?

Ken
------------------------

"Mr Newbie" wrote:

I was using this fine

sqlIDQuery = "SELECT SectionID FROM SECTIONS WHERE
(SectionID=SCOPE_IDENTITY());"

UNTIL. . . .

I added a parameter, now it fails, it doesent matter if I use the
parameter
or not, when I add a param, it falls over, Any Ideas ?

I know this is not the right place to post, but I know you guys are
good !!
--
Best Regards

The Inimitable Mr Newbie º¿º



Jan 16 '06 #5

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

Similar topics

4
by: Scrappy | last post by:
I have an ASP front end on SQL 2000 database. I have a form that submits to an insert query. The entry field is an "identity" and the primary key. I have used scope_identity() to display the...
1
by: Lauren Quantrell | last post by:
I'm using an Access2K front end on a SQL Server2K backend. I use Scope_Identity() in a lot of stored procedures to pass the newwly inserted record's unique ID to the next select statement in the...
5
by: Larry | last post by:
I am seeing a problem with an ASP application, where I have 2 tables. In the first table, the ASP inserts just 1 row and retrieves the primary key of the new row using SCOPE_IDENTITY. It then uses...
6
by: Hardy Wang | last post by:
Hi all, I have the following codes, but SCOPE_IDENTITY() just returns NULL to me. If I comment out SCOPE_IDENTITY() line and run @@IDENTITY line, it works fine!! Since I have a trigger on the...
4
by: Mr Newbie | last post by:
The following code is comprises a function to create a section of a documemt. Each section is stored in a table. This successfully inserts a record in the Sections table, but the scope_identity()...
1
by: Justyn | last post by:
Hi everyone, I hope someone can help me out! I have added a trigger that gets fired on INSERT and which itself inserts data into another table WITHOUT an identity I used to get back the...
2
by: needin4mation | last post by:
Hi, I have generated my queries and selected the refresh the table option. This creates a select after the insert to get the scope_identity() so that I can use that key value in another table,...
8
by: Martin Z | last post by:
INSERT INTO dbo.Transmission (TransmissionDate, TransmissionDirection, Filename, TransmittedData) VALUES (@TransmissionDate,@TransmissionDirection,@Filename,@TransmittedData); SELECT @retVal =...
10
by: Mike | last post by:
Sql Server, Scope_Identity(), Ado.NET: Which is better? Using an output parameter to return Scope_Identity through ExecuteNonQuery(), or adding Select Scope_Identity() to the end of the...
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...
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
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...
0
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...
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,...

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.