0

I want to create a dynamic select command in an SQLDataSource. It should read a value from the URL sent using get, grab it and stick it into the select command.

My sqldatadource is in my aspx page and looks like this

<asp:SqlDataSource ID="DS" Runat="server"

    SelectCommand="SELECT * FROM [dbo].[DS] WHERE [Install Process] = 'OCC' 
    AND [The Date] = @theDate"

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

</asp:SqlDataSource>

I want to update theDate parameter. I'm guessing you cant update it in Page_Load because the page hasn't loaded yet but I'm honestly not sure. can i call a codebehind function from within the sqldatasource or something...

This is my codebehind. It's currently in the page_load method, but i'm guessing that I need to move it? any help would be great. Thanks

protected void Page_Load(object sender, EventArgs e)
{
    string theDate = Request.QueryString["TheDate"];

    DS.SelectParameters.Add("@theDate", theDate);
    DS.SelectParameters["theDate"].DefaultValue = theDate.ToString();
}
kev670
  • 810
  • 2
  • 18
  • 37

2 Answers2

0

Can you not use the QueryStringParmater of the SelectParameters collection in the aspx declaration of the SqlDataSource as documented here?

mreyeros
  • 4,359
  • 20
  • 24
0

This is one of the best samples I have used, check Assigning Parameter Values Programmatically in the page.

http://www.asp.net/web-forms/tutorials/data-access/accessing-the-database-directly-from-an-aspnet-page/using-parameterized-queries-with-the-sqldatasource-cs

Ravi Vanapalli
  • 9,805
  • 3
  • 33
  • 43