Adding Existing Typed Datasets to a Form or Component
You will frequently need to add a typed dataset to a form or component so that you can manipulate data, bind controls to the dataset, and so on. When you add a dataset, you are actually creating an instance of the typed dataset class on your form or component. The typed dataset class must already exist and can come from several sources:
- You created it by generating it in another form or component in your project. For more information, see Creating Typed Datasets with the Component Designer.
- You created it manually in the XML Designer. For more information, see Creating XML Schemas and Datasets.
- You created a reference to an XML Web service or other component that returns a dataset. For more information, see Accessing an XML Web Service in Managed Code.
You can add an untyped dataset to a form or component as well, but you create it in the form or component rather than adding an instance of a typed dataset class. For more information, see Adding Untyped Datasets to a Form or Component.
To add an existing dataset to a form or component
- Open the form or component you want to work with. If necessary, switch to Design view of the designer.
- From the Data tab of the Toolbox, drag a DataSet object onto the designer.
The Choose a Dataset dialog box appears.
- Select Typed Dataset, and then from the drop-down list, select the dataset you want to use, and then click OK.
The drop-down list is populated with a list of all typed dataset classes in your project.Tip If you have added an XML Web service or component but cannot see the dataset's schema in Solution Explorer, open up the node for the reference. If you still do not see the schema, double-check that the XML Web service or component is returning a typed dataset.
An instance of the typed dataset class is added to the form or component.
- Add code to the form or component to fill the dataset.
If the dataset represents data from a database, you generally call the Fill method of a data adapter to execute an SQL statement or stored procedure that fetches the data for the dataset. For more information, see Populating a DataSet from a DataAdapter. The following example shows how you might fill the table Customers in the dataset dsCustomersOrders (whose instance is called dsCustomersOrders1):
' Visual Basic oleDbDataAdapter1.Fill( dsCustomersOrders1, "Customers" ) // C# oleDbDataAdapter1.Fill( dsCustomersOrders1, "Customers");
If the dataset represents information returned by an XML Web service or a component, you need to load the dataset by invoking the method that returns data. You then typically need to merge the data returned by the XML Web service or component into the dataset on your form. The following example shows how to create an instance of an XML Web service, invoke its method called GetCustomersDataset, and load it into a local dataset called dsCustomers1:
' Visual Basic Dim ws1 As New MyServer.CService1() dsCustomers1.Merge(ws1.GetCustomersDataset()) // C# MyServer.CService1 ws1 = new MyServer.CService1(); dsCustomers1.Merge(ws1.GetCustomersDataset());
You can also fill the dataset by creating data programmatically. For more information, see Adding Data to a Table.
- If your application requires that you write information from the dataset to a database, add code to the form or component to do this.
If the dataset is filled using a data adapter, you can call the data adapter's Update method to write changed data back the database. For more information, see Updating the Database with a DataAdapter and the DataSet.