ADO.NET

ADO.NET Projects

ADO.NET Project 1

ADO.NET Examples

Examples

adplus-dvertising
Complex Data Binding in Windows Forms
Previous Home Next

We will build Windows Form that will display all student in a bound ListBox and thestudents's associated marks in a bound DataGrid. A more complex DataSet will be needed that provides the relationship path to traverse from student to marks using the primary key of student (stdid) to foreign key in marks (stdid). we will have to do data bindings for both the ListBox control and the DataGrid con

Create a New Project in VS .NET

  1. Create a new project in VS .NET by choosing File, New, and then choosing the Project option.
  2. When the New Project dialog box appears, choose Visual C# Projects and Windows Applications. Name this project "comlexdatabinding". This creates a default form for you to start from.

Add the Data Connection and Two Data Adapters

we will need to access both the student table and themarks table, so two data adapters will be created, each populating different controls but using one database connection. From the Data tab of the Toolbox, drag aOledbDataAdapter object into your form. This will automatically invoke the Data Adapter Configuration Wizard. Both the data connection and the data adapter can be fully configured here.

  • a. The wizard starts with the Choose our Data Connection dialog box. If there already exist a connection defined in your project,then it will be placed in the dialog box; otherwise, choose to create a new connection and specify the appropriate connection information.
  • b. Choose the Use SQL Statements option.
  • c. we will be presented with a Generate the SQL Statements dialog box where you will simply type in a valid SQL statement, or you can use the Query Builder option to formulate the SQL query.such as:
    SELECT stdid,stdname FROM student
    
  • d. Finally, the wizard will show you the tasks that it has done and indicate whether the oledbDataAdapter has been configured successfully.
  1. we need one more data adapter for access to the mrks table. Drag another oledbDataAdapter object onto the form.
  • a. Again, the wizard starts with the Choose Your Data Connection dialog box.
  • b. Choose the Use SQL Statements option.
  • c. we will be presented with a Generate the SQL Statements dialog box where you will type the following query:
    SELECT subid, stdid, subname,mark
    FROM marks
    
  • d. Then the wizard will show we the tasks that it has done and indicate whether the oledbDataAdapter has been configured successfully .

Generate a DataSet

Now that the OledbDataAdapter and DataConnection objects have been configured and added to the form, you must generate a DataSet and then add an instance of this DataSet to the form.

  1. From the Data menu in Visual Studio, simply choose the Generate Dataset option.

  2. XML Schema editor—STUDENTDS.xsd schema file.

  3. Next, drag a Relation object onto the Orders table

  4. Adding the ListBox and DataGrid Controls
  5. create the ListBox to display all of the Sudent Names from the Student table.

    • a. Drag a ListBox object from the Windows Forms tab of the Toolbox onto the Form.
    • b. Press F4 to go right to the properties of this ListBox.
    • c. For the DataSource property, you will need to select the dtatsaet1 data source.
    • d. For the DisplayMember property, you will select student , expand this node, and select studentname.shows the complete ListBox property.

    the DataGrid control to display all of the Orders that are associated with a particular Company that is selected.

    • a. Drag a DataGrid object from the Windows Forms tab of the toolbox onto the Form.
    • b. Press F4 to go right to the properties of this DataGrid.
    • c. For the DataSource property, you will need to select the student data source.
    • d. And, for the DisplayMember property, you will select and expand student, and then select marks

    Just double-click on the form to create a handler for the form's Load event. You will need to clear the DataSet first, and then fill each data adapter that we defined.

    datsaet1.Clear()
    OledbDataAdapter1.Fill(dataset1)
    OledbDataAdapter2.Fill(dataset1)
    
    Previous Home Next