Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Typed DataSets

Typed DataSets

Scheduled Pinned Locked Moved ASP.NET
tutorial
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pssuresh
    wrote on last edited by
    #1

    Could any of you explain when i should exactly use typed Dataset. I know how to use it, but i have not implemented in my project. When i should go for typed dataset.Thanks for your precious time.

    P 1 Reply Last reply
    0
    • P pssuresh

      Could any of you explain when i should exactly use typed Dataset. I know how to use it, but i have not implemented in my project. When i should go for typed dataset.Thanks for your precious time.

      P Offline
      P Offline
      Parwej Ahamad
      wrote on last edited by
      #2

      DataColumns for our DataTables to specify what type of data could be stored in each column. This enforces runtime type-safety, but on most occasions we would like to know that our DataSets are type-safe when we write the code. Typed DataSets generate classes that expose each object in a DataSet in a type-safe manner. Using a DataSet // Create a DataAdapter for each of the tables we're filling SqlDataAdapter daCustomers = new SqlDataAdapter("SELECT * FROM CUSTOMER;", conn); // Create the Invoice DataAdapter SqlDataAdapter daInvoices = new SqlDataAdapter("SELECT * FROM INVOICE", conn); // Create your blank DataSet DataSet dataSet = new DataSet(); // Fill the DataSet with each DataAdapter daCustomers.Fill(dataSet, "Customers"); daInvoices.Fill(dataSet, "Invoices"); // Show the customer name Console.WriteLine(dataSet.Tables["Customers"]. Rows[0]["FirstName"].ToString()); Console.WriteLine(dataSet.Tables["Customers"]. Rows[0]["LastName"].ToString()); Console.WriteLine(dataSet.Tables["Customers"]. Rows[0]["HomePhone"].ToString()); // Change an invoice number with a string // this shouldn't work because InvoiceNumber // expects an integer dataSet.Tables["Invoices"]. Rows[0]["InvoiceNumber"] = "15234"; We need to use indexers with the DataSet to get each piece of the hierarchy until we finally get down to the row level. At any point you can misspell any name and get an error when this code is executed. In addition, the last line of the example shows us attempting to set the invoice number using a string. The DataSet knows that this column can only hold integers so we will get a runtime error enforcing that rule. Using a Typed DataSet There are a few things to notice in this example. First, we create our Typed DataSet much like we created a DataSet in the first example—the difference is that the schema already exists in our Typed DataSet. Second, even though this is a Typed DataSet, the CustomerTDS class directly derives from the DataSet class. Therefore, when we call the DataAdapters to fill our DataSet, it will accept our Typed DataSet. In fact, the Typed DataSet is a DataSet . . . a specialized DataSet. Next, you should notice that the syntax to get at tables and fields is much more straightforward with Typed DataSets. Each DataTable is now referenced with a property of CustomerTDS. Likewise, each field is a property of a row. Not only is this syntax more straight

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups