Articles → ADO.NET → How To Call Stored Procedure Using Sqldatasource In Asp.Net
How To Call Stored Procedure Using Sqldatasource In Asp.Net
Software Requirements
- Visual studio 2005 (or above) is installed on your machine
- SQL server 2005 (or above) is installed on your machine.
Technical Knowledge
- How to create a table in SQL server?
- How to create a stored procedure in SQL server?
- How to create a web based application using Visual Studio?
- Basics of asp.net controls like button, textbox, gridview etc.
- What is the purpose of SQLDataSource in asp.net?
Scenario
Create A New Table
Click to Enlarge
Click to Enlarge
Create A New Stored Procedure
Create Proc GetMyTask
@TaskId int
As
Select TaskId,TaskName from MyTask where TaskId = @TaskId
Create A Web Application And Add Controls In Web Form
<%--Controls--%>
<asp:TextBox ID="txtTaskId" runat="server"></asp:TextBox>
<asp:Button ID="btnTest" runat="server" Text="Click" />
<%--GridView--%>
<asp:GridView ID="gvTask" runat="server">
</asp:GridView>
Add Sqldatasource In The Web Form
<asp:SqlDataSource ID="source" runat="server" ConnectionString=""
SelectCommand="GetMyTask" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:ControlParameter ControlID="txtTaskId" PropertyName="Text" Name="TaskId" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<%--Controls--%>
<asp:TextBox ID="txtTaskId" runat="server"></asp:TextBox>
<asp:Button ID="btnTest" runat="server" Text="Click" />
<%--GridView--%>
<asp:GridView ID="gvTask" runat="server" DataSourceID="source"></asp:GridView>
- ID – unique id of SQLDataSource
- ConnectionString – Connection string of database from where we are calling stored procedure
- SelectCommand – Name of the stored procedure
- ControlID – Name of the control from where we get the value of input parameter task id
- PropertyName – Property of control that gives the value.
- Name – Name of the input parameter of stored procedure
- Type – Data type of input parameter
Output
Click to Enlarge