Reputation: 23501
I need to fill in the "SelectParameters" dynamically , but all I found on Google , was using fixed parameters.
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="XX"
OnSelecting="SqlDataSource2_Selecting" SelectCommand="">
<SelectParameters>
</SelectParameters>
</asp:SqlDataSource>
Is there any solutions ?
I also tried to use the "OnSelecting" event , but I can't assign value to parameters that are not present.
protected void SqlDataSource2_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
string[] queryFields = new string[] { ... }; // some dynamic field depends on different table
foreach (string s in queryFields)
{
e.Command.Parameters["@" + s].Value = "filter";
}
}
Upvotes: 1
Views: 4213
Reputation: 9429
If you're ok with setting the connection string when initializing the control you can do so on the control's init event like this:
protected void SqlDataSource1_Init(object sender, EventArgs e)
{
SqlDataSource1.ConnectionString = <whatever code you want here>;
SqlDataSource1.ProviderName = <whatever code you want here>;
}
This is still dynamic in the sense that you determine the connection properties at runtime, not design time. This helped me avoid having to resort to web.xml transformations and switch between QA and PROD connection strings based on an environment variable specifying QA/PROD environment.
Upvotes: -1
Reputation: 31239
You can in code behind set the variables like this:
SqlDataSource1.SelectParameters.Add("parameterName", "parameterValue");
But be aware that you need the clear the parameters before adding them. Because otherwise you will end up with duplicates of parameters.
Upvotes: 2