atastec
asked on
Get return value from ObjectDataSource
Given the following code how would I get the return value from InsertData():
<asp:ObjectDataSource ID="ObjectDataSource1" Runat="server"
TypeName = "MyNamespace.MyObject"
InsertMethod = "InsertData">
<InsertParameters>
<asp:Parameter Direction="ReturnValue" Name="Return" Type="Int32" />
</InsertParameters>
</asp:ObjectDataSource>
I would like to be able to say something like the following in my code-behind:
Sub SomeGridView_DataBound(s As Object, e As EventArgs)
Dim intId As Integer
intId = ObjectDataSource1.SelectPa rameters(0 ).ReturnVa lue
End Sub
This obviously doesn't work, but hopefully you see what I'm trying to do here.
Thank you!
<asp:ObjectDataSource ID="ObjectDataSource1" Runat="server"
TypeName = "MyNamespace.MyObject"
InsertMethod = "InsertData">
<InsertParameters>
<asp:Parameter Direction="ReturnValue" Name="Return" Type="Int32" />
</InsertParameters>
</asp:ObjectDataSource>
I would like to be able to say something like the following in my code-behind:
Sub SomeGridView_DataBound(s As Object, e As EventArgs)
Dim intId As Integer
intId = ObjectDataSource1.SelectPa
End Sub
This obviously doesn't work, but hopefully you see what I'm trying to do here.
Thank you!
ASKER
Thanks for responding!
I'm actually using a DetailsView to do the insert.
It's telling me the data type of the second parameter needs to be DetailsViewInsertedEventAr gs, not ObjectDataSourceStatusEven tArgs. Any ideas? Am I missing something?
I'm actually using a DetailsView to do the insert.
It's telling me the data type of the second parameter needs to be DetailsViewInsertedEventAr
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Closer, but I still can't get it to work.
This is basically what I have:
<asp:ObjectDataSource ID="srcClient" runat="server"
TypeName="Intranet.Busines sLogicLaye r.Client"
InsertMethod="Insert"
OnInserted="Client_Inserte d" >
<InsertParameters>
<asp:Parameter Direction="InputOutput" Name="ReturnValue" Type="Object" />
</InsertParameters>
</asp:ObjectDataSource>
Sub Client_Inserted(ByVal s As Object, ByVal e As ObjectDataSourceStatusEven tArgs)
Dim intId As Integer
intId = e.OutputParameters("Return Value")
reponse.write(intId)
End Sub
In my business logic layer this is what the Sub looks like:
Public Shared Sub Insert(ByVal ReturnValue As Object, _
ByVal Name As String, _
ByVal ShortName As String, _
ByVal ClientTypeID As Integer, _
ByVal CurrentClient As Boolean, _
ByVal ClientSince As String, _
ByVal Notes As String, _
ByVal CRMCompanyID As Integer)
ReturnValue = 5
End Sub
Given this, '5' should be written to the screen, but '0' is instead. What am I doing wrong?
This is basically what I have:
<asp:ObjectDataSource ID="srcClient" runat="server"
TypeName="Intranet.Busines
InsertMethod="Insert"
OnInserted="Client_Inserte
<InsertParameters>
<asp:Parameter Direction="InputOutput" Name="ReturnValue" Type="Object" />
</InsertParameters>
</asp:ObjectDataSource>
Sub Client_Inserted(ByVal s As Object, ByVal e As ObjectDataSourceStatusEven
Dim intId As Integer
intId = e.OutputParameters("Return
reponse.write(intId)
End Sub
In my business logic layer this is what the Sub looks like:
Public Shared Sub Insert(ByVal ReturnValue As Object, _
ByVal Name As String, _
ByVal ShortName As String, _
ByVal ClientTypeID As Integer, _
ByVal CurrentClient As Boolean, _
ByVal ClientSince As String, _
ByVal Notes As String, _
ByVal CRMCompanyID As Integer)
ReturnValue = 5
End Sub
Given this, '5' should be written to the screen, but '0' is instead. What am I doing wrong?
Are you calling a stored procedure with the objectdatasource? I have tried using a return value, but i couldn't do it through a datasource without designating a parameter as output ie:
CREATE PROCEDURE dbo.[usr_InsertNew]
(@ProjectID int OUTPUT )
And then in the proc:
SELECT @ProjectID = SCOPE_IDENTITY()
RETURN @ProjectID
You can also do this without a datasource like so:
SqlConnection Connection = new SqlConnection(Configuratio nManager.C onnectionS trings["My Conn"]);
SqlCommand Command = new SqlCommand("usr_InsertNew" , Connection);
Command.CommandType = CommandType.StoredProcedur e;
SqlParameter sqlParam = new SqlParameter();
sqlParam.ParameterName = "@ReturnVal";
sqlParam.Direction = ParameterDirection.Output;
sqlParam.Size = 4;
CREATE PROCEDURE dbo.[usr_InsertNew]
(@ProjectID int OUTPUT )
And then in the proc:
SELECT @ProjectID = SCOPE_IDENTITY()
RETURN @ProjectID
You can also do this without a datasource like so:
SqlConnection Connection = new SqlConnection(Configuratio
SqlCommand Command = new SqlCommand("usr_InsertNew"
Command.CommandType = CommandType.StoredProcedur
SqlParameter sqlParam = new SqlParameter();
sqlParam.ParameterName = "@ReturnVal";
sqlParam.Direction = ParameterDirection.Output;
sqlParam.Size = 4;
ASKER
The ObjectDataSource isn't calling a stored procedure. If you look closely at the code I've provided you can see it's calling a subroutine in a class I've written (Intranet.BusinessLogicLay er.Client) . To test this, all the ObjectDataSource does is call a subroutine (Insert) that sets the inputoutput parameter equal to 5, so we're not dealing with stored procedures at all at this point.
ASKER
I got it to work.
I changed the "Direction" on my InsertParemeters back to "ReturnValue" so my ObjectDataSouce looks like:
<asp:ObjectDataSource ID="srcClient" runat="server"
TypeName="Intranet.Busines sLogicLaye r.Client"
InsertMethod="Insert"
OnInserted="Client_Inserte d" >
<InsertParameters>
<asp:Parameter Direction="ReturnValue" Name="ReturnValue" Type="Int32" />
</InsertParameters>
</asp:ObjectDataSource>
I changed e.OutputParameters to e.ReturnValue so my code behind looks like:
Sub Client_Inserted(ByVal s As Object, ByVal e As ObjectDataSourceStatusEven tArgs)
Dim intId As Integer
intId = e.ReturnValue
response.write(intId)
End Sub
And finally I changed the Insert() subroutine to a function and return the value I need.
Thanks for your help strickdd!
I changed the "Direction" on my InsertParemeters back to "ReturnValue" so my ObjectDataSouce looks like:
<asp:ObjectDataSource ID="srcClient" runat="server"
TypeName="Intranet.Busines
InsertMethod="Insert"
OnInserted="Client_Inserte
<InsertParameters>
<asp:Parameter Direction="ReturnValue" Name="ReturnValue" Type="Int32" />
</InsertParameters>
</asp:ObjectDataSource>
I changed e.OutputParameters to e.ReturnValue so my code behind looks like:
Sub Client_Inserted(ByVal s As Object, ByVal e As ObjectDataSourceStatusEven
Dim intId As Integer
intId = e.ReturnValue
response.write(intId)
End Sub
And finally I changed the Insert() subroutine to a function and return the value I need.
Thanks for your help strickdd!
Sub SomeGridView_Inserted(s As Object, e As ObjectDataSourceStatusEven
Dim intId As Integer
intId = e.OutputParameters("Return
End Sub
<asp:ObjectDataSource ID="ObjectDataSource1" Runat="server"
TypeName = "MyNamespace.MyObject"
InsertMethod = "InsertData">
<InsertParameters>
<asp:Parameter Direction="InputOutput" Name="Return" Type="Object" />
</InsertParameters>
</asp:ObjectDataSource>