Dynamically change a datasource parameter

Posted on 2009-02-22
Last Modified: 2012-05-06
I have a default select parameter for a datasource in the aspx file.

I need to change the select parameter value in the aspx.cs file.
<asp:SqlDataSource ID="dscExceptionDetail" runat="server" 

         ConnectionString="<%$ ConnectionStrings:StockSelectSQLConnectionString %>" 





            <asp:ControlParameter ControlID="calFromDate" Name="FromDate" 

            propertyname="SelectedDate" Type = "DateTime" DefaultValue="2009-01-05" />

            <asp:ControlParameter ControlID="calToDate" Name="ToDate" 

            propertyname="SelectedDate" Type = "DateTime" DefaultValue="2009-02-05" />

            <asp:Parameter Name="SymbolID" Type = "int16" DefaultValue="1811" />




int intSymbolID = int.Parse(grdExceptions.SelectedRow.Cells[1].Text);

//This parameter has already been defined in the aspx file for the dscExceptionDetail datasource..


<asp:Parameter Name="SymbolID" Type = "int16" DefaultValue="1811" />

How do I change it here without clearing the other 3 parameters?

//Change the SymbolID selectparameter value of the the dscExceptionDetail from the "1811" 

default value to the  intSymbolID value


Open in new window

Question by:Dovberman
    LVL 10

    Accepted Solution

    Simply by overriding the Selecting event from your SQL DataSource

    Let me know
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" OnSelecting="SqlDataSource1_Selecting" ></asp:SqlDataSource>
    protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
            e.Command.Parameters["FromDate"] = new DateTime();
            e.Command.Parameters[0] = new DateTime();

    Open in new window


    Author Comment

    this is not working.

    I am selecting a row from the DataGrid.
    The Datasource DataBind refreshes based on new parmeter values.
    The chart which is based on the datasource should change, but it does not.
    I need the syntax that refreshes the chart.


    Author Comment

    The code below works, but only if I set the Command type to Text instead of StoredProcedure.
    I really need a way to reset the Stored Prodedure parameters to improve performance.
    // Reset ExceptionDetail (Does not work)
                //cmd.Parameters.Add("@FromDate", SqlDbType.DateTime).Value = strFromDate;
                //cmd.Parameters.Add("@ToDate", SqlDbType.DateTime).Value = strToDate;
                //cmd.Parameters.Add("@SymbolID", SqlDbType.Int).Value = grdExceptions.SelectedValue.ToString();
                // Reset ExceptionDetail (Works)
                int intSymbolID = int.Parse(grdExceptions.SelectedValue.ToString());
                string strSQL = "SELECT QuoteDate, ClosePrice FROM StockHist ";
                strSQL += "WHERE SymbolID=" + intSymbolID + "AND QuoteDate >= '" + strFromDate + "' ";
                strSQL += "AND QuoteDate <= '" + strToDate + "' ";
                strSQL += "ORDER BY QuoteDate ";
                dscExceptionDetail.SelectCommand = strSQL;
                // This refreshes the chart

    Open in new window

    LVL 10

    Expert Comment

    There is what you need to do is:

    1) Add the "SelectedIndexChanged" even on your GridView
    2) Add the "Selecting" on the SQLDataSource
    3) In the "SelectedIndexChanged" simply call "dscExceptionDetail.DataBind()";
    4) Then in the Selecting event change your parameters like i show.

    Let me know.

    Author Comment

    I will try your suggestions in the morning.
    It is Mardis Gras season here in Biloxi.


    Author Closing Comment

    Thank you. I will investigate later.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    Suggested Solutions

    AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
    In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    745 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

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now