How to populate a DataGrid using a DataSet

Platform: ASP.NET| Category: DataGrid

<asp:DataGrid id='DataGrid1' runat='server'></asp:DataGrid>

Use the namespace System.Data.SqlClient
VB.NET


Dim cn As SqlConnection
Dim da As SqlDataAdapter
Dim ds As DataSet
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
	cn = New SqlConnection('Server=localhost;uid=sa;pwd=;database=northwind')
	da = New SqlDataAdapter('Select * from products', cn)
	ds = New DataSet
	da.Fill(ds, 'Products')
	DataGrid1.DataSource = ds.Tables(0)
	’DataGrid1.DataSource = ds
	’DataGrid1.DataSource = ds.Tables('Product')
	DataGrid1.DataBind()
End Sub

C#


SqlConnection cn;
SqlDataAdapter da;
DataSet ds;
private void Page_Load(object sender, System.EventArgs e)
{
	cn=  new SqlConnection ('Server=localhost;uid=sa;pwd=;database=northwind');
	da= new SqlDataAdapter ('SELECT * FROM Products   ', cn);
	ds= new DataSet ();
	da.Fill (ds, 'Product');
	DataGrid1.DataSource =ds.Tables[0];
	//DataGrid1.DataSource= ds;
	//DataGrid1.DataSource= ds.Tables['Products'];
	DataGrid1.DataBind ();
}

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.