?
Solved

Get return value from ObjectDataSource

Posted on 2006-11-29
7
Medium Priority
?
4,966 Views
Last Modified: 2008-02-20
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.SelectParameters(0).ReturnValue
End Sub

This obviously doesn't work, but hopefully you see what I'm trying to do here.

Thank you!
0
Comment
Question by:atastec
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 28

Expert Comment

by:strickdd
ID: 18039108

Sub SomeGridView_Inserted(s As Object, e As ObjectDataSourceStatusEventArgs)
  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>
0
 
LVL 5

Author Comment

by:atastec
ID: 18039609
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 DetailsViewInsertedEventArgs, not ObjectDataSourceStatusEventArgs.  Any ideas?  Am I missing something?
0
 
LVL 28

Accepted Solution

by:
strickdd earned 1500 total points
ID: 18039651
Try this:

Sub ObjectDataSource1_Inserted(s As Object, e As ObjectDataSourceStatusEventArgs)
  Dim intId As Integer
  intId = e.OutputParameters("Return")
End Sub
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Author Comment

by:atastec
ID: 18040012
Closer, but I still can't get it to work.
This is basically what I have:

<asp:ObjectDataSource ID="srcClient" runat="server"
                TypeName="Intranet.BusinessLogicLayer.Client"
                InsertMethod="Insert"
                OnInserted="Client_Inserted" >
            <InsertParameters>
                <asp:Parameter Direction="InputOutput" Name="ReturnValue" Type="Object" />
            </InsertParameters>
</asp:ObjectDataSource>

Sub Client_Inserted(ByVal s As Object, ByVal e As ObjectDataSourceStatusEventArgs)
        Dim intId As Integer
        intId = e.OutputParameters("ReturnValue")
        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?
0
 
LVL 28

Expert Comment

by:strickdd
ID: 18040144
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(ConfigurationManager.ConnectionStrings["MyConn"]);
SqlCommand Command = new SqlCommand("usr_InsertNew", Connection);
Command.CommandType = CommandType.StoredProcedure;

SqlParameter sqlParam = new SqlParameter();
sqlParam.ParameterName = "@ReturnVal";
sqlParam.Direction = ParameterDirection.Output;
sqlParam.Size = 4;
0
 
LVL 5

Author Comment

by:atastec
ID: 18040271
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.BusinessLogicLayer.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.
0
 
LVL 5

Author Comment

by:atastec
ID: 18040549
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.BusinessLogicLayer.Client"
                InsertMethod="Insert"
                OnInserted="Client_Inserted" >
            <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 ObjectDataSourceStatusEventArgs)
        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!
0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question