SqlDataSource: Changing SelectCommand property before processed

M

Martin

Hi,

I am implementing an SQL repository component. That mean that I want to
overload standard SqlDataSource component and add new property (commandName).
After that, I would like to update the SelectCommand and SelectParameters
properties just before the select is processed (ie. I look into the
repository, find the requested query template, build the text of the query
from the template and update the mentioned properties).

Currently, I have attached on the Selecting event and update the properties
there. Unfortunately, even the property is updated (checked by stepping in
the code), the new value is not used later in the process. Let me show you a
very simplified example. See the source code bellow.

When I run the aspx page, I received the following error, even the
SelectCommand was already changed in the Selecting event (checked by putting
a breakpoint and see the values in the Watch).
Could not find stored procedure 'not used'.

Therefore, I would like to ask you for a help with this issue. Please note
that we really need to update the SelectCommand for each one call of the
query because the command might varry for each particular call (it depends on
the parameters as well).

As a result, my main question is the following:
How to make the code working as expected (ie. how to implement the described
approach)?

Thanks for every tip,

Martin

--- SqlRepositoryDataSource.cs ---

public class SqlRepositoryDataSource :
System.Web.UI.WebControls.SqlDataSource
{
public SqlRepositoryDataSource()
{
Selecting += new
SqlDataSourceSelectingEventHandler(OnSelectingHandler);
}

protected void OnSelectingHandler(Object source,
SqlDataSourceSelectingEventArgs e)
{
SelectCommand = "Select * from test;";
SelectParameters.Add("a", "b");
}
}

--- DataSourceTest.aspx ---

<cc1:SqlRepositoryDataSource ID="SqlRepositoryDataSource1"
runat="server" CommandTemplateName="system.Framework.test.simple"
ConnectionString="<%$ ConnectionStrings:TestDbConnectionString %>"
SelectCommand="[not used]">
</cc1:SqlRepositoryDataSource>
<asp:GridView ID="GridView1" runat="server"
DataSourceID="SqlRepositoryDataSource1">
</asp:GridView>
 
M

Martin

Hi,

I have finally found that. The event arguments must be changed directly.

Thanks,

Martin
 

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,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top