Binding to a Flat SQL Database

Glossary Item Box

Language

Visual Basic

C#

JScript

Show All

Languages Infragistics(R) NetAdvantage(R) for WPF

Binding to a Flat SQL Database

This topic shows you how to create the necessary components (SQLConnection, DataSet, SQLDataAdapter) to bind to a flat SQL database in procedural code. The procedural code is written generically enough that it is control-independent, and no mention of a data-displaying control is made until the "Use the Data Provider" section of the topic.

Note: This topic assumes you have a catalog called Northwind running on a Microsoft® SQL Server. If you don't have this catalog, you will need to change the SqlCommand and SqlConnection to point to the name of catalog you have running.

Set Up the Data Provider

  1. Create a Microsoft® Windows® Presentation Foundation Window or Page project.
  2. Before you start writing any code, you should place using/imports directives in your code-behind so you don't need to always type out a member's fully qualified name.

    In Visual Basic:

    Imports System.Data
    Imports System.Data.SqlClient
    

    In C#:

    using System.Data;
    using System.Data.SqlClient;
    

  3. You need to declare private variables for the SELECT command, connection, data set, and data adapter. The following example code shows what these declarations look like.

    In Visual Basic:

    Private sqlSelectCommand1 As System.Data.SqlClient.SqlCommand
    Private sqlConnection1 As System.Data.SqlClient.SqlConnection
    Private sqlDataAdapter1 As System.Data.SqlClient.SqlDataAdapter
    Private flatData As System.Data.DataSet
    

    In C#:

    private System.Data.SqlClient.SqlCommand sqlSelectCommand1;
    private System.Data.SqlClient.SqlConnection sqlConnection1;
    private System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
    private System.Data.DataSet flatData;
    
  4. Create a method that takes in an object, and a RoutedEventArgs. This method will be called when the form is loaded (you will make that declaration in a subsequent step). The following example code shows what this method could look like.

    In Visual Basic:

    Sub Samp_Loaded(ByVal o As Object, ByVal e As RoutedEventArgs)
    	...
    End Sub
    

    In C#:

    void Samp_Loaded(object o, RoutedEventArgs e)
    {
    	...
    }
    

  5. Once the method is declared, you need to initialize the variables that were created in step 3. The following example code shows you how to initialize all the variables. See inline comments for details of what each line does. In the SqlConnection initialization, you will need to change "DATABASE_NAME" to the name of your database server.

    In Visual Basic:

    ' Initialize DataSet and give it a name
    Me.flatData = New DataSet("flatData")
    ' Initialize the SqlConnection providing the 
    ' connection string
    Me.sqlConnection1 = New SqlConnection("Data Source=DATABASE_NAME;Initial " & _
      "Catalog=Northwind;Integrated Security=True")
    ' Initialize the SqlCommand that contains the Select command
    Me.sqlSelectCommand1 = New SqlCommand("SELECT     CustomerID, CompanyName, " & _
      "ContactName, ContactTitle, Address, City, PostalCode, " & _
      "Country, Phone" + ControlChars.Cr + ControlChars.Lf + "FROM         Customers")
    ' Assign the SqlConnection to the Connection property of the
    ' SqlCommand
    Me.sqlSelectCommand1.Connection = Me.sqlConnection1
    ' Initialize the SqlDataAdapter
    Me.sqlDataAdapter1 = New SqlDataAdapter()
    ' Assign the SelectCommand property to the SqlCommand that has
    ' been created
    Me.sqlDataAdapter1.SelectCommand = Me.sqlSelectCommand1
    

    In C#:

    // Initialize DataSet and give it a name
    this.flatData = new DataSet("flatData");
    // Initialize the SqlConnection providing the 
    // connection string
    this.sqlConnection1 = 
      new SqlConnection("Data Source=DATABASE_NAME;Initial " +
      "Catalog=Northwind;Integrated Security=True");
    // Initialize the SqlCommand that contains the Select command
    this.sqlSelectCommand1 = 
      new SqlCommand("SELECT     CustomerID, CompanyName, " + 
      "ContactName, ContactTitle, Address, City, PostalCode, " +
      "Country, Phone\r\nFROM         Customers");
    // Assign the SqlConnection to the Connection property of the
    // SqlCommand
    this.sqlSelectCommand1.Connection = this.sqlConnection1;
    // Initialize the SqlDataAdapter
    this.sqlDataAdapter1 = new SqlDataAdapter();
    // Assign the SelectCommand property to the SqlCommand that has
    // been created
    this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1;
    

  6. The following example code fills the data set. This is also were you would set a control's data source property, which you will be doing in subsequent steps. This is done inside a try-catch block, just in case an exception is thrown. You should place the example code below directly after the example code shown in step 5.

    In Visual Basic:

    ...
    Try
    	' Fill the DataSet
    	Me.sqlDataAdapter1.Fill(Me.flatData)
    	' Assign the DataSet's DefaultView to a control's data source
    Catch ex As SqlException
    	' Catch and display any exceptions that may occur
    	MessageBox.Show(ex.Message.ToString())
    End Try
    

    In C#:

    ...
    try
    {
    	// Fill the DataSet
    	this.sqlDataAdapter1.Fill(this.flatData);
    	// Assign the DataSet's DefaultView to a control's data source
    }
    catch (SqlException ex)
    {
    	// Catch and display any exceptions that may occur
    	MessageBox.Show(ex.Message.ToString());
    }
    

Use the Data Provider

At this point you have everything in place to retrieve your data from your database. Now let's discuss how to display the retrieved data inside one of the NetAdvantage for WPF data-driven controls. The control options include xamDataCarousel™, xamDataGrid™, xamDataPresenter™, and xamCarouselListBox™. With the exception of xamCarouselListBox, connecting the data set to the control is as simple as setting the control's DataSource property. For the xamCarouselListBox you have to set the ItemsSource property. 

The following steps show you how to take the above setup data provider, and connect the data set to xamDataGrid's DataSource property.

  1. In XAML, place the following namespace declaration inside the opening Page or Window tag for easier declaration of xamDataGrid.

    In XAML:

    xmlns:igDP="http://infragistics.com/DataPresenter"
    

  2. In addition, in the opening Page or Window tag of the XAML file set the Loaded property to the name of the method you created in the code-behind. 

    In XAML:

    Loaded="Samp_Loaded"
    

  3. Use the following XAML code to create an instance of XamDataGrid, and assign a name to it that you can reference in procedural code.

    In XAML:

    <igDP:XamDataGrid x:Name="XamDataGrid1"/>
    

  4. In your code-behind file, look for the comment: "Assign the DataSet's DefaultView to a control's data source", and place the following code snippet.

    In Visual Basic:

    ...
    	 Me.XamDataGrid1.DataSource = Me.flatData.Tables(0).DefaultView
    ...
    

    In C#:

    ...
    	this.XamDataGrid1.DataSource = this.flatData.Tables[0].DefaultView;
    ...
    

  5. Build and run the application. You should see xamDataGrid populated with the data.

E-mail your feedback on this topic.

Opinion about our help? Take our survey.

Copyright © 2003-2007 Infragistics, Inc. All rights reserved.

Build Version: 7.1.20071.1320