Shalvin.Com                                                                                                                      Home

DataSet, SqlDataAdapter and DataGridView

DataSet is an offline disconnected data store.

SqlDataAdapter
is an intemediary between the RDBMS and DataSet. The methods of SqlDataAdapter are Fill and Update. Fill Method is used for filling a DataSet. Update Method is used synchronizing the RDBMS with changes in DataSet


using System.Data.SqlClient;

SqlConnection cnn;
SqlDataAdapter da;
DataSet ds = new DataSet();
private void Form1_Load(object sender, EventArgs e)
{
  cnn = new SqlConnection("Integrated Security=sspi;Initial Catalog=Northwind");
  da = new SqlDataAdapter("select CategoryId, CategoryName, Description from Categories", cnn);
  da.Fill(ds, "Cat");
  dataGridView1. DataSource = ds.Tables["Cat"];
}

DataSet and ComboBox

In the case with a ComboBox you have to additionally specify the field to the displayed using the DisplayMember property.
In the case with Asp.Net DropDownList the equivalent property is DataTextField.

using System.Data.SqlClient;

SqlConnection cnn;
SqlDataAdapter da;
DataSet ds = new DataSet();
private void Form1_Load(object sender, EventArgs e)
{
  cnn = new SqlConnection("Integrated Security=sspi;Initial Catalog=Northwind");
  da = new SqlDataAdapter("select CategoryId, CategoryName, Description from Categories", cnn);
  da.Fill(ds, "Cat"); 
  cboCategories.DataSource = ds.Tables["Cat"];
  cboCategories.DisplayMember = "CategoryName";
}


Contact : shalvin@gmail.com