equentin
asked on
Retrieving Return Value from Stored Procedure using SqlDataSource
I've got a sqlDataSource that's running a stored procedure on the INSERT event. How do I get to the return value that's coming back from my DB? I'm assuming it's in the SqlDataSource1_Inserted event, but am not sure of the syntax.
Regards,
eq
Regards,
eq
apologies: its @resultCode
ASKER
Ok, thanks - but how do I code that in my event handler?
what code do you have at the moment?
you could just add it as a parameter, at a guess .Paramaters.Add("@resultco de", SqlDbType.varchar....
but we'll work on your code
you could just add it as a parameter, at a guess .Paramaters.Add("@resultco
but we'll work on your code
actually, apologies if i confuse you but i think you can just assign the value of ExecuteNonquery to a variable and it should assign the returned value to that variable. Eitehr way post your code and we'll go from there
ASKER
There isn't any code to post, because I haven't written it yet. Sorry if my question isn't clear, let me try again.
I have a SqlDataSource object bound to a DetailsView which I want to use to insert data to my DB using a stored procedure. This works fine, the data goes into the database, but my SP returns a reuturn code which I want to access in .NET and I don't know how. I'm assuming in the event handler for the data source in the Inserted event I can access the return value.
I have a SqlDataSource object bound to a DetailsView which I want to use to insert data to my DB using a stored procedure. This works fine, the data goes into the database, but my SP returns a reuturn code which I want to access in .NET and I don't know how. I'm assuming in the event handler for the data source in the Inserted event I can access the return value.
ASKER
Okay, I'm getting a bit closer, but still not quite there. I've got the following for my sqlDataSource:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:myConnec tionString %>"
InsertCommand="spInsertNew s" InsertCommandType="StoredP rocedure" OnInserted="SqlDataSource1 _Inserted" SelectCommand="SELECT * FROM [News] WHERE ([pk_NewsID] = @pk_NewsID)">
<InsertParameters>
<asp:Parameter Name="NewsDate" Type="DateTime" />
<asp:Parameter Name="Headline" Type="String" />
<asp:Parameter Name="Summary" Type="String" />
<asp:Parameter Name="Active" Type="Boolean" />
<asp:Parameter Name="Archive" Type="Boolean" />
<asp:Parameter Name="NewsCopy" Type="String" />
<asp:Parameter Direction="Output" Name="myID" Type="Int32" Size="4" />
</InsertParameters>
<SelectParameters>
<asp:QueryStringParameter DefaultValue="0" Name="pk_NewsID" QueryStringField="id"
Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
Then my stored procedure returns the ID of the inserted row. I've got the following in the sqlDataSource insert event:
Protected Sub SqlDataSource1_Inserted(By Val sender As Object, ByVal e As System.Web.UI.WebControls. SqlDataSou rceStatusE ventArgs) Handles SqlDataSource1.Inserted
Response.Write("Record Inserted: " & Server.HtmlEncode(e.Comman d.Paramete rs("@pk_Ne wsID").Val ue) & "<br/>")
End Sub
But I get the following error:
Conversion from type 'DBNull' to type 'String' is not valid.
Which would seem to appear that my referenced return value is NULL rather than containing the id....
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:myConnec
InsertCommand="spInsertNew
<InsertParameters>
<asp:Parameter Name="NewsDate" Type="DateTime" />
<asp:Parameter Name="Headline" Type="String" />
<asp:Parameter Name="Summary" Type="String" />
<asp:Parameter Name="Active" Type="Boolean" />
<asp:Parameter Name="Archive" Type="Boolean" />
<asp:Parameter Name="NewsCopy" Type="String" />
<asp:Parameter Direction="Output" Name="myID" Type="Int32" Size="4" />
</InsertParameters>
<SelectParameters>
<asp:QueryStringParameter DefaultValue="0" Name="pk_NewsID" QueryStringField="id"
Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
Then my stored procedure returns the ID of the inserted row. I've got the following in the sqlDataSource insert event:
Protected Sub SqlDataSource1_Inserted(By
Response.Write("Record Inserted: " & Server.HtmlEncode(e.Comman
End Sub
But I get the following error:
Conversion from type 'DBNull' to type 'String' is not valid.
Which would seem to appear that my referenced return value is NULL rather than containing the id....
ASKER
Sorry, should be:
Protected Sub SqlDataSource1_Inserted(By Val sender As Object, ByVal e As System.Web.UI.WebControls. SqlDataSou rceStatusE ventArgs) Handles SqlDataSource1.Inserted
Response.Write("Record Inserted: " & Server.HtmlEncode(e.Comman d.Paramete rs("@myID" ).Value) & "<br/>")
End Sub
Protected Sub SqlDataSource1_Inserted(By
Response.Write("Record Inserted: " & Server.HtmlEncode(e.Comman
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Have finally worked out what it is, I needed to change my output parameter in the <insertparameters>
<asp:Parameter Direction="ReturnValue" Name="myID" Type="Int32" Size="4" DefaultValue="0" />
I've marked your answer as accepted. Feel free to help with my other sqlDataSource question I've just asked though! ;-)
<asp:Parameter Direction="ReturnValue" Name="myID" Type="Int32" Size="4" DefaultValue="0" />
I've marked your answer as accepted. Feel free to help with my other sqlDataSource question I've just asked though! ;-)
you can call it with the @ReturnCode parameter in the command object
Apresto