Wednesday, March 7, 2012

define Select parameters

Hi

I have a DropDownlist (Drop1) and a GridView,the GridView is bount to an SqlDataSource1 that has 2 Select parameters CatId and SourceId

The dropdownlist has a selectedvalue of the following format 15-10(2 numbers seperated by -).I want to set CatId to 15 and SourceId to 10

<

asp:SqlDataSourceID="SqlDataSource1"runat="server"ConnectionString="<%$ ConnectionStrings:Art %>"SelectCommand="Select * from Option WhereSourceId=@.SourceId AndCatId=@.CatId"><SelectParameters><asp:ControlParameterControlID="Drop1"Name="SourceId"/><asp:ControlParameterControlID="Drop1"Name="CatId"/></SelectParameters></asp:SqlDataSource>

Can anyone help me to define the parameters?

thanks

Hi engnouna,

We can bind the select parameters to Label controls' Text property. And set the Text every time DropDownList select index changed. Here is the demo code:

<asp:GridViewID="GridView1"runat="server"DataSourceID="SqlDataSource1">

</asp:GridView>

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

SelectCommand="SELECT [SourceID], [CatID], [Name] FROM [ForDynamicGridView] WHERE (([SourceID] = @.SourceID) AND ([CatID] = @.CatID))">

<SelectParameters>

<asp:ControlParameterControlID="SourceID"DefaultValue="1"Name="SourceID"PropertyName="Text"

Type="Int32"/>

<asp:ControlParameterControlID="CatID"DefaultValue="2"Name="CatID"PropertyName="Text"

Type="Int32"/>

</SelectParameters>

</asp:SqlDataSource>

</div>

<asp:DropDownListID="DropDownList1"runat="server"AutoPostBack="True"OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">

<asp:ListItem>1-2</asp:ListItem>

<asp:ListItem>2-4</asp:ListItem>

<asp:ListItem>3-4</asp:ListItem>

</asp:DropDownList>

<asp:LabelID="SourceID"runat="server"Text="1"Visible="false"></asp:Label>

<asp:LabelID="CatID"runat="server"Text="2"Visible="false"></asp:Label>

protectedvoid DropDownList1_SelectedIndexChanged(object sender,EventArgs e)

{

string[] value = DropDownList1.SelectedValue.Split('-');

SourceID.Text = value[0];

CatID.Text = value[1];

}

|||

Hi,

The ControlParameter itself cannot parse the text in your dropdownlist directly. So, I suggest you parse it in your select statement.

For example, if they are all 2 digits numbers, you can use

Select * from Option Where SourceId=LEFT(@.Input,2) And CatId=RIGHT(@.Input,2)

HTH. If this does not answer your question, please feel free to mark the post as Not Answered and reply. Thank you!

No comments:

Post a Comment