Tuesday 27 November 2012

How to use stored procedure in SqlDataSourceID in GridView in Asp.Net .


In this post  I am describing ‘ how to use stored procedure in SqlDataSourceID in Asp.Net’. It is very simple. Please follow the step which is given below.
Step1: Open Visual studio
·         Create new website
·         Give it name
·         Click ok.
Step2:- You have to add new page
·         Right click on project solution
·         Select Add new items
·         Give it a name
·         Click ok
Step3: Right a stored procedure in sqlServer
Note: One column must be primary key in the table. Procedure given below  in which EmployeeID is primary key in the table.

CREATE PROCEDURE [dbo].[sp_employee]
(
@EmployeeID int
)
AS
BEGIN
     
      select * from Employee where EmployeeID=@EmployeeID;
END


Step4: Drag and drop a GridView on the page

Step5: Click on gridView  -> choose data source .

Choose stored procedure instead of  table . See the figure.



After that choose the stored procedure instead of sql table.

Select your stored procedure .

Give the Default value (means in your table there must be one column is primary key )

Click  next and then click finish button.

Step6: Design page source code

Default.aspx:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            DataKeyNames="EmployeeID" DataSourceID="SqlDataSource1">
            <Columns>
                <asp:BoundField DataField="EmployeeID" HeaderText="EmployeeID"
                    InsertVisible="False" ReadOnly="True" SortExpression="EmployeeID" />
                <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
                <asp:BoundField DataField="BirthDate" HeaderText="BirthDate"
                    SortExpression="BirthDate" />
                <asp:BoundField DataField="MaritalStatus" HeaderText="MaritalStatus"
                    SortExpression="MaritalStatus" />
                <asp:BoundField DataField="Gender" HeaderText="Gender"
                    SortExpression="Gender" />
                <asp:BoundField DataField="HireDate" HeaderText="HireDate"
                    SortExpression="HireDate" />
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString="<%$ ConnectionStrings:modiConnectionString %>"
            SelectCommand="sp_employee" SelectCommandType="StoredProcedure">
            <SelectParameters>
                <asp:Parameter DefaultValue="1" Name="EmployeeID" Type="Int32" />
            </SelectParameters>
        </asp:SqlDataSource>
    </div>
    </form>
</body>
</html>

Debug the code and run.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.