Wrox Programmer Forums

Need to download code?

View our list of code downloads.

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

You are currently viewing the ASP.NET 3.5 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
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 13th, 2009, 06:27 AM
Authorized User
 
Join Date: May 2007
Location: Dar es Salaam, , Tanzania.
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to jazzydonald
Default problem in displaying null value to the control in the footer template of gridview

Hi folks!
I am developing a web application in which I have a gridview in which there is footer template where i've put 3 labels that displays the sum of the values in their corresponding columns after retrieving data from the database. The sum of values are displayed correctly when there's some records retrieved from the database, but it doesnt work when there are no records retrieved from the db i get an erros tha says

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Here is my code:

Sub GetTotals()
Dim conn As SqlConnection = New SqlConnection(strConn)
Dim cmd As SqlCommand = New SqlCommand
cmd.CommandText = "select ISNULL(SUM(amount_taken),0.00) as 'Amount_Taken', ISNULL(SUM(loan_Installment),0.00) as 'loan_Installment',ISNULL(SUM(loan_balance),0.00) as 'loan_balance' from Loans where employee_no=@Employee_no"
cmd.Connection = conn
cmd.CommandType = CommandType.Text
cmd.Parameters.AddWithValue("@Employee_no", Session("Employee_no"))
conn.Open()
cmd.ExecuteNonQuery()
Dim MyReader As SqlDataReader
MyReader = cmd.ExecuteReader
MyReader.Read()

'The error occurs here when trying to get the controls in the footer template
Dim Total_AmountTaken As Label = CType(GridViewloans.FooterRow.FindControl("lblTotAmountTaken"), Label)
Dim Total_LoanInstallment As Label = CType(GridViewloans.FooterRow.FindControl("lblTotMonthlyInstallment"), Label)
Dim Total_LoanBalance As Label = CType(GridViewloans.FooterRow.FindControl("lblTotLoanBalance"), Label)

Total_LoanInstallment.Text = String.Format("{0:#,###.00}", MyReader("Loan_Installment")).ToString
Total_LoanBalance.Text = String.Format("{0:#,###.00}", MyReader("Loan_Balance")).ToString
Total_AmountTaken.Text = String.Format("{0:#,###.00}", MyReader("amount_taken")).ToString
conn.Close()
cmd.Dispose()
MyReader.Close()
EndSub

As you can from the sql statement i tried to use the ISNULL() just the default values when the it is null but still it does not work.

I hope i've describe my problem clearly
Please somebody help me.
  #2 (permalink)  
Old February 13th, 2009, 07:11 AM
jomet
Guest
 
Posts: n/a
Default

Try like this

Code:
Dim MyReader As SqlDataReader
MyReader = cmd.ExecuteReader

 
'The error occurs here when trying to get the controls in the footer template
Dim Total_AmountTaken As Label = CType(GridViewloans.FooterRow.FindControl("lblTotAmountTaken"), Label)
Dim Total_LoanInstallment As Label = CType(GridViewloans.FooterRow.FindControl("lblTotMonthlyInstallment"), Label)
Dim Total_LoanBalance As Label = CType(GridViewloans.FooterRow.FindControl("lblTotLoanBalance"), Label)

If MyReader.Read() Then
Total_LoanInstallment.Text = String.Format("{0:#,###.00}", MyReader("Loan_Installment")).ToString
Total_LoanBalance.Text = String.Format("{0:#,###.00}", MyReader("Loan_Balance")).ToString
Total_AmountTaken.Text = String.Format("{0:#,###.00}", MyReader("amount_taken")).ToString
End If

conn.Close()
cmd.Dispose()
MyReader.Close()
  #3 (permalink)  
Old February 13th, 2009, 08:03 AM
Authorized User
 
Join Date: May 2007
Location: Dar es Salaam, , Tanzania.
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to jazzydonald
Default

Thanks for your generous reply but your solution did no work!! am stillgetting th error "Object reference not set to an instance of an object" , "Null reference exception was unhandled by the user code"

Thanks
  #4 (permalink)  
Old February 13th, 2009, 08:07 AM
Friend of Wrox
Points: 7,395, Level: 36
Points: 7,395, Level: 36 Points: 7,395, Level: 36 Points: 7,395, Level: 36
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Capital Federal, , Argentina.
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

If the error occurs when you are trying to get the footer controls, maybe they are not rendered yet??? can you debug your code and see if the grid can really find the controls??
__________________
HTH

Gonzalo


================================================== =========
Read this if you want to know how to get a correct reply for your question.
(Took that from Doug signature and he Took that from Peter profile)
================================================== =========
My programs achieved a new certification :
WORKS ON MY MACHINE
================================================== =========
I know that CVS was evil, and now i got the
proof.
================================================== =========
  #5 (permalink)  
Old February 13th, 2009, 08:33 AM
Authorized User
 
Join Date: May 2007
Location: Dar es Salaam, , Tanzania.
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to jazzydonald
Default problem in displaying null value to the control in the footer template of gridview

thanks gbiachi for the reply. I debuged the code and find out that the grid can only find the controls only when my datareader can read data from the db i.e when records are found. but the grid can not find the controls when no records are returned after exeuting my command.
  #6 (permalink)  
Old February 13th, 2009, 08:42 AM
Friend of Wrox
Points: 7,395, Level: 36
Points: 7,395, Level: 36 Points: 7,395, Level: 36 Points: 7,395, Level: 36
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Capital Federal, , Argentina.
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

Ok, we found the problem them.

to try to solve it.. what if you invert the isnull in the SQL ?? (use SUM(ISNULL(field,0)) )
__________________
HTH

Gonzalo


================================================== =========
Read this if you want to know how to get a correct reply for your question.
(Took that from Doug signature and he Took that from Peter profile)
================================================== =========
My programs achieved a new certification :
WORKS ON MY MACHINE
================================================== =========
I know that CVS was evil, and now i got the
proof.
================================================== =========
  #7 (permalink)  
Old February 13th, 2009, 09:39 AM
Authorized User
 
Join Date: May 2007
Location: Dar es Salaam, , Tanzania.
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to jazzydonald
Default problem in displaying null value to the control in the footer template of gridview

Still It does not work when i try to invert the sql as you suggested, I get the same error.
What i was thinking is to find a way of assigning 0 value or default values in the labels in the footer template of the gridview if no records are returned from the database.

Last edited by jazzydonald; February 13th, 2009 at 09:43 AM.. Reason: Missing some point to clarify
  #8 (permalink)  
Old February 13th, 2009, 09:44 AM
Friend of Wrox
Points: 7,395, Level: 36
Points: 7,395, Level: 36 Points: 7,395, Level: 36 Points: 7,395, Level: 36
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Capital Federal, , Argentina.
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

Are you using sql server or access?
__________________
HTH

Gonzalo


================================================== =========
Read this if you want to know how to get a correct reply for your question.
(Took that from Doug signature and he Took that from Peter profile)
================================================== =========
My programs achieved a new certification :
WORKS ON MY MACHINE
================================================== =========
I know that CVS was evil, and now i got the
proof.
================================================== =========
  #9 (permalink)  
Old February 13th, 2009, 09:48 AM
Authorized User
 
Join Date: May 2007
Location: Dar es Salaam, , Tanzania.
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to jazzydonald
Default problem in displaying null value to the control in the footer template of gridview

am using sql server 2008.
  #10 (permalink)  
Old February 13th, 2009, 10:44 AM
Lee Dumond's Avatar
Wrox Author
Points: 4,942, Level: 29
Points: 4,942, Level: 29 Points: 4,942, Level: 29 Points: 4,942, Level: 29
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2008
Location: Decatur, IL, USA.
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

This really has nothing to do with how you are retrieving data from the database. It has to do with the fact that the footer row will not render when there are no records found.

You need to set up an EmptyDataTemplate. The GridView shows the EmptyDataTemplate whenever no data is returned. Then, you can put anything you want in there. You could even hard-code "$0.00" labels if you want to.
__________________
Visit my blog at http://leedumond.com
Follow me on Twitter: http://twitter.com/LeeDumond

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}
 


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
Totaling columns in a footer row of a GridView psimonson ASP.NET 2.0 Basics 1 May 5th, 2008 01:34 PM
How to Bind DDL in footer template? divekar.vishal ASP.NET 2.0 Basics 0 May 2nd, 2008 11:02 PM
Image Button as Template Field in GridView control vbmazza ASP.NET 2.0 Professional 10 May 27th, 2007 10:03 AM
display sum in footer of gridview sarah lee ASP.NET 1.0 and 1.1 Basics 3 December 27th, 2006 12:36 PM



All times are GMT -4. The time now is 08:29 PM.


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