Wrox Programmer Forums
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 9th, 2007, 04:41 AM
Authorized User
 
Join Date: Nov 2006
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????
 
Old April 9th, 2007, 05:19 AM
Friend of Wrox
 
Join Date: Aug 2004
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.
 
Old April 10th, 2007, 12:18 AM
Authorized User
 
Join Date: Nov 2006
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to akhilhp
Default

Thanks a lot..
It has worked...
 
Old October 8th, 2007, 03:05 PM
Sjo Sjo is offline
Registered User
 
Join Date: Oct 2007
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?



 
Old October 8th, 2007, 03:10 PM
Wrox Author
 
Join Date: Oct 2005
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
================================================== =========
 
Old October 8th, 2007, 03:18 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 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
 
Old October 8th, 2007, 03:25 PM
Wrox Author
 
Join Date: Oct 2005
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
================================================== =========
 
Old October 8th, 2007, 03:29 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 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
 
Old October 8th, 2007, 03:34 PM
Wrox Author
 
Join Date: Oct 2005
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
================================================== =========
 
Old October 8th, 2007, 03:40 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 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





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





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