View Single Post
  #2 (permalink)  
Old 03-18-2008
mira mira is offline
Jr. Member

Join Date: Mar 2008
Posts: 263
BRL$: -164.73
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 41
mira is on a distinguished road
Default Re: How to bind datagrid in asp.net

There are two ways of binding datagrid in asp.net

1) Data reader
2) Data adapter

HTML Code:
Imports System.Data.SqlClient
   For select query to a SQL database, you must create a SqlConnection to the database passing the connection string, and then construct a SqlDataAdapter object that contains your query statement.  To populate a DataSet object with the results from the query, you call the command's Fill method. 
  
Dim cn As New SqlConnection("server=yourservername;database=pubs;uid=username;pwd=")
cn.Open()
Dim cmd As New SqlDataAdapter("select * from Authors", cn)

Dim ds As New DataSet()
cmd.Fill(ds, "Authors")

The advantage of using a dataset is that it gives you a disconnected view of the database.  You can operate on a dataset in your application, and then
reconcile your changes with the actual database later.

DataGrid1.DataSource=ds.Tables("authors")
DataGrid1.DataBind()

Hope it will help you bind grid using asp.net
It is a code in vb.net you can convert it using c#


Questions & Answers Section. Raise your question and receive best of the best reply from community members.
Reply With Quote