Help please

  • Thread starter Thread starter Gary
  • Start date Start date
G

Gary

Hi all,

I want to bind a Datagrid to Sqlcommand, below is my code

SqlConnection cn = new SqlConnection("Server=winnt;user id =sa;initial

catalog =Northwind");

cn.Open();

SqlCommand cmd = new SqlCommand("select * from employees");

cmd.Connection = cn;

dr = cmd.executereader()

Datagrid1.datasource = dr

Can we bind Datagrid to a command directly or to Data Reader without using

dataAdaptaor ?

TIA,

Gary
 
Hi Gary,

You can bind to a datareader. You don't need a dataadapter unless you are
intending to fill a dataset.

HTH,
 
Gary,

It depends on what you want to databind to. ASP.Net controls are rendered
by a one-time read through the data source, and you can bind ASP.Net
controls to data readers (which only support on-time forward reading).

Windows controls such as the DataGrid however need to be able to scroll
backwards and forwards through the data source and use Complex Binding to
achieve this. Hence, you can bind DataGrids to collections, arrays,
DataTables and DataSets, but not to DataReaders.

I suspect you have already tried this and found it not to work, as you have
all the required code there anyway.


Neil.
 
Back
Top