The way I always use and works for me is this:
in my StoredProcedure after inserting... return Scope_Identity()
In my code I declare:
cmd.Parameters.Add("@Retur
cmd.ExecuteNonQuery();
return cmd.Parameters("@ReturnVal
Main Topics
Browse All TopicsI have a stored proc that does a
SELECT SCOPE_IDENTITY() after inserting a record. I have tested the stored proc in query analyzer and it returns the new Key Value for the newly inserted row as expected.
In my DAL, I have a TableAdapter where one of the query is as follows (code is generated by VS2005)
[System.Diagnostics.Debugg
[System.ComponentModel.Des
public virtual object CreateUser(string Afis, string UserName, string FirstName, string LastName, string Email, string PhoneNumber, string RoomNumber, System.Nullable<short> DepartmentId, System.Nullable<short> LocationId) {
System.Data.SqlClient.SqlC
if ((Afis == null)) {
command.Parameters[1].Valu
}
else {
command.Parameters[1].Valu
}
if ((UserName == null)) {
command.Parameters[2].Valu
}
else {
command.Parameters[2].Valu
}
if ((FirstName == null)) {
command.Parameters[3].Valu
}
else {
command.Parameters[3].Valu
}
if ((LastName == null)) {
command.Parameters[4].Valu
}
else {
command.Parameters[4].Valu
}
if ((Email == null)) {
command.Parameters[5].Valu
}
else {
command.Parameters[5].Valu
}
if ((PhoneNumber == null)) {
command.Parameters[6].Valu
}
else {
command.Parameters[6].Valu
}
if ((RoomNumber == null)) {
command.Parameters[7].Valu
}
else {
command.Parameters[7].Valu
}
if ((DepartmentId.HasValue == true)) {
command.Parameters[8].Valu
}
else {
command.Parameters[8].Valu
}
if ((LocationId.HasValue == true)) {
command.Parameters[9].Valu
}
else {
command.Parameters[9].Valu
}
System.Data.ConnectionStat
if (((command.Connection.Stat
!= System.Data.ConnectionStat
command.Connection.Open();
}
object returnValue;
try {
returnValue = command.ExecuteScalar();
}
finally {
if ((previousConnectionState == System.Data.ConnectionStat
command.Connection.Close()
}
}
if (((returnValue == null)
|| (returnValue.GetType() == typeof(System.DBNull)))) {
return null;
}
else {
return ((object)(returnValue));
}
}
In my BLL, I have the following:
public static short CreateUser(UserProfile u)
{
short userId = 0;
try
{
userId = (short)usersAdapter.Create
}
catch (Exception e)
{
// TODO: add logging here
}
return userId;
}
}
However, whenever the code reaches
userId = (short)usersAdapter.Create
I get the following exception.
System.InvalidCastExceptio
Message="Specified cast is not valid."
Source="App_Code.-hk-pw4-"
StackTrace:
at UserProfileBLL.CreateUser(
I have the query's ExecuteMode set to "Scalar" in the TableAdapter.
What am I doing wrong here? I have spent way too much time trying to figure this out.
Please help.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Business Accounts
Answer for Membership
by: IUAATechPosted on 2007-03-16 at 12:06:33ID: 18737209
and just wanted to mention, the record is getting inserted. I am only having trouble getting the new key value