Glossary Item Box
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.
In Visual Basic:
Imports System.Data
Imports System.Data.SqlClient
In C#:
using System.Data;
using System.Data.SqlClient;
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;
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)
{
...
}
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;
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());
}
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.
In XAML:
xmlns:igDP="http://infragistics.com/DataPresenter"
In XAML:
Loaded="Samp_Loaded"
In XAML:
<igDP:XamDataGrid x:Name="XamDataGrid1"/>
In Visual Basic:
...
Me.XamDataGrid1.DataSource = Me.flatData.Tables(0).DefaultView
...
In C#:
...
this.XamDataGrid1.DataSource = this.flatData.Tables[0].DefaultView;
...
