HighTechTalks DotNet Forums  

Scope_Identity returning Decimal for an Integer field

Dotnet Framework (ADO.net) microsoft.public.dotnet.framework.adonet


Discuss Scope_Identity returning Decimal for an Integer field in the Dotnet Framework (ADO.net) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
RobinS
 
Posts: n/a

Default Scope_Identity returning Decimal for an Integer field - 04-15-2007 , 01:32 PM






SQLServer2005, VS2005, C#.

I have a stored procedure that returns a SCOPE_IDENTITY after doing an
insert.

Here's the pertinent part of the SQL.

INSERT INTO PlanData
(PlanName, ...) VALUES (@PlanName, ...)
SELECT PlanID = SCOPE_IDENTITY()

PlanID is defined as an integer in the database and in my business object.

To get the data back, I am retrieving a datatable, which should have one
row and column. So to pull my identity column, I am doing this:

PlanId = (int)dt.Rows[0][cn_PlanID]; //cn_PlanID = "PlanID", the name
of the SQL column

I get an error "Cannot unbox 'dt.Rows[0][cn_PlanID]' as a 'int'"

When I do a dt.Rows[0][cn_PlanID].GetType();, it says it's a decimal.

I *can* do this:

PlanId = (int)(decimal)dt.Rows[0][cn_PlanID];

or this:

PlanId = (int)dt.Rows[0][cn_PlanID].ToString();

I tried changing it to use ExecuteScalar, and it does the same thing; my
SQL procedure returns a Decimal. I tried retrieving @@Identity, and it does
the same thing.

Any idea what I'm doing wrong or what I've missed? It seems stupid to have
to cast it as decimal and then recast it as int.

Thanks in advance,
Robin S.



Reply With Quote
  #2  
Old   
Travis
 
Posts: n/a

Default RE: Scope_Identity returning Decimal for an Integer field - 04-18-2007 , 04:02 PM






In your SQL, try PlanID = CAST(SCOPE_IDENTITY() as int)


Reply With Quote
  #3  
Old   
RobinS
 
Posts: n/a

Default Re: Scope_Identity returning Decimal for an Integer field - 04-19-2007 , 02:46 AM



Okay, I can do that. Why is it returning it as a decimal? Is that just what
SQLServer does? I don't have this problem in VB, even though I have Option
Strict On. It just happens to me in C#. Weird.
Robin S.
----------------------------
"Travis" <Travis (AT) discussions (DOT) microsoft.com> wrote

Quote:
In your SQL, try PlanID = CAST(SCOPE_IDENTITY() as int)




Reply With Quote
  #4  
Old   
Armin Zingler
 
Posts: n/a

Default Re: Scope_Identity returning Decimal for an Integer field - 07-24-2007 , 09:10 AM



"RobinS" <RobinS (AT) NoSpam (DOT) yah.none> schrieb
Quote:
Okay, I can do that. Why is it returning it as a decimal? Is that
just what SQLServer does? I don't have this problem in VB, even
though I have Option Strict On. It just happens to me in C#. Weird.
Robin S.
I have the same problem. The field is an int field, but after INSERTing,
select @@identity returns a Decimal value (the value is correct, but the
type is wrong). (SQL server Express 2005)

Any idea?


Armin



Reply With Quote
  #5  
Old   
William Vaughn
 
Posts: n/a

Default Re: Scope_Identity returning Decimal for an Integer field - 07-24-2007 , 11:52 AM



This I gotta see. Let's see your code (SQL and how you're calling the
routine). @@Identity is an INT or BIGINT.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant, Dad, Grandpa
Microsoft MVP
INETA Speaker
www.betav.com
www.betav.com/blog/billva
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
Visit www.hitchhikerguides.net to get more information on my latest book:
Hitchhiker's Guide to Visual Studio and SQL Server (7th Edition)
and Hitchhiker's Guide to SQL Server 2005 Compact Edition (EBook)
-----------------------------------------------------------------------------------------------------------------------

"Armin Zingler" <az.nospam (AT) freenet (DOT) de> wrote

Quote:
"RobinS" <RobinS (AT) NoSpam (DOT) yah.none> schrieb
Okay, I can do that. Why is it returning it as a decimal? Is that
just what SQLServer does? I don't have this problem in VB, even
though I have Option Strict On. It just happens to me in C#. Weird.
Robin S.

I have the same problem. The field is an int field, but after INSERTing,
select @@identity returns a Decimal value (the value is correct, but the
type is wrong). (SQL server Express 2005)

Any idea?


Armin


Reply With Quote
  #6  
Old   
Armin Zingler
 
Posts: n/a

Default Re: Scope_Identity returning Decimal for an Integer field - 07-24-2007 , 12:56 PM



"William Vaughn" <billvaNoSPAM (AT) betav (DOT) com> schrieb
Quote:
This I gotta see. Let's see your code (SQL and how you're calling the
routine). @@Identity is an INT or BIGINT.
Thanks for your reply. Here some excerpts from the code and the table
definition:


Private f_CMDSelectIdentity As SqlCommand

'...

'f_con is the SQLConnection
f_CMDSelectIdentity = New SqlCommand("select @@identity", f_Con)


'...

Private Function GetIdentity() As Integer

Dim dr As SqlDataReader

dr = f_CMDSelectIdentity.ExecuteReader
dr.Read()
'TODO: conversion is workaround
Return CInt(DirectCast(dr(0), Decimal)) '<----------------------
End Function

(ExecuteScalar behaves the same)

I call GetIdentity /immediatelly/ after executing this SQL:

INSERT INTO Documents (...) values (...)

(of course, the identity column is not included in the SQL)


Table Documents (from management studio express):

CREATE TABLE [dbo].[Documents](
[ID] [int] IDENTITY(1,1) NOT NULL,
....


As you see, ID is an int identity field. In Function GetIdentity, I was
previously using

Return Directcast(dr(0), Integer)

which lead to an exception. When examining dr(0) in debug mode, I found out
that it's type is Decimal, not Integer as expected. Though, the value is
always correct.


Armin



Reply With Quote
  #7  
Old   
William Vaughn
 
Posts: n/a

Default Re: Scope_Identity returning Decimal for an Integer field - 07-24-2007 , 05:07 PM



Ah, why are you casting to a Decimal in the first place? I must have missed
something. This example uses a couple of different ways to execute SQL and
return a value. Using the DataReader for a single scalar value is the last
choice I would make.

Try
cn = New OleDbConnection(My.Settings.ConnectToJet)
cn.Open()
cmd = New OleDbCommand("INSERT INTO Shippers (CompanyName,
Phone) VALUES ('USPS', '(214) 555-1212')", cn)
cmd.ExecuteNonQuery()
cmd.CommandText = "SELECT @@Identity"
Dim objIdentity As Object
objIdentity = cmd.ExecuteScalar
Debug.Print(objIdentity.ToString)
Dim dr As OleDbDataReader
dr = cmd.ExecuteReader
If dr.HasRows Then
dr.Read()
Dim objID As Object
objID = dr.GetValue(0)
Debug.Print(objID.ToString)
End If

cn.Close()
Catch ex As Exception
MsgBox(ex.ToString)
End Try

hth

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant, Dad, Grandpa
Microsoft MVP
INETA Speaker
www.betav.com
www.betav.com/blog/billva
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
Visit www.hitchhikerguides.net to get more information on my latest book:
Hitchhiker's Guide to Visual Studio and SQL Server (7th Edition)
and Hitchhiker's Guide to SQL Server 2005 Compact Edition (EBook)
-----------------------------------------------------------------------------------------------------------------------

"Armin Zingler" <az.nospam (AT) freenet (DOT) de> wrote

Quote:
"William Vaughn" <billvaNoSPAM (AT) betav (DOT) com> schrieb
This I gotta see. Let's see your code (SQL and how you're calling the
routine). @@Identity is an INT or BIGINT.

Thanks for your reply. Here some excerpts from the code and the table
definition:


Private f_CMDSelectIdentity As SqlCommand

'...

'f_con is the SQLConnection
f_CMDSelectIdentity = New SqlCommand("select @@identity", f_Con)


'...

Private Function GetIdentity() As Integer

Dim dr As SqlDataReader

dr = f_CMDSelectIdentity.ExecuteReader
dr.Read()
'TODO: conversion is workaround
Return CInt(DirectCast(dr(0), Decimal)) '<----------------------
End Function

(ExecuteScalar behaves the same)

I call GetIdentity /immediatelly/ after executing this SQL:

INSERT INTO Documents (...) values (...)

(of course, the identity column is not included in the SQL)


Table Documents (from management studio express):

CREATE TABLE [dbo].[Documents](
[ID] [int] IDENTITY(1,1) NOT NULL,
....


As you see, ID is an int identity field. In Function GetIdentity, I was
previously using

Return Directcast(dr(0), Integer)

which lead to an exception. When examining dr(0) in debug mode, I found
out
that it's type is Decimal, not Integer as expected. Though, the value is
always correct.


Armin



Reply With Quote
  #8  
Old   
Armin Zingler
 
Posts: n/a

Default Re: Scope_Identity returning Decimal for an Integer field - 07-24-2007 , 05:25 PM



"William Vaughn" <billvaNoSPAM (AT) betav (DOT) com> schrieb
Quote:
Ah, why are you casting to a Decimal in the first place?
Because it /is/ a Decimal value. Casting to an Integer throws an exception
if it is a Decimal.

Quote:
I must have
missed something. This example uses a couple of different ways to
execute SQL and return a value. Using the DataReader for a single
scalar value is the last choice I would make.
I know how to get the value, though I don't know why the type is Decimal,
not Integer.


Quote:
Try
cn = New OleDbConnection(My.Settings.ConnectToJet)

Be aware of the fact that I am using an SqlConnection, not an
OleDBConnection.


Quote:
cn.Open()
cmd = New OleDbCommand("INSERT INTO Shippers
(CompanyName, Phone) VALUES ('USPS', '(214) 555-1212')", cn)
cmd.ExecuteNonQuery()
cmd.CommandText = "SELECT @@Identity"
Dim objIdentity As Object
objIdentity = cmd.ExecuteScalar
Debug.Print(objIdentity.ToString)
Dim dr As OleDbDataReader
dr = cmd.ExecuteReader
If dr.HasRows Then
dr.Read()
Dim objID As Object
objID = dr.GetValue(0)
Insert this line here:

MsgBox(dr.GetValue(0).GetType.FullName())

Which type name is displayed?


Quote:
Debug.Print(objID.ToString)
End If

cn.Close()
Catch ex As Exception
MsgBox(ex.ToString)
End Try

hth

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant, Dad, Grandpa
Microsoft MVP
INETA Speaker
www.betav.com
www.betav.com/blog/billva
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no
rights. __________________________________
Visit www.hitchhikerguides.net to get more information on my latest
book: Hitchhiker's Guide to Visual Studio and SQL Server (7th
Edition)
and Hitchhiker's Guide to SQL Server 2005 Compact Edition (EBook)
-----------------------------------------------------------------------------------------------------------------------

"Armin Zingler" <az.nospam (AT) freenet (DOT) de> wrote in message
news:e2BrbOhzHHA.3600 (AT) TK2MSFTNGP04 (DOT) phx.gbl...
"William Vaughn" <billvaNoSPAM (AT) betav (DOT) com> schrieb
This I gotta see. Let's see your code (SQL and how you're
calling the routine). @@Identity is an INT or BIGINT.

Thanks for your reply. Here some excerpts from the code and the
table definition:


Private f_CMDSelectIdentity As SqlCommand

'...

'f_con is the SQLConnection
f_CMDSelectIdentity = New SqlCommand("select @@identity", f_Con)


'...

Private Function GetIdentity() As Integer

Dim dr As SqlDataReader

dr = f_CMDSelectIdentity.ExecuteReader
dr.Read()
'TODO: conversion is workaround
Return CInt(DirectCast(dr(0), Decimal))
'<---------------------- End Function

(ExecuteScalar behaves the same)

I call GetIdentity /immediatelly/ after executing this SQL:

INSERT INTO Documents (...) values (...)

(of course, the identity column is not included in the SQL)


Table Documents (from management studio express):

CREATE TABLE [dbo].[Documents](
[ID] [int] IDENTITY(1,1) NOT NULL,
....


As you see, ID is an int identity field. In Function GetIdentity,
I was previously using

Return Directcast(dr(0), Integer)

which lead to an exception. When examining dr(0) in debug mode, I
found out
that it's type is Decimal, not Integer as expected. Though, the
value is always correct.


Armin




Reply With Quote
  #9  
Old   
William Vaughn
 
Posts: n/a

Default Re: Scope_Identity returning Decimal for an Integer field - 07-24-2007 , 06:07 PM



Okay, let's go back two steps. What DBMS engine are you using? JET? SQL
Server? Something else?

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant, Dad, Grandpa
Microsoft MVP
INETA Speaker
www.betav.com
www.betav.com/blog/billva
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
Visit www.hitchhikerguides.net to get more information on my latest book:
Hitchhiker's Guide to Visual Studio and SQL Server (7th Edition)
and Hitchhiker's Guide to SQL Server 2005 Compact Edition (EBook)
-----------------------------------------------------------------------------------------------------------------------

"Armin Zingler" <az.nospam (AT) freenet (DOT) de> wrote

Quote:
"William Vaughn" <billvaNoSPAM (AT) betav (DOT) com> schrieb
Ah, why are you casting to a Decimal in the first place?

Because it /is/ a Decimal value. Casting to an Integer throws an exception
if it is a Decimal.

I must have
missed something. This example uses a couple of different ways to
execute SQL and return a value. Using the DataReader for a single
scalar value is the last choice I would make.

I know how to get the value, though I don't know why the type is Decimal,
not Integer.


Try
cn = New OleDbConnection(My.Settings.ConnectToJet)


Be aware of the fact that I am using an SqlConnection, not an
OleDBConnection.


cn.Open()
cmd = New OleDbCommand("INSERT INTO Shippers
(CompanyName, Phone) VALUES ('USPS', '(214) 555-1212')", cn)
cmd.ExecuteNonQuery()
cmd.CommandText = "SELECT @@Identity"
Dim objIdentity As Object
objIdentity = cmd.ExecuteScalar
Debug.Print(objIdentity.ToString)
Dim dr As OleDbDataReader
dr = cmd.ExecuteReader
If dr.HasRows Then
dr.Read()
Dim objID As Object
objID = dr.GetValue(0)

Insert this line here:

MsgBox(dr.GetValue(0).GetType.FullName())

Which type name is displayed?


Debug.Print(objID.ToString)
End If

cn.Close()
Catch ex As Exception
MsgBox(ex.ToString)
End Try

hth

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant, Dad, Grandpa
Microsoft MVP
INETA Speaker
www.betav.com
www.betav.com/blog/billva
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no
rights. __________________________________
Visit www.hitchhikerguides.net to get more information on my latest
book: Hitchhiker's Guide to Visual Studio and SQL Server (7th
Edition)
and Hitchhiker's Guide to SQL Server 2005 Compact Edition (EBook)
-----------------------------------------------------------------------------------------------------------------------

"Armin Zingler" <az.nospam (AT) freenet (DOT) de> wrote in message
news:e2BrbOhzHHA.3600 (AT) TK2MSFTNGP04 (DOT) phx.gbl...
"William Vaughn" <billvaNoSPAM (AT) betav (DOT) com> schrieb
This I gotta see. Let's see your code (SQL and how you're
calling the routine). @@Identity is an INT or BIGINT.

Thanks for your reply. Here some excerpts from the code and the
table definition:


Private f_CMDSelectIdentity As SqlCommand

'...

'f_con is the SQLConnection
f_CMDSelectIdentity = New SqlCommand("select @@identity", f_Con)


'...

Private Function GetIdentity() As Integer

Dim dr As SqlDataReader

dr = f_CMDSelectIdentity.ExecuteReader
dr.Read()
'TODO: conversion is workaround
Return CInt(DirectCast(dr(0), Decimal))
'<---------------------- End Function

(ExecuteScalar behaves the same)

I call GetIdentity /immediatelly/ after executing this SQL:

INSERT INTO Documents (...) values (...)

(of course, the identity column is not included in the SQL)


Table Documents (from management studio express):

CREATE TABLE [dbo].[Documents](
[ID] [int] IDENTITY(1,1) NOT NULL,
....


As you see, ID is an int identity field. In Function GetIdentity,
I was previously using

Return Directcast(dr(0), Integer)

which lead to an exception. When examining dr(0) in debug mode, I
found out
that it's type is Decimal, not Integer as expected. Though, the
value is always correct.


Armin





Reply With Quote
  #10  
Old   
Armin Zingler
 
Posts: n/a

Default Re: Scope_Identity returning Decimal for an Integer field - 07-24-2007 , 07:15 PM



Well, what do you think as I use an SqlConnection in the example and as I
wrote "SQL server Express 2005" in my first post? :-)

Armin


Quote:
Okay, let's go back two steps. What DBMS engine are you using? JET? SQL
Server? Something else?

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant, Dad, Grandpa
Microsoft MVP
INETA Speaker
www.betav.com
www.betav.com/blog/billva
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no
rights.
__________________________________
Visit www.hitchhikerguides.net to get more information on my latest book:
Hitchhiker's Guide to Visual Studio and SQL Server (7th Edition)
and Hitchhiker's Guide to SQL Server 2005 Compact Edition (EBook)
-----------------------------------------------------------------------------------------------------------------------

"Armin Zingler" <az.nospam (AT) freenet (DOT) de> wrote in message
news:uYmGgkjzHHA.5484 (AT) TK2MSFTNGP03 (DOT) phx.gbl...
"William Vaughn" <billvaNoSPAM (AT) betav (DOT) com> schrieb
Ah, why are you casting to a Decimal in the first place?

Because it /is/ a Decimal value. Casting to an Integer throws an
exception
if it is a Decimal.

I must have
missed something. This example uses a couple of different ways to
execute SQL and return a value. Using the DataReader for a single
scalar value is the last choice I would make.

I know how to get the value, though I don't know why the type is Decimal,
not Integer.


Try
cn = New OleDbConnection(My.Settings.ConnectToJet)


Be aware of the fact that I am using an SqlConnection, not an
OleDBConnection.


cn.Open()
cmd = New OleDbCommand("INSERT INTO Shippers
(CompanyName, Phone) VALUES ('USPS', '(214) 555-1212')", cn)
cmd.ExecuteNonQuery()
cmd.CommandText = "SELECT @@Identity"
Dim objIdentity As Object
objIdentity = cmd.ExecuteScalar
Debug.Print(objIdentity.ToString)
Dim dr As OleDbDataReader
dr = cmd.ExecuteReader
If dr.HasRows Then
dr.Read()
Dim objID As Object
objID = dr.GetValue(0)

Insert this line here:

MsgBox(dr.GetValue(0).GetType.FullName())

Which type name is displayed?


Debug.Print(objID.ToString)
End If

cn.Close()
Catch ex As Exception
MsgBox(ex.ToString)
End Try

hth

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant, Dad, Grandpa
Microsoft MVP
INETA Speaker
www.betav.com
www.betav.com/blog/billva
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no
rights. __________________________________
Visit www.hitchhikerguides.net to get more information on my latest
book: Hitchhiker's Guide to Visual Studio and SQL Server (7th
Edition)
and Hitchhiker's Guide to SQL Server 2005 Compact Edition (EBook)
-----------------------------------------------------------------------------------------------------------------------

"Armin Zingler" <az.nospam (AT) freenet (DOT) de> wrote in message
news:e2BrbOhzHHA.3600 (AT) TK2MSFTNGP04 (DOT) phx.gbl...
"William Vaughn" <billvaNoSPAM (AT) betav (DOT) com> schrieb
This I gotta see. Let's see your code (SQL and how you're
calling the routine). @@Identity is an INT or BIGINT.

Thanks for your reply. Here some excerpts from the code and the
table definition:


Private f_CMDSelectIdentity As SqlCommand

'...

'f_con is the SQLConnection
f_CMDSelectIdentity = New SqlCommand("select @@identity", f_Con)


'...

Private Function GetIdentity() As Integer

Dim dr As SqlDataReader

dr = f_CMDSelectIdentity.ExecuteReader
dr.Read()
'TODO: conversion is workaround
Return CInt(DirectCast(dr(0), Decimal))
'<---------------------- End Function

(ExecuteScalar behaves the same)

I call GetIdentity /immediatelly/ after executing this SQL:

INSERT INTO Documents (...) values (...)

(of course, the identity column is not included in the SQL)


Table Documents (from management studio express):

CREATE TABLE [dbo].[Documents](
[ID] [int] IDENTITY(1,1) NOT NULL,
....


As you see, ID is an int identity field. In Function GetIdentity,
I was previously using

Return Directcast(dr(0), Integer)

which lead to an exception. When examining dr(0) in debug mode, I
found out
that it's type is Decimal, not Integer as expected. Though, the
value is always correct.


Armin






Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.