Learn How to Use Stored Procedures with ASP.NET 4.0 and C#
We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect’s help, we were able to avoid any headaches!To begin, create a new ASP.NET Empty Web Site and:
- Right click the project in your solution explorer.
- Select add ASP.NET folder.
- Select the App_Data folder.
- Right click the App_Data folder.
- Select add existing item.
- Select NORTHWND.mdf.
- Open up your server explorer.
- Expand the stored procedures folder.
- Double click ‘CustOrderHist’.
ALTER PROCEDURE CustOrderHist @CustomerID nchar(5)
SELECT ProductName, Total=SUM(Quantity)
FROM Products P, [Order Details] OD, Orders O, Customers C
WHERE C.CustomerID = @CustomerID
AND C.CustomerID = O.CustomerID AND O.OrderID = OD.OrderID AND OD.ProductID = P.ProductID
GROUP BY ProductName
Next, we will want to add a new page to the project named ‘Default.aspx’. Add a grid view to the page leaving the default settings. Then in our code behind file, add the following code to the Page_Load event method:
SqlCommand cmd = new SqlCommand("CustOrderHist", new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\NORTHWND.mdf;Integrated Security=True;User Instance=True"));
cmd.CommandType = CommandType.StoredProcedure;
GridView1.DataSource = cmd.ExecuteReader();