SqlDataSource: Changing SelectCommand property before processed

Discussion in 'ASP .Net Web Controls' started by Martin, Mar 28, 2007.

  1. Martin

    Martin Guest

    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>
    Martin, Mar 28, 2007
    #1
    1. Advertising

  2. Martin

    Martin Guest

    Hi,

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

    Thanks,

    Martin

    "Martin" wrote:

    > 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>
    >
    >
    Martin, Mar 28, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tammy B
    Replies:
    0
    Views:
    744
    Tammy B
    Mar 9, 2006
  2. phil
    Replies:
    2
    Views:
    34,464
  3. John Kotuby
    Replies:
    1
    Views:
    867
    John Kotuby
    Feb 24, 2007
  4. sapta
    Replies:
    1
    Views:
    283
  5. Fred Dag

    In-line code in <asp:SqlDataSource SelectCommand tag

    Fred Dag, Aug 24, 2005, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    339
    Fred Dag
    Aug 24, 2005
Loading...

Share This Page