![]() | |
![]() |
| | Thread Tools | Search this Thread | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
#3
| |||
| |||
|
|
In your SQL, try PlanID = CAST(SCOPE_IDENTITY() as int) |
#4
| |||
| |||
|
|
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. |
#5
| |||
| |||
|
|
"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 |
#6
| |||
| |||
|
|
This I gotta see. Let's see your code (SQL and how you're calling the routine). @@Identity is an INT or BIGINT. |
#7
| |||
| |||
|
|
"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 |
#8
| |||||
| |||||
|
|
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 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 |
#9
| |||
| |||
|
|
"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 |
#10
| |||
| |||
|
|
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 |
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
| |