473,695 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 "dgCustomer s".

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(s trConn)
sqlCmd = New SqlCommand("SEL ECT FirstName, LastName FROM
eNPCustomers", conMain)
conMain.Open()
Dim rdrCustomers As SqlDataReader = sqlCmd.ExecuteR eader

I bind it to the datagrid as follows:

dgCustomers.Dat aSource = rdrCustomers
dgCustomers.Dat aBind()

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.Re ad
Response.Write( "<br>" & rdrCustomers.Ge tString(0) & " " &
rdrCustomers.Ge tString(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.Dat aSource = arrCustomers
dgCustomers.Dat aBind()

What am I doing wrong??

Thanks for your help.

- Jeff
Nov 19 '05 #1
8 1910
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 "dgCustomer s".

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(s trConn)
sqlCmd = New SqlCommand("SEL ECT FirstName, LastName FROMeNPCustomers ", conMain)
conMain.Open()
Dim rdrCustomers As SqlDataReader = sqlCmd.ExecuteR eader
I bind it to the datagrid as follows:

dgCustomers.Dat aSource = rdrCustomers
dgCustomers.Dat aBind()

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.Re ad
Response.Write( "<br>" & rdrCustomers.Ge tString(0) & " " &rdrCustomers.G etString(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.Dat aSource = arrCustomers
dgCustomers.Dat aBind()

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(s trConn)
sqlCmd = New SqlCommand("SEL ECT FirstName, LastName FROM
eNPCustomers", conMain)
conMain.Open()

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

daCustomers.Fil l(dsCustomers, "Customers" )

dgCustomers.Dat aSource = dsCustomers.Tab les("Customers" ).DefaultView
dgCustomers.Dat aBind()

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*******@disc ussions.microso ft.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 "dgCustomer s".

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(s trConn)
sqlCmd = New SqlCommand("SEL ECT FirstName, LastName

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

sqlCmd.ExecuteR eader

I bind it to the datagrid as follows:

dgCustomers.Dat aSource = rdrCustomers
dgCustomers.Dat aBind()

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.Re ad
Response.Write( "<br>" & rdrCustomers.Ge tString(0)

& " " &
rdrCustomers.G etString(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.Dat aSource = arrCustomers
dgCustomers.Dat aBind()

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*******@disc ussions.microso ft.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.o detocode.com> wrote in message
news:qu******** *************** *********@4ax.c om...
On Fri, 4 Mar 2005 19:31:28 -0800, "Elton Wang"
<an*******@disc ussions.microso ft.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*********@eN etPortals.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*********@eN etPortals.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.o detocode.com> wrote in message
news:qu******* *************** **********@4ax. com...
On Fri, 4 Mar 2005 19:31:28 -0800, "Elton Wang"
<an*******@disc ussions.microso ft.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.o detocode.com> wrote in message
news:bc******** *************** *********@4ax.c om...
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*********@eN etPortals.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.o detocode.com> wrote in message
news:qu******* *************** **********@4ax. com...
On Fri, 4 Mar 2005 19:31:28 -0800, "Elton Wang"
<an*******@disc ussions.microso ft.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*******@dis cussions.micros oft.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
1383
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 simple bound. So for example if I have a text box, list box and a Datagrid on a windows form, and I want to navigate the records in a dataset(and we must not forget the child records from the child tables aswell!) How do I create the CurrencyManager to...
5
12129
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 output (eg. delete a row in the datagrid) and update the change in the corresponding table in the database? thanks for your time.
1
2228
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 DataList by binding to a fresh SQLDataReader. At the moment I can only bind to the first instance of the DataList. If I could bind the data by the ClientID (or UniqueID), I'm sure it would work. Is there a way of doing this? Or maybe there is a better...
4
1297
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 would one handle to get a handle on things just BEFORE the data is bound. To be clearer, not controlling the formatting of the bound control(backcolor or what have you), but rather control the WAYor even IF the data is bound at all. For instance,...
4
1167
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 'System.Data.SqlClient.SqlDataReader' to 'bugreq.SqlDataReader'". The code from 'codebehind' is posted below. using System; using System.Collections; using System.ComponentModel; using System.Data;
10
4507
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 (basically have the data binding done in the If NOT IsPostBack Then statement). How come? How does this information get preserved? Which raises another question. Becuase I want to get away from the spaghetti code approach, I would like to create...
2
1732
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 SqlConnection(ConfigurationSettings.AppSettings("strConn")) Dim cmd As New SqlCommand("PageDetails", Myconn) cmd.CommandType = CommandType.StoredProcedure
8
2177
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 answered... In 1.1 we always did our own myDataAdapter.fills and we liked that control for lots of good reasons. Now the new DataSource (or is it a TableAdapter:Dataset) automatically fills the Gridview. How can we control that fill? In a...
3
4362
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 condition1 is true, then the SqlDataReader will be populated with the records existing in table1 & will return 0 to the calling function but if condition2 is true, then the SqlDataReader will be populated with the records from another table named...
0
8630
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8568
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9115
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8982
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8845
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8825
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6491
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5839
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4579
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.