Solved

Possible to bind data to Gridview using DataReader?

Posted on 2007-03-18
16
4,648 Views
Last Modified: 2012-06-22
I was reading this site . This is about DataGrid but it says not to use dataset in web app. It says :
"The DataGrid simply displays data from some data source (be it a DataSet, DataReader, custom collection, etc.). "...

http://scottonwriting.net/sowblog/posts/3778.aspx

Now, is it possible to bind dataView to DataReader...if so, anyone with an example?
0
Comment
Question by:Camillia
  • 10
  • 6
16 Comments
 
LVL 9

Expert Comment

by:bele04
ID: 18745750
it is possible to bind a datareader to a gridview since the gridview just takes a collection of objects and displays it.  Almost any type of collection, if not all, can be displayed using the GridView.  These would include like custom made collections, ArrayLists, DataReader, an array of strings and the likes and etc.  All you need to do to bind a collection to the gridview is simply assign it as the datasource to the gridview just like what you would do for a dataset.

GridView1.DataSource = datareader1;
GridView1.DataBind();

0
 
LVL 7

Author Comment

by:Camillia
ID: 18745771
no need to loop thru the datareader?? no need to check for  , for example,

if datareader.read() then

end if

Or should  I be doing this:

if datareader.read() then
 GridView1.DataSource = datareader1;
GridView1.DataBind();
end if
0
 
LVL 7

Author Comment

by:Camillia
ID: 18745889
And is this correct "Because the sorting behavior of SqlDataSource relies on the underlying DataView Sort property, sorting is only supported by SqlDataSource when in DataSet mode; if set to DataReader mode, sorting will be disabled." ???

Per this site...so If i use a datareader, i cant use hte sorting?
http://dotnetjunkies.com/QuickStartv20/aspnet/doc/data/databases.aspx
0
 
LVL 9

Accepted Solution

by:
bele04 earned 500 total points
ID: 18745964
checking is another thing.  You should always check and see if the datasource you're binding to your data control (ie. GridView) has values in it.  This doesn't only apply to dataset and datareader but every other collection you're planning to bind to a GridView itself.

As for the SqlDataSource, it's best to use the DataSourceMode property of the SqlDataSource to datareader only when you want to display read-only data since DataReader is a forward-only, read-only data container.  If you want to have filtering and sorting features on your GridView then you should definitely set the DataSourceMode property to DataSet.  You can read more about the SqlDataSource on the msdn website: http://msdn2.microsoft.com/en-us/library/dz12d98w(vs.80).aspx
0
 
LVL 7

Author Comment

by:Camillia
ID: 18747846
wonder why the author is such an advocate of not using datasets .
In this article, he mentions that dataset is usefull in some situations and he mentions 2:
http://aspnet.4guysfromrolla.com/articles/050405-1.aspx

Another author here says the only time he finds dataset is usefull is when the underlying data changes:
http://builder.com.com/5100-6387-1045330.html

But based on MS, dataset HAS to be used for sorting/filtering. Maybe those 2 authors use datareader in gridview/datagrid but then find a way to sort/filter as well...i dont know.
0
 
LVL 9

Expert Comment

by:bele04
ID: 18752808
There are actually other ways to sort and filter data and that doesn't include the dataset.  DataSet is only one of the options you have.  From what I read in the article, the author seems to be very picky about performance of web applications on a large scale basis.  I mean it's only natural we prioritize performance so as not to overload the server where the web applications is located.  And also, performance and features will almost always, if not always, be inversely proportional to each other.  

You can still use a dataset if you want but you have to take into careful consideration the performance tradeoffs that goes along with it and adjust accordingly.
0
 
LVL 7

Author Comment

by:Camillia
ID: 18752940
what are other ways of doing it without dataset? was it in that article??
0
 
LVL 7

Author Comment

by:Camillia
ID: 18752997
and i wouldnt say my project is large scale. I will display only top 20 results in a gridview. I dont think that's worth the trouble of finding another way of binding to a gridview and sort/filter..no?
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 9

Expert Comment

by:bele04
ID: 18753073
that depends whether you have a large number of users or not.  You'll have to think about the maximum number of users accessing the data at the same time and see if that would affect the performance of your web application.  
0
 
LVL 7

Author Comment

by:Camillia
ID: 18753243
and what other way can I use sorting/filtering with gridview if i use datareader but not dataset??
0
 
LVL 7

Author Comment

by:Camillia
ID: 18753425
0
 
LVL 9

Expert Comment

by:bele04
ID: 18753622
you can use the queries for filtering the results to be displayed in the GridView.  The SqlDataSource has built-in functionality that supports this.  You can check this tutorial: http://www.exforsys.com/content/view/1652/354/

The SqlDataSource's sorting is disabled when using the DataReader DataSourceMode.  So for sorting you can either follow the example given in the link you provided.  If your data comes from an XML file then the XMLDataSource provides sorting/filtering features you can use.  Also you can implement your own Collection object to implement sorting/filtering if you like.  
0
 
LVL 7

Author Comment

by:Camillia
ID: 18753695
i emailed the author and he recommanded reading this article and using typed datasets:
http://www.asp.net/Learn/DataAccess/

Cant do that because my code has to work with AS400 and SQL Server.
Just looked at your link. Dont think i can use that either. My code has to work with as400 and sql server. I have one DAL that connects to both based on what DB the user selects...

I think that example is good start if i dont want to use a dataset. Again , i dont want to make it harder than it is. *** At most 20 rows, at most 50 users at peak time *** ( we have clients where only 3 or 4 people use the app)...
0
 
LVL 7

Author Comment

by:Camillia
ID: 18753705

He just responded to my question about what he recommands...for 20 rows, at most 50 users and he said : "Go with the DataSet, just have a method in the TableAdapter that you can use to return a subset of records (i.e., SELECT TOP 20 ... FROM ...)" ....

Not sure what he means by "return a subset of records"....my stored proc will return top 20 rows...

u know what he's talking about (didnt want to email him again and ask :-) )
0
 
LVL 9

Expert Comment

by:bele04
ID: 18754207
With that many users I would also suggest to simply go with the DataSet.  

What he probably meant was to have a function that returns only a specified number of records from an entire set of data.  This number of records could well be a variable that you could past to it or a fixed number depending on your requirements.  Normally, TableAdapters are used for typed datasets so I'm assuming he wanted to suggest to use a typed dataset.  TableAdapters are an extended version of the DataAdapters and they behave almost exactly the same except for the extra features you could add when creating TableAdapters.
0
 
LVL 7

Author Comment

by:Camillia
ID: 18755747
"to have a function that returns only a specified number of records from an entire set of data"....i dont think i still understand it...i'm returning top 20 from the stored proc...still narrow it down??

I agree, the author did recommand typed datasets...he's written articles about it...

 I'll look into tableadapters in combination with datasets...

someone said it's possible to do a sorting , client side, using Javascript. I havent seen examples of that .
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now