Sunday, September 20, 2009

Select Record using Data Grid,Dataset and Table Adapters in C#.Net

First of all you need to have a Data Connection. (Read.. How to create a Data Connection to SQL Server using C#.net Wizard )

Then you need to have data set and table adapters configured (Read.. How to Create a Dataset, Data Tables, Table Adapter Using C#.NET Wizard) 

Then add a data grid (dtgrid) to your windows form.  And use following code. When we not saved our connection we need to give connection settings to the table adapter when ever it wants to connect to the data base.

image

private void btnSelect_Click(object sender, EventArgs e)
{
// using System.Data.SqlClient; import this namespace
SqlConnectionStringBuilder cb = new SqlConnectionStringBuilder();
cb.UserID = "sa";
cb.Password = "sa123";
cb.InitialCatalog = "CustomerInfo";

// Create SQL Connection by giving SQL Connection String
SqlConnection sc = new SqlConnection(cb.ConnectionString);

CusDataSetTableAdapters.CusMainTableAdapter Ta =
new
CusDataSetTableAdapters.CusMainTableAdapter();
Ta.Connection = sc;
CusDataSet.CusMainDataTable Td = Ta.GetDataByName(txtCutName.Text);

dtGrid.DataSource = Td;
}
Post a Comment