Friday, March 23, 2012

Little help with a ReturnValue from SQL

I use a SP to write resident information to the SQL table. Then, I want to get the newly created RESIDENT_ID and send it back to my page.

Can someone give me a hand with it? I get the error:
"Cast from type 'DBNull' to type 'Integer' is not valid. "

Here is my SP:


CREATE PROCEDURE LS_resident_add
@.RESIDENT_ID int output,
@.HOUSE_ID as int,
@.Type as int = '0',
@.FName as varchar(50)=NULL,
@.LName as varchar(50)=NULL,
@.NickName as varchar(50)=NULL,
@.Email as varchar(50)=NULL,
@.Notes as varchar(1000)=NULL,
@.Access_Level as int = '0',
@.Password as varchar(8)=NULL,
@.Status as int = '0'

AS

INSERT INTO Street_Resident
(House_ID,
Type,
FName,
LName,
NickName,
Email,
Notes,
Access_Level,
Password,
Status)

VALUES
(@.House_ID,
@.Type,
@.FName,
@.LName,
@.NickName,
@.Email,
@.Notes,
@.Access_Level,
@.Password,
@.Status)
GO

Here is my script:


Sub resident_add(Source as Object, E as EventArgs)
Dim myConnection as New SqlConnection(ConfigurationSettings.AppSettings("MM_CONNECTION_STRING_LindenStreet"))
Dim myCommand as New SqlCommand("LS_resident_add", myConnection)
myCommand.CommandType=CommandType.StoredProcedure

myCommand.Parameters.Add(New SQLParameter("@.HOUSE_ID", frm_HOUSE_ID.SelectedItem.Value))
myCommand.Parameters.Add(New SQLParameter("@.Type", frm_Type.SelectedItem.Value))
IF frm_FName.text > "" THEN
myCommand.Parameters.Add(New SQLParameter("@.FName", frm_FName.Text))
END IF

IF frm_LName.text > "" THEN
myCommand.Parameters.Add(New SQLParameter("@.LName", frm_LName.Text))
END IF

IF frm_NickName.text > "" THEN
myCommand.Parameters.Add(New SQLParameter("@.NickName", frm_NickName.Text))
END IF

IF frm_Email.text > "" THEN
myCommand.Parameters.Add(New SQLParameter("@.Email", frm_Email.Text))
END IF

IF frm_Notes.text > "" THEN
myCommand.Parameters.Add(New SQLParameter("@.Notes", frm_Notes.Text))
END IF

IF frm_Access_Level.SelectedIndex > -1 THEN
myCommand.Parameters.Add(New SQLParameter("@.Access_Level", frm_Access_Level.SelectedItem.Value))
END IF

IF frm_Password.text > "" THEN
myCommand.Parameters.Add(New SQLParameter("@.Password", frm_Password.Text))
END IF

myCommand.Parameters.Add(New SQLParameter("@.Status", 1))

'Returns the new Resident ID so we can add any activities they signed up for
MyCommand.Parameters.Add("@.RESIDENT_ID", SqlDbType.Int)
MyCommand.Parameters("@.RESIDENT_ID").Direction = ParameterDirection.Output

myCommand.Connection.Open()
myCommand.ExecuteNonQuery

'Returns the new Resident ID so we can add any activities they signed up for
RESIDENT_ID = myCommand.Parameters("@.RESIDENT_ID").Value

myCommand.Connection.Close()
'Resident_Activity_delete()
Resident_Activity_Update()
Notify_User()
Dim strClose As String
strClose = "<script>"
strClose &= "window.close();"
strClose &= "</"
strClose &= "script>"
Response.Write(strClose)
End Sub

You're not returning anything from the sproc, thus the null reference error. Add a line something like this to the stored procedure:

RETURN SCOPE_IDENTITY()

Don

No comments:

Post a Comment