FabrikamDataContext context = new FabrikamDataContext("user idi
=username;password=password");
IEnumerable
result =
from prod in context.ProductTypes
orderby prod.Name
select prod;
DataTable dt = new DataTable("ProductType");
DataColumn dc;
DataRow dr;
dc = new DataColumn();
320
Appendix A: Case Study
dc.DataType = System.Type.GetType("System.Int32");
dc.ColumnName = "ID";
dt.Columns.Add(dc);
dc = new DataColumn();
dc.DataType = System.Type.GetType("System.String");
dc.ColumnName = "Name";
dt.Columns.Add(dc);
foreach (ProductType product in result)
{
dr = dt.NewRow();
dr["ID"] = product.ProductTypeID;
dr["Name"] = product.Name;
dt.Rows.Add(dr);
}
cboProductType.DataSource = dt;
cboProductType.DisplayMember = "Name";
cboProductType.ValueMember = "ID";
Next, add the following code to the SelectedIndexChanged event of the cboProductType combo.
If (cboProductType.SelectedIndex > 0)
{
int ProductID = Convert.ToInt32(cboProductType.SelectedIndex);
DataSet ds = new DataSet();
string connectionInfo = "Data Source=avalonserver;Initial Catalog=Fabrikam;
user id=username;pwd=password";
SqlDataAdapter da = new SqlDataAdapter("select ProductID, Name,
Description, ManufacturerSKU, Cost, Price, QuantityOnHand FROM products
WHERE ProductTypeID = @ID", connectionInfo);
da.
Pages:
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499