R
RobinS
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.
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.