Return value parameter error

D

David C

I am getting the following error after inserting a record in a SqlDataSource
and cannot figure out why.

Object cannot be cast from DBNull to other types.

I have below at the end of my stored procedure

RETURN SCOPE_IDENTITY();

In my SqlDataSource I have the Insert Command below

InsertCommand="EXEC mc_insBudgets @PersonID, @EnteredBy,
@ApprovedDate, @CreatedDate, @FirstName, @LastName, @MiddleName,
@MedicaidID, @Birthdate, @PartTypeID, @AnnualAllocation,
@BudgetEffectiveDate, @BudgetEndingDate, @Address1, @Address2, @City,
@State, @ZipCode, @Gender, @SSN, @Phone, @Email"
InsertCommandType="StoredProcedure">

In my InsertParameters I have below.

<InsertParameters>
<asp:parameter Name="PersonID" Type="Int32" DefaultValue="0" />
<asp:parameter Name="EnteredBy" Type="String" />
<asp:parameter Name="ApprovedDate" DbType="Date" />
<asp:parameter Name="CreatedDate" DbType="Date" />
<asp:parameter Name="FirstName" Type="String" />
<asp:parameter Name="LastName" Type="String" />
<asp:parameter Name="MiddleName" Type="String" />
<asp:parameter Name="Address1" Type="String" />
<asp:parameter Name="Address2" Type="String" />
<asp:parameter Name="City" Type="String" />
<asp:parameter Name="State" Type="String" />
<asp:parameter Name="ZipCode" Type="String" />
<asp:parameter Name="Gender" Type="String" />
<asp:parameter Name="SSN" Type="Int32" />
<asp:parameter Name="Phone" Type="String" />
<asp:parameter Name="Email" Type="String" />
<asp:parameter Name="MedicaidID" Type="String" />
<asp:parameter Name="Birthdate" DbType="Date" />
<asp:parameter Name="PartTypeID" Type="Int32" />
<asp:parameter Name="AnnualAllocation" Type="Decimal" />
<asp:parameter Name="BudgetEffectiveDate" DbType="Date" />
<asp:parameter Name="BudgetEndingDate" DbType="Date" />
<asp:parameter Name="NewBudgetID" Type="Int32"
Direction="ReturnValue" />
</InsertParameters>

In my SqlDataSource ItemInserted event I have the following.

Protected Sub SqlBudgets_Inserted(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.SqlDataSourceStatusEventArgs) Handles
SqlBudgets.Inserted
Dim newBudgetID As Integer =
Convert.ToInt32(e.Command.Parameters("@NewBudgetID").Value)
txtBudgetID.Text = newBudgetID.ToString
.....etc
End Sub

The error is occurring in the DIM line because apparently the parameter
value is null and I don't know why.
Any help is appreciated.

David
 
S

Scott M.

Have you tried getting any other parameters to see if they give you the same
problem? Have you tried using an ordinal instead of the parameter name?

Also, have you tried getting the data directly through the SQLDataSource
InstertCommand parameters collection instead of the e argument to the event
handler?

This will help narrow down the problem to just that parameter, or a problem
in general.

-Scott
 
J

Jesse Houwing

* David C wrote, On 16-9-2009 21:43:
I am getting the following error after inserting a record in a SqlDataSource
and cannot figure out why.

Object cannot be cast from DBNull to other types.

I have below at the end of my stored procedure

RETURN SCOPE_IDENTITY();

In my SqlDataSource I have the Insert Command below

InsertCommand="EXEC mc_insBudgets @PersonID, @EnteredBy,
@ApprovedDate, @CreatedDate, @FirstName, @LastName, @MiddleName,
@MedicaidID, @Birthdate, @PartTypeID, @AnnualAllocation,
@BudgetEffectiveDate, @BudgetEndingDate, @Address1, @Address2, @City,
@State, @ZipCode, @Gender, @SSN, @Phone, @Email"
InsertCommandType="StoredProcedure">

In my InsertParameters I have below.

<InsertParameters>
<asp:parameter Name="PersonID" Type="Int32" DefaultValue="0" />
<asp:parameter Name="EnteredBy" Type="String" />
<asp:parameter Name="ApprovedDate" DbType="Date" />
<asp:parameter Name="CreatedDate" DbType="Date" />
<asp:parameter Name="FirstName" Type="String" />
<asp:parameter Name="LastName" Type="String" />
<asp:parameter Name="MiddleName" Type="String" />
<asp:parameter Name="Address1" Type="String" />
<asp:parameter Name="Address2" Type="String" />
<asp:parameter Name="City" Type="String" />
<asp:parameter Name="State" Type="String" />
<asp:parameter Name="ZipCode" Type="String" />
<asp:parameter Name="Gender" Type="String" />
<asp:parameter Name="SSN" Type="Int32" />
<asp:parameter Name="Phone" Type="String" />
<asp:parameter Name="Email" Type="String" />
<asp:parameter Name="MedicaidID" Type="String" />
<asp:parameter Name="Birthdate" DbType="Date" />
<asp:parameter Name="PartTypeID" Type="Int32" />
<asp:parameter Name="AnnualAllocation" Type="Decimal" />
<asp:parameter Name="BudgetEffectiveDate" DbType="Date" />
<asp:parameter Name="BudgetEndingDate" DbType="Date" />
<asp:parameter Name="NewBudgetID" Type="Int32"
Direction="ReturnValue" />
</InsertParameters>

In my SqlDataSource ItemInserted event I have the following.

Protected Sub SqlBudgets_Inserted(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.SqlDataSourceStatusEventArgs) Handles
SqlBudgets.Inserted
Dim newBudgetID As Integer =
Convert.ToInt32(e.Command.Parameters("@NewBudgetID").Value)
txtBudgetID.Text = newBudgetID.ToString
.....etc
End Sub

The error is occurring in the DIM line because apparently the parameter
value is null and I don't know why.
Any help is appreciated.
try

> InsertCommand="mc_insBudgets"
> InsertCommandType="StoredProcedure">

and keep the rest of your code the same.
 
D

David C

I tried to follow the 4 Guys article 050207-1 but when I looked at it again,
the article showed the InsertParameters as ControlParameter and not plain
Parameter entries. Should that make a difference? Also, this is a ListView
so I am using the workaround for 2 DropDownList controls by populating them
in the Inserting event. Thanks.

David
 
S

Scott M.

But what about the questions I asked (since I don't know what the 4 Guys
article says)?

-Scott
 
D

David C

I tried the Ordinal position instead of the actual parameter name and it
gave me the same error. Then I tried returning a different ordinal position
of the same data type and it gave me the error below.

Procedure or function mc_insBudgets has too many arguments specified

Then I checked the parameter count and it is the exact same as the stored
procedure.

David
 
S

Scott M.

Well, there's got to be something wrong with the insert command, which is
causing the results to be null.

If you look at the SQLDataSource's properties in the Property window, you
can bring up the query designer by clicking on the InsertQuery property and
manually execute the query with dummy data to see what's happening.

-Scott
 
A

Andrew Morton

David said:
I am getting the following error after inserting a record in a
SqlDataSource and cannot figure out why.

Object cannot be cast from DBNull to other types.

I have below at the end of my stored procedure

RETURN SCOPE_IDENTITY();

In my SqlDataSource I have the Insert Command below

InsertCommand="EXEC mc_insBudgets @PersonID, @EnteredBy,
@ApprovedDate, @CreatedDate, @FirstName, @LastName, @MiddleName,
@MedicaidID, @Birthdate, @PartTypeID, @AnnualAllocation,
@BudgetEffectiveDate, @BudgetEndingDate, @Address1, @Address2, @City,
@State, @ZipCode, @Gender, @SSN, @Phone, @Email"
InsertCommandType="StoredProcedure">

I see 22 parameters there, but 23 parameters listed in your
InsertParameters.

What happens if you include the NewBudgetID parameter in the InsertCommand?

Andrew
 
D

David C

Andrew Morton said:
I see 22 parameters there, but 23 parameters listed in your
InsertParameters.

What happens if you include the NewBudgetID parameter in the
InsertCommand?

Andrew

It fails on too many parameters message.

David
 
A

Andrew Morton

David said:
It fails on too many parameters message.

Oh. If you iterate over all the keys in e.Command.Parameters after the call,
does it include NewBudgetID? Doesn't the SP need NewBudgetID declared as an
OUTPUT parameter into which you select the value, in the style of

ALTER PROCEDURE [dbo].[getDaysSinceCleanup]
@DaysAgo INTEGER OUTPUT
AS
SELECT @DaysAgo=DATEDIFF(d, lastRunDate, getdate()) FROM lastCleanup;

So you'd use SELECT @NewBudgetID=SCOPE_IDENTITY();

I looked up "RETURN [integer]" in BOL and it's an unconditional exit from a
query with an [optional] exit code (and it cannot return a null value),
rather than SELECT, which is used to return values (don't blame me, I didn't
define SQL).

I could, of course, be way out.

Andrew
 
S

Scott M.

Could it be that you are inserting a unique Person Id (via the @PersonID)
attribute, when that field is automatically populated by the database, in
which case, you are passing 1 parameter too many?

You usually don't send an ID to the database on an insert (because the DB
manages unique id's for you). You usually only have to send an ID to the DB
when you are doing updates, filtered selects, or deletes.

-Scott
 
D

David C

The PersonID is not the identity column and it can have multiples as it just
identifies the person for the budget.

David
 
D

David C

Andrew Morton said:
David said:
It fails on too many parameters message.

Oh. If you iterate over all the keys in e.Command.Parameters after the
call, does it include NewBudgetID? Doesn't the SP need NewBudgetID
declared as an OUTPUT parameter into which you select the value, in the
style of

ALTER PROCEDURE [dbo].[getDaysSinceCleanup]
@DaysAgo INTEGER OUTPUT
AS
SELECT @DaysAgo=DATEDIFF(d, lastRunDate, getdate()) FROM lastCleanup;

So you'd use SELECT @NewBudgetID=SCOPE_IDENTITY();

I looked up "RETURN [integer]" in BOL and it's an unconditional exit from
a query with an [optional] exit code (and it cannot return a null value),
rather than SELECT, which is used to return values (don't blame me, I
didn't define SQL).

I could, of course, be way out.

Andrew

I may try the OUTPUT parameter because the stored proc is working when I
include all parameters in the InsertCommand line, e.g. InsertCommand="EXEC
mc_insBudgets @PersonID, ......" and InsertCommandType="Text"

David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,965
Messages
2,570,148
Members
46,710
Latest member
FredricRen

Latest Threads

Top