Changing the Parameter Source

Changing the Parameter Source

In ASP.NET, you can change the source of a parameter used in a SqlDataSource control by specifying the ControlID property of the parameter.

Here are the steps to change the source of a parameter in a SqlDataSource control:

Define a parameter in your markup, for example:

<asp:SqlDataSource ID=”SqlDataSource1″ runat=”server” ConnectionString=”<%$ ConnectionStrings:NorthwindConnectionString %>”

     SelectCommand=”SELECT * FROM Orders WHERE ShipCity = @City”>

     <SelectParameters>

         <asp:Parameter Name=”City” Type=”String” DefaultValue=”London” />

     </SelectParameters>

</asp:SqlDataSource>

Add a control to your markup that will be the source of the parameter. For example, a TextBox control:

<asp:TextBox ID=”TextBox1″ runat=”server”></asp:TextBox>

Change the ControlID property of the parameter to the ID of the control you want to use as the parameter source:

<asp:SqlDataSource ID=”SqlDataSource1″ runat=”server” ConnectionString=”<%$ ConnectionStrings:NorthwindConnectionString %>”

     SelectCommand=”SELECT * FROM Orders WHERE ShipCity = @City”>

     <SelectParameters>

         <asp:ControlParameter Name=”City” Type=”String” ControlID=”TextBox1″ PropertyName=”Text” DefaultValue=”London” />

     </SelectParameters>

</asp:SqlDataSource>

In this example, the ControlID property of the parameter is set to “TextBox1”, which is the ID of the TextBox control. The PropertyName property of the parameter is set to “Text”, which specifies that the value of the Text property of the TextBox control should be used as the value of the parameter. Now, when the SqlDataSource control is executed, it will use the value of the TextBox control as the value of the “City” parameter in the query. You can change the value of the TextBox control at runtime, and the SqlDataSource control will use the new value as the value of the parameter.

Apply for ASP.NET Certification Now!!

https://www.vskills.in/certification/certified-aspnet-programmer

Back to Tutorial

Risk and Return
Database connection in PHP

Get industry recognized certification – Contact us

keyboard_arrow_up
Open chat
Need help?
Hello 👋
Can we help you?