I 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
erNonUserC
odeAttribu
te()]
[System.ComponentModel.Des
ign.HelpKe
ywordAttri
bute("vs.d
ata.TableA
dapter")]
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
ommand command = this.CommandCollection[1];
if ((Afis == null)) {
command.Parameters[1].Valu
e = System.DBNull.Value;
}
else {
command.Parameters[1].Valu
e = ((string)(Afis));
}
if ((UserName == null)) {
command.Parameters[2].Valu
e = System.DBNull.Value;
}
else {
command.Parameters[2].Valu
e = ((string)(UserName));
}
if ((FirstName == null)) {
command.Parameters[3].Valu
e = System.DBNull.Value;
}
else {
command.Parameters[3].Valu
e = ((string)(FirstName));
}
if ((LastName == null)) {
command.Parameters[4].Valu
e = System.DBNull.Value;
}
else {
command.Parameters[4].Valu
e = ((string)(LastName));
}
if ((Email == null)) {
command.Parameters[5].Valu
e = System.DBNull.Value;
}
else {
command.Parameters[5].Valu
e = ((string)(Email));
}
if ((PhoneNumber == null)) {
command.Parameters[6].Valu
e = System.DBNull.Value;
}
else {
command.Parameters[6].Valu
e = ((string)(PhoneNumber));
}
if ((RoomNumber == null)) {
command.Parameters[7].Valu
e = System.DBNull.Value;
}
else {
command.Parameters[7].Valu
e = ((string)(RoomNumber));
}
if ((DepartmentId.HasValue == true)) {
command.Parameters[8].Valu
e = ((short)(DepartmentId.Valu
e));
}
else {
command.Parameters[8].Valu
e = System.DBNull.Value;
}
if ((LocationId.HasValue == true)) {
command.Parameters[9].Valu
e = ((short)(LocationId.Value)
);
}
else {
command.Parameters[9].Valu
e = System.DBNull.Value;
}
System.Data.ConnectionStat
e previousConnectionState = command.Connection.State;
if (((command.Connection.Stat
e & System.Data.ConnectionStat
e.Open)
!= System.Data.ConnectionStat
e.Open)) {
command.Connection.Open();
}
object returnValue;
try {
returnValue = command.ExecuteScalar();
}
finally {
if ((previousConnectionState == System.Data.ConnectionStat
e.Closed))
{
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
User(u.Afi
s, u.UserName, u.FirstName, u.LastName, u.Email, u.Phone, u.RoomNumber, u.Department, u.Location);
}
catch (Exception e)
{
// TODO: add logging here
}
return userId;
}
}
However, whenever the code reaches
userId = (short)usersAdapter.Create
User(u.Afi
s, u.UserName, u.FirstName, u.LastName, u.Email, u.Phone, u.RoomNumber, u.Department, u.Location);
I get the following exception.
System.InvalidCastExceptio
n was caught
Message="Specified cast is not valid."
Source="App_Code.-hk-pw4-"
StackTrace:
at UserProfileBLL.CreateUser(
UserProfil
e u) in c:\Users\radhikar\Document
s\Visual Studio 2005\Projects\IUAAOnline\A
pp_Code\Us
erProfile\
UserProfil
eBLL.cs:li
ne 94
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.