Friday, March 9, 2012

Dynamically change the SelectCommand property

Hello

I have a gridview that I use on a products page, my problems is certain products have different attributes that I would like to display.

Therefore what I would like to do is change the SelectCommand property to my SQLDatasource depending on the querystring that is passed.

For instance in my page load event I would have a CASE statement with numerous SQLString Variables.

Here is the current coding for my datasource

<asp:SqlDataSourceID="SqlDataSource2"runat="server"ConnectionString="<%$ ConnectionStrings:ConnectionString %>"

SelectCommand="SELECT PS.ProductSizeMM AS [Coupling Size], PS.ProductWallThickness AS [To Suit], PS.Cost AS [Price], PS.Sold_By AS [Sold by] FROM tblProduct AS P INNER JOIN tblProductSize AS PS ON P.ProductCode = PS.ProductCode WHERE (P.ProductDescription = @.ProductDescription) ORDER BY PS.Sorter">

<SelectParameters>

<asp:QueryStringParameterName="ProductDescription"QueryStringField="ProductDescription"/>

</SelectParameters>

</asp:SqlDataSource>

I have tried declaring a string variable in my page load event (SQLString) then setting the

SelectCommand="SQLString" but this causes a syntax error

Exception Details:System.Data.SqlClient.SqlException: Incorrect syntax near 'SQLString'.

Any help would be greatly appreicated!!

Hi,

a cleaner way to use would be by doing it in the SQLDataSource's Selecting event:http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource.selecting.aspx.

Grz, Kris.

|||

You can't do that declaratively within your SqlDataSource. Instead, you'll need to set it within the code-behind like so:

SqlDataSource2.SelectCommand = SQLString

|||

Thanks it worked a treatBig Smile

No comments:

Post a Comment