Programmatically creating a DataSet object : DataSet « ADO.net Database « ASP.Net






Programmatically creating a DataSet object

<%@ 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>

 








Related examples in the same category

1.Loop through data in Sql Server by DataSet
2.Loop through DataSet
3.Get query result from DataSet
4.Load Table from DataSet
5.Build a DataSet
6.Create DataSet pragmatically
7.Build a DataSet with relationship
8.Output the content of a DataSet as XML
9.Finding a Particular Row in a DataSet
10.Converting XML to DataSet and Vice versa
11.Converting XML to DataSet and Vice versa (VB)
12.DataSet Serialization and Deserialization using Binary Format