Execute stored procedure with parameters

(ASP.NET)


Hi,
In this article i discuss about how to execute stored procedure with parameters using ado.net.

.aspx

<div>
     <asp:Button ID="btn_exstoredprocwithparameters" runat="server" OnClick="btn_exstoredprocwithparameters_Click" Text="Execute stored procedure with parameters" />
        <br />
        <br />
        <asp:GridView ID="grdvw_products" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">
            <AlternatingRowStyle BackColor="White" />
            <EditRowStyle BackColor="#2461BF" />
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#EFF3FB" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#F5F7FB" />
            <SortedAscendingHeaderStyle BackColor="#6D95E1" />
            <SortedDescendingCellStyle BackColor="#E9EBEF" />
            <SortedDescendingHeaderStyle BackColor="#4870BE" />
        </asp:GridView>
</div>


Code behind:-

      //execute stored procedure with parameters
        private void executestoredprocedurewithParameters()
        {
            try
            {
                using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["adodotnetConstring"].ToString()))
                {
                    using (SqlCommand cmd = new SqlCommand("CustOrderHist", con))
                    {
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.Add("@CustomerID", SqlDbType.VarChar);
                        cmd.Parameters["@CustomerID"].Value = "ALFKI";
                        SqlDataReader rdr;
                        con.Open();
                        rdr = cmd.ExecuteReader();
                        grdvw_products.DataSource = rdr;
                        grdvw_products.DataBind();
                        rdr.Close();
                        con.Close();
                    }
                }
            }
            catch (Exception)
            { }
        }

 protected void btn_exstoredprocwithparameters_Click(object sender, EventArgs e)
        {
            try
            {
                executestoredprocedurewithParameters();
            }
            catch (Exception)
            { }
        }
  

Image

Ratings


Average Rating: 5.00 by 1 users
Giri Prasad
7/21/2013
7/21/2013
Download PDF

Comments