A C# Example of Displaying the Products Table

The following example shows the output of the Products table using data binding. In this example we are using C# code.


The Products Table

ProductIDCategoryIDProductNameProductDescriptionUnitPriceImagePathServingSizeServingsQuantityMinOnHandMaxOnHandManufacturer
10011Chocolate City MilkChocolate City Milk tastes REALLY good!2.01/quickstart/aspplus/images/milk5.gif8 fl oz (240 mL)8000Chocolate City
10021Bessie Brand 2% MilkBessie Brand 2% Milk is tasty!1.19/quickstart/aspplus/images/milk1.gif8 fl oz (240 mL)8000Milk Factory
10031Funny Farms MilkFunny Farms Whole Milk Description1.29/quickstart/aspplus/images/milk4.gif8 fl oz (240 mL)10000Funny Farms
20012Fruity PopsFruity Pops Description4.07/quickstart/aspplus/images/cereal7.gif3/4 cup (30 g)17000River Mills
20022U.F.O.s CerealU.F.O.s Cereal Description3.34/quickstart/aspplus/images/cereal3.gif1 cup (30 g)10000Acme Harvesters
20032Healthy GrainsHealthy Grains Cereal Description3.78/quickstart/aspplus/images/cereal1.gif3/4 cup (30 g)17000All Natural Co.
20042Super Sugar StrikeSuper Sugar Strike Description4.17/quickstart/aspplus/images/cereal6.gif3/4 cup (30 g)17000Capitol Cereals
30013Purple RainBrown Barrel Root Beer Description1.1/quickstart/aspplus/images/soda5.gif4 fl oz (120 mL)8000BrainFade, Inc.
30023Extreme OrangeBargain Cola Description0.89/quickstart/aspplus/images/soda6.gif6 fl oz (180 mL)6000SuperX Beverages
30033Kona Diet ColaSuper Red Pop Soda Description1.1/quickstart/aspplus/images/soda7.gif4 fl oz (120 mL)10000Kona Kola Co.
30043Fizzy Fizzing DrinkLemon Lime Quencher Description1.05/quickstart/aspplus/images/soda8.gif6 fl oz (180 mL)5000Sparkle Co.
10051Marigold Whole MilkMarigold Whole Milk Description1.39/quickstart/aspplus/images/milk6.gif8 fl oz (240 mL)8000Marigold Meadows


Source Code

<% @Import Namespace="System.Data" %>
<% @Import Namespace="System.Data.OleDb" %>
<script language="c#" runat="server">

  void Page_Load(Object sender, EventArgs e)
  {
    // 1. Create a connection
    const string strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
                             "Data Source=C:\\Data\\GrocerToGo.mdb"
    
    OleDbConnection objConn = new OleDbConnection(strConnString);
    
    // You must open the db connection before populating the DataReader
    objConn.Open();	

    // 2. Create a command object for the query
    const string strSQL = "SELECT * FROM Products";
    OleDbCommand objCmd = new OleDbCommand(strSQL, objConn);


    // 3. Create/Populate the DataReader
    OleDbDataReader objDR = objCmd.ExecuteReader(CommandBehavior.CloseConnection);

    // Do data binding
    dgProducts.DataSource = objDR;
    dgProducts.DataBind();
  }
</script>

<html>
<body>

  <h1>The <code>Products</code> Table</h1>
  <p>
  <asp:datagrid runat="server" id="dgProducts" ForeColor="White" BackColor="Navy"
          Font-Name="Verdana" BorderWidth="0" CellPadding="4"
          HeaderStyle-Font-Bold="True"
          HeaderStyle-Font-Size="Small"
          Font-Size="Smaller"
          HeaderStyle-BackColor="Gray"
	/>
</body>
</html>


  • Return to the article