Monday, September 19, 2011

Monday 9.19.11

using System;
using System.Data;
using System.Data.SqlClient;

namespace BuildDataSetProgrammatically
{
class Program
{
static void Main(string[] args)
{
DataSet ds = new DataSet("SalesOrders");

//build the SaleOrderHeader(Parent) table
DataTable dtHeader = new DataTable("SalesOrderHeader");

//get the collection of columns from the parent table
DataColumnCollection cols = dtHeader.Columns;

//add the identity field
DataColumn col = cols.Add("SalesOrderID", typeof(System.Int32));
col.AutoIncrement = true;
col.AutoIncrementSeed = -1;
col.AutoIncrementStep = -1;
//add other fields
cols.Add("OrderDate", typeof(System.DateTime)).AllowDBNull = false;
cols.Add("SalesOrderNumber", typeof(System.String)).MaxLength = 25;
cols.Add("TotalDue", typeof(System.Decimal));
//set the primary key
dtHeader.PrimaryKey = new DataColumn[] {cols["SalesOrderID"]};
//add the salesorderheader table to the dataset
ds.Tables.Add(dtHeader);

//-----------------------------------------
//build the order details (child) table
DataTable dtDetail = new DataTable("SalesOrderDetail");

cols = dtDetail.Columns;
//add the pk fields
cols.Add("SalesOrderID", typeof(System.Int32)).AllowDBNull = false;

col = cols.Add("SalesOrderDetailID", typeof(System.Int32));
col.AutoIncrement = true;
col.AutoIncrementSeed = -1;
col.AutoIncrementStep = -1;
//add the other fields
cols.Add("UnitPrice", typeof(System.Decimal)).AllowDBNull = false;
cols.Add("OrderQty", typeof(System.Int16)).AllowDBNull = false;
cols.Add("LineTotal", typeof(System.Decimal));
//set the primary key
dtDetail.PrimaryKey = new DataColumn[] {cols["OrderID"], cols["ProductID"]};
//add the order details table to the dataset
ds.Tables.Add(dtDetail);

//----------------
//add the relation between header and detail tables
ds.Relations.Add("SalesOrderHeader_SalesOrderDetail", dtHeader.Columns["SalesOrderID"], dtDetail.Columns["SalesOrderID"]);

//fill the dataset
string sqlConnectString = @"Data Source=Alfred-PC\SQLExpress;" +
"Integrated Security=SSPI;Initial Catalog=AdventureWorks;";

string sqlSelect = @"SELECT SalesOrderID, OrderDate, SalesOrderNumber, TotalDue
FROM Sales.SalesOrderHeader
SELECT SalesOrderID, SalesOrderDetailID, UnitPrice, OrderQty, LineTotal
FROM Sales.SalesOrderDetail;";

SqlDataAdapter da = new SqlDataAdapter(sqlSelect, sqlConnectString);
da.TableMappings.Add("Table", "SalesOrderHeader");
da.TableMappings.Add("Table1", "SalesOrderDetail");
da.Fill(ds);

//output the first three orders with details for each
for (int i = 0; i < 3; i++)
{
DataRow row = ds.Tables["SalesOrderHeader"].Rows[i];
Console.WriteLine("{0}\t{1}\t{2}\t{3}", row[0], row[1], row[2], row[3]);

foreach(DataRow rowChild in row.GetChildRows("SalesOrderHeader_SalesOrderDetail"))
{
Console.WriteLine("\t{0}\t{1}\t{2}\t{3}\t{4}", rowChild[0], rowChild[1], rowChild[2], rowChild[3], rowChild[4]);
}
}

Console.ReadLine();
}
}
}


protected void cblItems_Init(object sender, EventArgs e)
{
//create an array of items to add
string[] Genre = { "SciFi", "Fiction", "Computers", "History", "Religion" };
cblItems.DataSource = Genre;
cblItems.DataBind();
}

protected void cblItems_SelectedIndexChanged(object sender, EventArgs e)
{
if (cblItems.SelectedItem == null)
{
lblCategory.Text = "
No genres selected.";
}
else
{
StringBuilder sb = new StringBuilder();

foreach(ListItem li in cblItems.Items)
{
if(li.Selected)
{
sb.Append("
" + li.Value + " - " + li.Text);
}
}
lblCategory.Text = sb.ToString();
}
}


protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//build 2 dimensional array for the lists
//first dimension contains bookname
//2nd dimension contains ISBN number
string[,] books = {
{"Learning ASP.NET 2.0 with AJAX", "9780596513976"},
{"Beginning ASP.NET 2.0 with C#", "9780470042583"},
{"Programming C#", "9780596527433"},
{"Programming .NET 3.5", "978059652756X"},
{"Programming .NET Windows Applications", "0596003218"},
{"Programming ASP.NET 3e", "0596001711"},
{"WebClasses From Scratch", "0789721260"},
{"Teach yourself C++ in 21 Days", "067232072X"},
{"Teach Yourself C++ in 10 Minutes", "067231603X"},
{"XML & Java From Scratch", "0789724766"},
};

//now populate the list
for (int i = 0; i < books.GetLength(0); i++)
{
//add both text and value
ddlBooks.Items.Add(new ListItem(books[i, 0], books[i, 1]));
}
}
}

protected void ddlBooks_SelectedIndexChanged(object sender, EventArgs e)
{
//check to verify that something has been selected
if (ddlBooks.SelectedIndex != -1)
{
lblBookInfo.Text = ddlBooks.SelectedItem.Text + "---->IBSN: " + ddlBooks.SelectedValue;
}
}


public partial class BulletedListDemo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
//build 2 dimensional array for the lists
//first dimension contains bookname
//2nd dimension contains ISBN number
string[,] books = {
{"Learning ASP.NET 2.0 with AJAX", "9780596513976"},
{"Beginning ASP.NET 2.0 with C#", "9780470042583"},
{"Programming C#", "9780596527433"},
{"Programming .NET 3.5", "978059652756X"},
{"Programming .NET Windows Applications", "0596003218"},
{"Programming ASP.NET 3e", "0596001711"},
{"WebClasses From Scratch", "0789721260"},
{"Teach yourself C++ in 21 Days", "067232072X"},
{"Teach Yourself C++ in 10 Minutes", "067231603X"},
{"XML & Java From Scratch", "0789724766"},
};

//now populate the list
for (int i = 0; i < books.GetLength(0); i++)
{
//add both text value
bltBooks.Items.Add(new ListItem(books[i, 0], "http://www.amazon.com/gp/product/" + books[i, 1]));
}
}
}

protected void lbxSelectedIndexChanged(object sender, EventArgs e)
{
ListBox lb = (ListBox)sender;
string strID = lb.ID;
string strValue = lb.SelectedValue;

switch (strID)
{
case "lbxBulletStyle":
BulletStyle style = (BulletStyle)Enum.Parse(typeof(BulletStyle), strValue);
bltBooks.BulletStyle = style;
break;
case "lbxBulletNumber":
bltBooks.FirstBulletNumber = Convert.ToInt32(strValue);
break;
case "lbxDisplayMode":
BulletedListDisplayMode displayMode = (BulletedListDisplayMode)Enum.Parse(typeof(BulletedListDisplayMode), strValue);
bltBooks.DisplayMode = displayMode;
break;

default: break;
}
}


protected void bltBooks_Click(object sender, BulletedListEventArgs e)
{
BulletedList b = (BulletedList)sender;
tdMessage.InnerHtml = "Selected index: " + e.Index.ToString() + "
" + "Selected value: " +
b.Items[e.Index].Value + "
";
}
}

No comments: