File: App_Code\DataLayer1.cs
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Collections.Generic;
public class DataLayer1
{
private static readonly string _connectionString;
public class ProductCategory
{
private int _id;
private string _name; public int Id
{
get { return _id; }
set { _id = value; }
}
public string Name
{
get { return _name; }
set { _name = value; }
}
}
public class Product
{
private string _title;
private int _categoryId;
public string Title
{
get { return _title; }
set { _title = value; }
}
public int CategoryId
{
get { return _categoryId; }
set { _categoryId = value; }
}
}
public static void GetProductData(List productCategories, List products)
{
string commandText = "SELECT Id,Name FROM ProductCategories;SELECT Title, CategoryId FROM Products";
SqlConnection con = new SqlConnection(_connectionString);
SqlCommand cmd = new SqlCommand(commandText, con);
using (con)
{
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
DataLayer1.ProductCategory newCategory = new DataLayer1.ProductCategory();
newCategory.Id = (int)reader["Id"];
newCategory.Name = (string)reader["Name"];
productCategories.Add(newCategory);
}
reader.NextResult();
while (reader.Read())
{
DataLayer1.Product newProduct = new DataLayer1.Product();
newProduct.Title = (string)reader["Title"];
newProduct.CategoryId = (int)reader["CategoryID"];
products.Add(newProduct);
}
}
}
static DataLayer1()
{
_connectionString = WebConfigurationManager.ConnectionStrings["Products"].ConnectionString;
}
}
File: Web.config
connectionString="Data Source=.\SQLEXPRESS;
AttachDbFilename=|DataDirectory|MyDatabase.mdf;Integrated Security=True;User Instance=True" />
File: ShowDataLayer1.aspx
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Collections.Generic" %>
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Show DataLayer1