Monday, March 12, 2012

problem modifying sqldatasource for dropdownlist control

i use asp.net 2.0 and c#

initially the dropdownlist control is bound to the queryA ("select * from tableA".) for sqldatasource1

now in a button click event, i want to change the query to queryB ("select * from tableA where id = @.id".)

in the code below, i can change the SELECT query of sqldatasource1 from queryA to queryB, but how can i give value to the parameter? please help

protected

void Button1_Click(object sender,EventArgs e)

{

sqldatasource1.SelectCommand = queryB

}

You almost had it, you just needed to add a parameter to the SelectParameters collection like so:

ASPX

<asp:dropdownlist id="ddlProducts" runat="server" datasourceid="sdsProducts" datatextfield="ProductName"datavaluefield="ProductID" /><asp:sqldatasource id="sdsProducts" runat="server" connectionstring="<%$ ConnectionStrings:NorthwindConnectionString%>"selectcommand="SELECT Products.* FROM Products" /><br /><br /><asp:button id="btnSubmit" runat="server" onclick="btnSubmit_Click" text="Load DropDownList with Seafood products only" />

CODE-BEHIND

protected void btnSubmit_Click(object sender, EventArgs e){sdsProducts.SelectCommand ="SELECT * FROM Products WHERE CategoryID = @.CategoryID";sdsProducts.SelectParameters.Add("CategoryID","8");}

|||hi ecbruk, there is an error saying parameter already exist, please chose a different name ;(|||Are you already setting forth your parameter declaratively within you SqlDataSource?

No comments:

Post a Comment