Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old April 9th, 2007, 04:41 AM
Authorized User
 
Join Date: Nov 2006
Location: Mandi, Himachal Pradesh, India.
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to akhilhp
Default Working With Datareader

I am using datareaders to populate my gridview. the code is :
Datareader1 = sqlcmd.executereader

gridview1.datasource = datareader1
gridview1.databind()

Every thing is working fine. I only want following:
if the executereader returns no rows i.e query has generated no records, I wish too show the message "No Records Available" How to do that: I have tried to check by using :

if datareader1.read then
     gridview1.datasource = datareader1
     gridview1.databind()
else
     Show the message
endif
but here gridview1 gets one row less (because I think one row is lost in the read command and rest has been populated to gridview1)

Any Idea????
Reply With Quote
  #2 (permalink)  
Old April 9th, 2007, 05:19 AM
Friend of Wrox
Points: 1,935, Level: 17
Points: 1,935, Level: 17 Points: 1,935, Level: 17 Points: 1,935, Level: 17
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2004
Location: United Kingdom
Posts: 550
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi

Try this,

 SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection) ;

            if (dr.HasRows)
            {
                grd.DataSource = dr;
                grd.DataBind();
            }
            else
            {
                lbl.Text = "No Records";
            }

Regards
Mike

Fortune favours the brave, so don't regret on missed oppurtunities.
Reply With Quote
  #3 (permalink)  
Old April 10th, 2007, 12:18 AM
Authorized User
 
Join Date: Nov 2006
Location: Mandi, Himachal Pradesh, India.
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to akhilhp
Default

Thanks a lot..
It has worked...
Reply With Quote
  #4 (permalink)  
Old October 8th, 2007, 03:05 PM
Sjo Sjo is offline
Registered User
 
Join Date: Oct 2007
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

I am using IDataReader and trying to see if the query returns value or not. Since I am using IDataReader and that the only option I have for the getting the result using Microsoft.Enterprise.Library, is there any way I can check weather IDataReader has records or not?



Reply With Quote
  #5 (permalink)  
Old October 8th, 2007, 03:10 PM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Consider that your IDataReader instance is named dr, your code would be:

if(dr.HasRows)
{
 //DataReader hasrows
}
else
{
 //DataReader is empty
}


================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
Technical Editor for:
Beginning Visual C# 2008
Professional Search Engine Optimization with ASP.NET
Professional IIS 7 and ASP.NET Integrated Programming
Wrox Blox: Introduction to Google Gears
Wrox Blox: Create Amazing Custom User Interfaces with WPF and .NET 3.0
================================================== =========
Reply With Quote
  #6 (permalink)  
Old October 8th, 2007, 03:18 PM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Quote:
quote:Consider that your IDataReader instance is named dr, your code would be:
if(dr.HasRows)
Unfortunately, that won't work, as IDataReader doesn't define a HasRows property.

You either need to cast your reader to something that does support HasRows or look a this work around: http://imar.spaanjaars.com/QuickDocId.aspx?quickdoc=356

Basically, it checks out the bound control afterwards to see if it contains aby rows....

Imar

---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Reply With Quote
  #7 (permalink)  
Old October 8th, 2007, 03:25 PM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Ahh silly me. Imar is quite right and his work around that he displays on his site will work. You could also make use of some logic and .Read() to determine if the IDataReader contains any data which is a similar approach to what Imar provides on his site.

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
Technical Editor for:
Beginning Visual C# 2008
Professional Search Engine Optimization with ASP.NET
Professional IIS 7 and ASP.NET Integrated Programming
Wrox Blox: Introduction to Google Gears
Wrox Blox: Create Amazing Custom User Interfaces with WPF and .NET 3.0
================================================== =========
Reply With Quote
  #8 (permalink)  
Old October 8th, 2007, 03:29 PM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Yeah, but just realize that, as akhilhp has pointed out, Read() "eats" a record....

Imar

---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Reply With Quote
  #9 (permalink)  
Old October 8th, 2007, 03:34 PM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

True but logic like this:

Code:
bool hasRecords = false;

while(dr.Read())
{
 //do something if the IDataReader has data
 hasRecords = true;
}

if(!hasRecords){//do something else}
should aleviate the problem of data being lost. Obviously you can get this done with less lines as you show on your site, but this would still be a workable solution.

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
Technical Editor for:
Beginning Visual C# 2008
Professional Search Engine Optimization with ASP.NET
Professional IIS 7 and ASP.NET Integrated Programming
Wrox Blox: Introduction to Google Gears
Wrox Blox: Create Amazing Custom User Interfaces with WPF and .NET 3.0
================================================== =========
Reply With Quote
  #10 (permalink)  
Old October 8th, 2007, 03:40 PM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Yes, I see what you mean. That would work perfectly OK....

Imar

---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
datareader MunishBhatia ASP.NET 2.0 Professional 2 October 17th, 2007 07:05 AM
regarding datareader adityamadisetty VB.NET 1 May 8th, 2006 09:31 AM
Datareader NitinJoshi ADO.NET 4 January 31st, 2005 08:34 AM
datareader surapongmax ADO.NET 1 October 8th, 2004 07:53 AM
DataReader cjcd BOOK: Beginning ASP.NET 1.0 2 March 21st, 2004 01:06 PM



All times are GMT -4. The time now is 07:10 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.