How to pass a value to a stored procedure and then show the result in a readonlylist

How to pass a value to a stored procedure and then show the result in a readonlylist

Old forum URL: forums.lhotka.net/forums/t/544.aspx


f-scherer posted on Wednesday, July 05, 2006

Hi,

i have an asp.net form with a search textbox. This textbox is empty when loading the page. I also want to use the csladatasource to show the result of my query in a detailsview.

The problem is that the detailsview wants to show the values right away (at page load) But at this point i havenĀ“t passed a criteria yet.

Can someone give me an advice? Is it possible to use the csladatasource for my problem?


Thanks in advance,

frank.

skagen00 replied on Wednesday, July 05, 2006

CslaDataSource, in order to get it to reassign the business object through the "select" (where one codes something like this: e.BusinessObject = myQuery.GetQueryResults()..), only gets triggered on non-postback page calls if I'm correct.

One way might be to go away from the CslaDataSource. Another might be to do a Server.Transfer to itself and then refer back to the calling page to get the criteria and then to facilitate the retrieval and assign e.BusinessObject to the newly retrieved object - since Server.Transfer will facilitate a postback obviously, it'll trigger that select code. (I use the latter technique myself when it comes to a maintenance form right now where various parts of the BO can be manipulated but must be fully valid before saving is possible).

In the case of what you're talking about, I would probably opt for the former approach (not use the CslaDataSource).

I too would be interested in knowing if something else can "rebind" to a new business object myself other than a non-postback page call. (In which case you'd just bind to an empty result set initially and rebind afterwards).

My two cents.

Chris

 

f-scherer replied on Wednesday, July 05, 2006

Hi Chris,

thanks for the quick answer. I am still very new the csla. Earlier i always used datasets to pass database values to a frontend.

Since i want to continue using csla what would be the best to pass values... I read the book and following the example values are always as type object.

Thanks in advance,

frank.


skagen00 replied on Wednesday, July 05, 2006

I'll give you an example of what I do for queries, and hopefully it'll help.

I have what is called a Query object. It's an editable root. I can request new queries, retrieve a saved one, delete one, etc. The Query object contains the information defining the query. What elements are part of my search both to show as criteria and as result set items (it's a data-centric search engine).

I have, on my Query object, an ExecuteQuery command. (I'm assuming you're using Csla 2.0 -- if not, since you're new, I'd recommend stepping up to it). On my Query object, I have an instance method on my Query object called Execute(). It will utilize the ExecuteQuery command to perform a server side activity that will construct the dynamic SQL and return a DataTable and such.

Within the ExecuteQuery command is where you'd invoke your stored procedure (instead of what I do in my case, where I'm invoking a command type other than a stored procedure). You'll note that my command assigns values for the QueryResults, and that the instance method Execute() returns this QueryResults. This is how my UI code will utilize the "return value" of the stored procedure, so to say.

So my instance method looks like this on my Query (BusinessBase<Query>):

/// <summary>

/// Executes the Query, returning a DataTable.

/// </summary>

public QueryResults Execute()

{

ExecuteQueryCommand executeCommand;

executeCommand = DataPortal.Execute<ExecuteQueryCommand>(new ExecuteQueryCommand(this));

return executeCommand.Results;

}

And then the command looks like this:

        /// <summary>
        /// This command executes the Query, also recording the SQL used and the execution time.
        /// </summary>
        [Serializable()]
        private class ExecuteQueryCommand : CommandBase
        {
            private Query _query;
            private QueryResults _results;

            public QueryResults Results
            {
                get
                {
                    return _results;
                }
            }

            public ExecuteQueryCommand(Query queryToExecute)
            {
                _query = queryToExecute;
            }

            protected override void DataPortal_Execute()
            {
                querySQL = <Dynamically constructed SQL>

                <snip>

                selectColumnsCommand = new SqlCommand(querySQL, connection);

                legacyDataAdapter = new SqlDataAdapter(selectColumnsCommand);

                connection.Open();

                legacyDataAdapter.Fill(dt);

                }

  <snip>


                executionTimeMS = ...execution time;

                _results = new QueryResults(executionTimeMS, querySQL, dt);

            }
        }

Hope that helps.

RockfordLhotka replied on Wednesday, July 05, 2006

You need to understand how Web Forms data binding is working - this isn't specific to CslaDataSource, but rather to the broader data binding infrastructure.

The data binding mechanism is very lazy - which is good, as it is an optimization feature. What happens is this: a UI control (grid, etc.) determines that it needs data, so it asks for that data from data binding, which asks the data control, which asks the data source.

In the case of CslaDataSource, that last step means that the select event is raised so you know to provide the requested data.

To cause data on a page to reload at any time, you need to tell the UI control (grid, etc.) that it needs new data. Typically this is done by simply calling that control's DataBind() method. That tells the control to rebind, which causes it to ask data binding for data, which asks the control, which (with CslaDataSource) raises the select event.

So, after you've retrieved your other data, and need to refresh the drop-down list or whatever, simply add code to invalidate the drop-down list's data and it will rebinding all by itself.

skagen00 replied on Wednesday, July 05, 2006

RockfordLhotka:

To cause data on a page to reload at any time, you need to tell the UI control (grid, etc.) that it needs new data. Typically this is done by simply calling that control's DataBind() method. That tells the control to rebind, which causes it to ask data binding for data, which asks the control, which (with CslaDataSource) raises the select event.

That's very useful information, thank you.

glenntoy replied on Wednesday, July 05, 2006

I did have some problems last time when it calls the page_load it will populates the Gridview, however I found a trick no to trigger the SelectObject() event in  csladatasource during the first load. In your Gridview, convert all fields to template fields. Setting all fields to template fields, you also dont need to set the DataKeyNames properties.

Cheers,

Glenn

f-scherer replied on Thursday, July 06, 2006

Hi,

now instead of using the csladatasource to bind data, what should i use to bind data coming from a readonlylist to a gridview for example?

Sorry if this question is dumb, but i am still used to write a method passing me a dataset and then binding it  to a control for example....

thx in advance,

frank.

glenntoy replied on Friday, July 07, 2006

Hi Frank,

If your GridView doesn't do anything, just simply displaying a set of results you can use ObjectDataSource.

Cheers,

Glenn

 

 

Copyright (c) Marimer LLC