CSharp Programmatically Creating Dataset Object – ADO.Net Database

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %><script runat="server">
  void Page_Load(object sender, EventArgs e)
  {
      DataSet customerOrders = new DataSet("CustomerOrders");
      DataTable customers = customerOrders.Tables.Add("Customers");
      DataTable orders = customerOrders.Tables.Add("Orders");
 
      customers.Columns.Add("CustomerID", Type.GetType("System.Int32"));
      customers.Columns.Add("FirstName", Type.GetType("System.String"));
      customers.Columns.Add("LastName", Type.GetType("System.String"));
      customers.Columns.Add("Phone", Type.GetType("System.String"));
      customers.Columns.Add("Email", Type.GetType("System.String"));
      orders.Columns.Add("CustomerID", Type.GetType("System.Int32"));
      orders.Columns.Add("OrderID", Type.GetType("System.Int32"));
      orders.Columns.Add("OrderAmount", Type.GetType("System.Double"));
      orders.Columns.Add("OrderDate", Type.GetType("System.DateTime"));
      customerOrders.Relations.Add("Cust_Order_Rel", customerOrders.Tables["Customers"].Columns["CustomerID"], customerOrders.Tables["Orders"].Columns["CustomerID"]);
      DataRow row = customers.NewRow();
      row["CustomerID"] = 1;
      row["FirstName"] = "first";
      row["LastName"] = "last";
      row["Phone"] = "111-1111";
      row["Email"] = "test@test.com";
      customers.Rows.Add(row);
      row = orders.NewRow();
      row["CustomerID"] = 1;
      row["OrderID"] = 22;
      row["OrderAmount"] = 0;
      row["OrderDate"] = "11/10/2007";
      orders.Rows.Add(row);
      Response.Write(Server.HtmlEncode(customerOrders.GetXml()));
      gridResults.DataSource = customerOrders.Tables["Customers"];
      gridResults.DataBind();
  }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
  <title>Programmatically creating a DataSet object</title>
</head>
<body>
  <form id="form1" runat="server">
    <div>
        <asp:GridView runat="Server" ID="gridResults" />
    </div>
  </form>
</body>
</html>

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.