HighTechTalks DotNet Forums  

Why isn't my SCOPE_IDENTITY() working?

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


Discuss Why isn't my SCOPE_IDENTITY() working? in the Dotnet Framework (ADO.net) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
news.microsoft.com
 
Posts: n/a

Default Why isn't my SCOPE_IDENTITY() working? - 11-17-2007 , 04:26 PM






I have a table called Activities with a primary key of ActivityId which is
an identity field. I can see the value after the insert is there. But my
return back from the ExecuteScalar method call is not working.

private const string SQL_INSERT_ACTIVITY = "INSERT INTO
APActivities(ActivityName,Description, " +
"Start,[End],CoordinatorId,ActivityTypeId,LocationId)
VALUES(@ActivityName,@Description, " +
"@Start,@End,@CoordinatorId,@ActivityTypeId,@Locat ionId); SELECT @ActivityId
= SCOPE_IDENTITY()";

here is my call for the insert.

Open(ConfigurationManager.AppSettings["SQL_CONN_STRING"]);
activity.ActivityId =
Convert.ToInt32(SQLHelper.ExecuteScalar(_internalC onnection,
_internalADOTransaction, CommandType.Text, SQL_INSERT_ACTIVITY,
activityParams));

here is my ExecuteScalar

public static int ExecuteScalar(SqlConnection conn, SqlTransaction trans,
CommandType cmdType, string cmdText, params SqlParameter[] cmdParms)
{
if (conn.State != ConnectionState.Open)
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = cmdText;
cmd.Connection = conn;
if (trans != null)
cmd.Transaction = trans;
PrepareCommand(cmd, cmdParms);
int val = Convert.ToInt32(cmd.ExecuteScalar());
return val;
}

The variable val is holding 0 after the call cmd.ExecuteScalar() returns.
But if I look at the table the row is there with the identity column filled
in.

Bill



Reply With Quote
  #2  
Old   
Kerry Moorman
 
Posts: n/a

Default RE: Why isn't my SCOPE_IDENTITY() working? - 11-17-2007 , 07:40 PM






Bill,

I don't know if this makes any differences, but I use "Select
Scope_Identity()" as the select that I batch with an insert statement and
retrieve using ExecuteScalar.

Kerry Moorman


"news.microsoft.com" wrote:

Quote:
I have a table called Activities with a primary key of ActivityId which is
an identity field. I can see the value after the insert is there. But my
return back from the ExecuteScalar method call is not working.

private const string SQL_INSERT_ACTIVITY = "INSERT INTO
APActivities(ActivityName,Description, " +
"Start,[End],CoordinatorId,ActivityTypeId,LocationId)
VALUES(@ActivityName,@Description, " +
"@Start,@End,@CoordinatorId,@ActivityTypeId,@Locat ionId); SELECT @ActivityId
= SCOPE_IDENTITY()";

here is my call for the insert.

Open(ConfigurationManager.AppSettings["SQL_CONN_STRING"]);
activity.ActivityId =
Convert.ToInt32(SQLHelper.ExecuteScalar(_internalC onnection,
_internalADOTransaction, CommandType.Text, SQL_INSERT_ACTIVITY,
activityParams));

here is my ExecuteScalar

public static int ExecuteScalar(SqlConnection conn, SqlTransaction trans,
CommandType cmdType, string cmdText, params SqlParameter[] cmdParms)
{
if (conn.State != ConnectionState.Open)
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = cmdText;
cmd.Connection = conn;
if (trans != null)
cmd.Transaction = trans;
PrepareCommand(cmd, cmdParms);
int val = Convert.ToInt32(cmd.ExecuteScalar());
return val;
}

The variable val is holding 0 after the call cmd.ExecuteScalar() returns.
But if I look at the table the row is there with the identity column filled
in.

Bill




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

Default Re: Why isn't my SCOPE_IDENTITY() working? - 11-17-2007 , 11:39 PM



try set nocount on

"news.microsoft.com" <billgower (AT) charter (DOT) net> wrote

Quote:
I have a table called Activities with a primary key of ActivityId which is
an identity field. I can see the value after the insert is there. But my
return back from the ExecuteScalar method call is not working.

private const string SQL_INSERT_ACTIVITY = "INSERT INTO
APActivities(ActivityName,Description, " +
"Start,[End],CoordinatorId,ActivityTypeId,LocationId)
VALUES(@ActivityName,@Description, " +
"@Start,@End,@CoordinatorId,@ActivityTypeId,@Locat ionId); SELECT
@ActivityId
= SCOPE_IDENTITY()";

here is my call for the insert.

Open(ConfigurationManager.AppSettings["SQL_CONN_STRING"]);
activity.ActivityId =
Convert.ToInt32(SQLHelper.ExecuteScalar(_internalC onnection,
_internalADOTransaction, CommandType.Text, SQL_INSERT_ACTIVITY,
activityParams));

here is my ExecuteScalar

public static int ExecuteScalar(SqlConnection conn, SqlTransaction trans,
CommandType cmdType, string cmdText, params SqlParameter[] cmdParms)
{
if (conn.State != ConnectionState.Open)
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = cmdText;
cmd.Connection = conn;
if (trans != null)
cmd.Transaction = trans;
PrepareCommand(cmd, cmdParms);
int val = Convert.ToInt32(cmd.ExecuteScalar());
return val;
}

The variable val is holding 0 after the call cmd.ExecuteScalar() returns.
But if I look at the table the row is there with the identity column
filled
in.

Bill





Reply With Quote
  #4  
Old   
Patrice
 
Posts: n/a

Default Re: Why isn't my SCOPE_IDENTITY() working? - 11-19-2007 , 05:28 AM



SELECT @var= etc.. doesn't return anything client side. Use SELECT
SCOPE_IDENTITY() instead (or add SELECT @ActivityId if you really need to
keep this value).

--
Patrice


"news.microsoft.com" <billgower (AT) charter (DOT) net> a écrit dans le message de
news: O1llgCWKIHA.4752 (AT) TK2MSFTNGP05 (DOT) phx.gbl...
Quote:
I have a table called Activities with a primary key of ActivityId which is
an identity field. I can see the value after the insert is there. But my
return back from the ExecuteScalar method call is not working.

private const string SQL_INSERT_ACTIVITY = "INSERT INTO
APActivities(ActivityName,Description, " +
"Start,[End],CoordinatorId,ActivityTypeId,LocationId)
VALUES(@ActivityName,@Description, " +
"@Start,@End,@CoordinatorId,@ActivityTypeId,@Locat ionId); SELECT
@ActivityId = SCOPE_IDENTITY()";

here is my call for the insert.

Open(ConfigurationManager.AppSettings["SQL_CONN_STRING"]);
activity.ActivityId =
Convert.ToInt32(SQLHelper.ExecuteScalar(_internalC onnection,
_internalADOTransaction, CommandType.Text, SQL_INSERT_ACTIVITY,
activityParams));

here is my ExecuteScalar

public static int ExecuteScalar(SqlConnection conn, SqlTransaction trans,
CommandType cmdType, string cmdText, params SqlParameter[] cmdParms)
{
if (conn.State != ConnectionState.Open)
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = cmdText;
cmd.Connection = conn;
if (trans != null)
cmd.Transaction = trans;
PrepareCommand(cmd, cmdParms);
int val = Convert.ToInt32(cmd.ExecuteScalar());
return val;
}

The variable val is holding 0 after the call cmd.ExecuteScalar() returns.
But if I look at the table the row is there with the identity column
filled in.

Bill




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.