473,385 Members | 1,873 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Databinding a SQLDataReader to a DataGrid control

Hi -

I'm having trouble Databinding a SQLDataReader to a DataGrid control.

I have an ASP.NET web page accessing a SQL database. I've used VS to build
the app and stored it in a directory of my localhost on my development
machine. The database is on the web.

When I run the app on the local machine, IE opens, loads my aspx page from
localhost, and hangs. (Eventually I get a page cannot be displayed error.)
No exceptions are raised.

In the aspx page, I dragged a datagrid from the toolbox to the page and
named it "dgCustomers".

In the aspx.vb page, I define the reader as follows:

Dim conMain As SqlConnection
Dim sqlCmd As SqlCommand
Dim strConn As String
Dim strSQL As String

strConn = [my connection string]
conMain = New SqlConnection(strConn)
sqlCmd = New SqlCommand("SELECT FirstName, LastName FROM
eNPCustomers", conMain)
conMain.Open()
Dim rdrCustomers As SqlDataReader = sqlCmd.ExecuteReader

I bind it to the datagrid as follows:

dgCustomers.DataSource = rdrCustomers
dgCustomers.DataBind()

The page hangs, again when I run it on my development machine. (It runs
fine if I change the page directive from codebehind to src and upload it to
my 3rd-party web server.)

If, instead of the last 2 databinding lines, I simply print out the reader
rows, the page runs fine (all data is displayed as expected):

While rdrCustomers.Read
Response.Write("<br>" & rdrCustomers.GetString(0) & " " &
rdrCustomers.GetString(1))
End While

So the reader seems to successfully get the data from the sql database.
Just to see whether there is something amiss with databinding, I tried the
following (instead of the datareader) and it works fine:

Dim arrCustomers() As String = {"tom", "dick", "harry"}
dgCustomers.DataSource = arrCustomers
dgCustomers.DataBind()

What am I doing wrong??

Thanks for your help.

- Jeff
Nov 19 '05 #1
8 1899
DataGrid's data source has to be bound to an object that
implements IEnumerable interface, such as DataView (or
DataTable / DataSet), ArrayList, HashTable, and so on. But
not DataReader.

HTH

Elton Wang
-----Original Message-----
Hi -

I'm having trouble Databinding a SQLDataReader to a DataGrid control.
I have an ASP.NET web page accessing a SQL database. I've used VS to buildthe app and stored it in a directory of my localhost on my developmentmachine. The database is on the web.

When I run the app on the local machine, IE opens, loads my aspx page fromlocalhost, and hangs. (Eventually I get a page cannot be displayed error.)No exceptions are raised.

In the aspx page, I dragged a datagrid from the toolbox to the page andnamed it "dgCustomers".

In the aspx.vb page, I define the reader as follows:

Dim conMain As SqlConnection
Dim sqlCmd As SqlCommand
Dim strConn As String
Dim strSQL As String

strConn = [my connection string]
conMain = New SqlConnection(strConn)
sqlCmd = New SqlCommand("SELECT FirstName, LastName FROMeNPCustomers", conMain)
conMain.Open()
Dim rdrCustomers As SqlDataReader = sqlCmd.ExecuteReader
I bind it to the datagrid as follows:

dgCustomers.DataSource = rdrCustomers
dgCustomers.DataBind()

The page hangs, again when I run it on my development machine. (It runsfine if I change the page directive from codebehind to src and upload it tomy 3rd-party web server.)

If, instead of the last 2 databinding lines, I simply print out the readerrows, the page runs fine (all data is displayed as expected):
While rdrCustomers.Read
Response.Write("<br>" & rdrCustomers.GetString(0) & " " &rdrCustomers.GetString(1))
End While

So the reader seems to successfully get the data from the sql database.Just to see whether there is something amiss with databinding, I tried thefollowing (instead of the datareader) and it works fine:

Dim arrCustomers() As String = {"tom", "dick", "harry"} dgCustomers.DataSource = arrCustomers
dgCustomers.DataBind()

What am I doing wrong??

Thanks for your help.

- Jeff
.

Nov 19 '05 #2
Thanks Elton -

OK I tried the same thing with a data table; same result (page hangs running
on local machine):

Dim conMain As SqlConnection
Dim sqlCmd As SqlCommand
Dim strConn As String

strConn = [my connection string]
conMain = New SqlConnection(strConn)
sqlCmd = New SqlCommand("SELECT FirstName, LastName FROM
eNPCustomers", conMain)
conMain.Open()

Dim daCustomers As SqlDataAdapter = New SqlDataAdapter(strSQL,
conMain)
Dim dsCustomers As DataSet = New DataSet

daCustomers.Fill(dsCustomers, "Customers")

dgCustomers.DataSource = dsCustomers.Tables("Customers").DefaultView
dgCustomers.DataBind()

conMain.Close()

[Also, if I can't bind a data reader to a data grid, then why does the same
code - with the data reader - work fine when I upload to the server?]

Any other ideas? Can you spot something I'm doing wrong?

- Jeff
"Elton Wang" <an*******@discussions.microsoft.com> wrote in message
news:45****************************@phx.gbl...
DataGrid's data source has to be bound to an object that
implements IEnumerable interface, such as DataView (or
DataTable / DataSet), ArrayList, HashTable, and so on. But
not DataReader.

HTH

Elton Wang
-----Original Message-----
Hi -

I'm having trouble Databinding a SQLDataReader to a

DataGrid control.

I have an ASP.NET web page accessing a SQL database.

I've used VS to build
the app and stored it in a directory of my localhost on

my development
machine. The database is on the web.

When I run the app on the local machine, IE opens, loads

my aspx page from
localhost, and hangs. (Eventually I get a page cannot be

displayed error.)
No exceptions are raised.

In the aspx page, I dragged a datagrid from the toolbox

to the page and
named it "dgCustomers".

In the aspx.vb page, I define the reader as follows:

Dim conMain As SqlConnection
Dim sqlCmd As SqlCommand
Dim strConn As String
Dim strSQL As String

strConn = [my connection string]
conMain = New SqlConnection(strConn)
sqlCmd = New SqlCommand("SELECT FirstName, LastName

FROM
eNPCustomers", conMain)
conMain.Open()
Dim rdrCustomers As SqlDataReader =

sqlCmd.ExecuteReader

I bind it to the datagrid as follows:

dgCustomers.DataSource = rdrCustomers
dgCustomers.DataBind()

The page hangs, again when I run it on my development

machine. (It runs
fine if I change the page directive from codebehind to

src and upload it to
my 3rd-party web server.)

If, instead of the last 2 databinding lines, I simply

print out the reader
rows, the page runs fine (all data is displayed as

expected):

While rdrCustomers.Read
Response.Write("<br>" & rdrCustomers.GetString(0)

& " " &
rdrCustomers.GetString(1))
End While

So the reader seems to successfully get the data from the

sql database.
Just to see whether there is something amiss with

databinding, I tried the
following (instead of the datareader) and it works fine:

Dim arrCustomers() As String =

{"tom", "dick", "harry"}
dgCustomers.DataSource = arrCustomers
dgCustomers.DataBind()

What am I doing wrong??

Thanks for your help.

- Jeff
.

Nov 19 '05 #3
On Fri, 4 Mar 2005 19:31:28 -0800, "Elton Wang"
<an*******@discussions.microsoft.com> wrote:
DataGrid's data source has to be bound to an object that
implements IEnumerable interface, such as DataView (or
DataTable / DataSet), ArrayList, HashTable, and so on. But
not DataReader.

HTH

Elton Wang


Elton:

A data reader does implement IEnumerable and can be used as a
DataSource (but not if the grid is set for automatic paging - then it
needs an object with an IList interface).

--
Scott
http://www.OdeToCode.com/blogs/scott/
Nov 19 '05 #4
Thanks Scott and Elton -

I think I've gotten this fixed now, Scott's paging clue got me trying a few
different things --

If I set the datagrid's EnableViewState = False, everything works fine (with
either the dataset or the datareader approach).

- Jeff
"Scott Allen" <sc***@nospam.odetocode.com> wrote in message
news:qu********************************@4ax.com...
On Fri, 4 Mar 2005 19:31:28 -0800, "Elton Wang"
<an*******@discussions.microsoft.com> wrote:
DataGrid's data source has to be bound to an object that
implements IEnumerable interface, such as DataView (or
DataTable / DataSet), ArrayList, HashTable, and so on. But
not DataReader.

HTH

Elton Wang


Elton:

A data reader does implement IEnumerable and can be used as a
DataSource (but not if the grid is set for automatic paging - then it
needs an object with an IList interface).

--
Scott
http://www.OdeToCode.com/blogs/scott/

Nov 19 '05 #5
HI Jeff:

On Sat, 05 Mar 2005 03:50:03 GMT, "Jeff" <je*********@eNetPortals.com>
wrote:
Thanks Elton -

OK I tried the same thing with a data table; same result (page hangs running
on local machine):

<snip>

There is nothing obviously wrong with the code. Perhaps there is
something awry with the environment. Is there a firewall between your
machine and the database? Could it be the browser?

[Also, if I can't bind a data reader to a data grid, then why does the same
code - with the data reader - work fine when I upload to the server?]

You can bind a data reader to a grid - it does work - really.
Any other ideas? Can you spot something I'm doing wrong?


Like I say - nothing looks wrong with the code - so check other pieces
of the system: browser settings, network settings, machine settings..

HTH,

--
Scott
http://www.OdeToCode.com/blogs/scott/

Nov 19 '05 #6
Glad you figured out a workaround, Jeff. The strange thing is the page
should work with ViewState enabled - is it a really large Viewstate
perhaps?

That being said - no need to enable view state if you don't need the
overhead.

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Sat, 05 Mar 2005 04:34:44 GMT, "Jeff" <je*********@eNetPortals.com>
wrote:
Thanks Scott and Elton -

I think I've gotten this fixed now, Scott's paging clue got me trying a few
different things --

If I set the datagrid's EnableViewState = False, everything works fine (with
either the dataset or the datareader approach).

- Jeff
"Scott Allen" <sc***@nospam.odetocode.com> wrote in message
news:qu********************************@4ax.com.. .
On Fri, 4 Mar 2005 19:31:28 -0800, "Elton Wang"
<an*******@discussions.microsoft.com> wrote:
>DataGrid's data source has to be bound to an object that
>implements IEnumerable interface, such as DataView (or
>DataTable / DataSet), ArrayList, HashTable, and so on. But
>not DataReader.
>
>HTH
>
>Elton Wang
>


Elton:

A data reader does implement IEnumerable and can be used as a
DataSource (but not if the grid is set for automatic paging - then it
needs an object with an IList interface).

--
Scott
http://www.OdeToCode.com/blogs/scott/


Nov 19 '05 #7
Thanks Scott -

I wouldn't think the viewstate was large - the file has about 400 records,
and I'm just selecting 2 columns. But, when I opened the source of the
'hung' page, the viewstate field looked pretty large. I obviously couldn't
make sense of its contents, so I tried disabling it. Could be something
about size. Could be some kind of a timeout issue. I'm not aware of any
limits or other issues that would apply.

- Jeff
"Scott Allen" <sc***@nospam.odetocode.com> wrote in message
news:bc********************************@4ax.com...
Glad you figured out a workaround, Jeff. The strange thing is the page
should work with ViewState enabled - is it a really large Viewstate
perhaps?

That being said - no need to enable view state if you don't need the
overhead.

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Sat, 05 Mar 2005 04:34:44 GMT, "Jeff" <je*********@eNetPortals.com>
wrote:
Thanks Scott and Elton -

I think I've gotten this fixed now, Scott's paging clue got me trying a fewdifferent things --

If I set the datagrid's EnableViewState = False, everything works fine (witheither the dataset or the datareader approach).

- Jeff
"Scott Allen" <sc***@nospam.odetocode.com> wrote in message
news:qu********************************@4ax.com.. .
On Fri, 4 Mar 2005 19:31:28 -0800, "Elton Wang"
<an*******@discussions.microsoft.com> wrote:

>DataGrid's data source has to be bound to an object that
>implements IEnumerable interface, such as DataView (or
>DataTable / DataSet), ArrayList, HashTable, and so on. But
>not DataReader.
>
>HTH
>
>Elton Wang
>

Elton:

A data reader does implement IEnumerable and can be used as a
DataSource (but not if the grid is set for automatic paging - then it
needs an object with an IList interface).

--
Scott
http://www.OdeToCode.com/blogs/scott/

Nov 19 '05 #8
Hi Scott,

Thanks

-----Original Message-----
On Fri, 4 Mar 2005 19:31:28 -0800, "Elton Wang"
<an*******@discussions.microsoft.com> wrote:
DataGrid's data source has to be bound to an object that
implements IEnumerable interface, such as DataView (or
DataTable / DataSet), ArrayList, HashTable, and so on. Butnot DataReader.

HTH

Elton Wang

Elton:

A data reader does implement IEnumerable and can be used

as aDataSource (but not if the grid is set for automatic paging - then itneeds an object with an IList interface).

--
Scott
http://www.OdeToCode.com/blogs/scott/
.

Nov 19 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Kevin | last post by:
Hi Al When I have a text box, list box, and a datagrid, how does a CurrencyManager relate to all these controls(seeing as though the datagrid uses Complex binding and textbox and list box use...
5
by: Wing | last post by:
Hi all, I execute a stored procedure in my C# code, assign the result to the SqlDataReader object and display it with datagrid. the question I like to ask, is possible to edit the datagrid...
1
by: oafyuf | last post by:
Hi, I'm trying to learn ASP.NET by doing a pilot project: I have a DataGrid which contains a nested DataList. I want to iterate through the SQLDataReader for the DataGrid and populate each...
4
by: Jason S | last post by:
I haven't been able to find a clear answer to this and I'm hoping someone could enlighten me. As pertains to databinding a control in a repeating fashion(datagrid, repeater, etc.) what event...
4
by: aroth | last post by:
I have a basic form with some dropdownlist controls that I would like to populate with items from a database. I keep getting the error "Cannot implicitly convert type...
10
by: Krista Lemieux | last post by:
I'm new to ASP.NET and I'm not use to the way things are handled with this technology. I've been told that when I have a control, I should only bind the data to it once, and not on each post back...
2
by: Tim::.. | last post by:
Can someone explain how I create a databinding to a Literal control! I tried doing it as shown below but the reader won't read into the control. Thanks ...::CODE::.. Dim Myconn As New...
8
by: GaryDean | last post by:
We have been noticing that questions on vs.2005/2.0 don't appear to get much in answers so I'm reposting some questions posted by some of the programmers here in our organization that never got...
3
by: rn5a | last post by:
A SqlDataReader is populated with the records from a SQL Server 2005 DB table. The records retrieved depends upon 2 conditions (the conditions depend on what a user selects in an ASPX page). If...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.