How to specify parameter value for stored procedure in SqlDataSource
Asked Answered
F

3

5

Being new to using the declarative syntax of SqlDataSource I am trying to figure out a way to set the value of a parameter to a stored procedure. I have a Client_ID that is passed via the Request Object and I need to set the Client_ID before the stored procedure of the SqlDataSource is executed.

I have a couple of questions.

  1. Does the stored procedure parameter have to be predefined in the ASPX markup or can it be added dynamically in the code-behind?

  2. Would anyone have an example that demonstrates the SqlDataSource markup with a stored procedure and parameter as well as setting that parameter value in code-behind?

Fattish answered 16/3, 2011 at 13:37 Comment(0)
M
9

With .net 4 framework you can ...

1.It can be added dynamically, but you would have to provide your own code expression builder (for further info see here) 1.1 You can also use different parameter to achieve same goal, like :

enter image description here

2.

  <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
  ConnectionString="<%$ ConnectionStrings:yourConnectionString %>" 
  SelectCommand="YourStoreProcedure" SelectCommandType="StoredProcedure">
  <SelectParameters>
     <asp:ControlParameter Name="yourParameterName" ControlID="controlThatHoldsParameterValue" PropertyName="Text" />
  </SelectParameters>

Mandymandych answered 6/4, 2011 at 20:12 Comment(0)
T
3

This is a fairly thorough example of using parameterized queries (Stored Procedure and Text) with SqlDataSource, including programmatically setting parameters. ASP.NET - Using Parameterized Queries with the SqlDataSource.

Tiga answered 18/3, 2011 at 13:43 Comment(1)
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes.Radiolarian
K
0
 <asp:SqlDataSource ID="ADSqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:ADConnection %>"
    SelectCommand="GetProfile" SelectCommandType="StoredProcedure">
    <SelectParameters>
        <asp:ControlParameter ControlID="InputTextBox" Name="Host" PropertyName="Text" Type="String" />
    </SelectParameters>
</asp:SqlDataSource>

GetProfile is the stored proc name and Host is parameter name, which is retreived from a texbox called InputTextBox

Knuth answered 28/1, 2013 at 19:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.