 |
| 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 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
|
|
|
|

February 13th, 2009, 06:27 AM
|
|
Authorized User
|
|
Join Date: May 2007
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|
|

February 13th, 2009, 07:11 AM
|
|
|
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()
|
|

February 13th, 2009, 08:03 AM
|
|
Authorized User
|
|
Join Date: May 2007
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

February 13th, 2009, 08:07 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
|
|
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.
================================================== =========
|
|

February 13th, 2009, 08:33 AM
|
|
Authorized User
|
|
Join Date: May 2007
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|
|

February 13th, 2009, 08:42 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
|
|
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.
================================================== =========
|
|

February 13th, 2009, 09:39 AM
|
|
Authorized User
|
|
Join Date: May 2007
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

February 13th, 2009, 09:44 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
|
|
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.
================================================== =========
|
|

February 13th, 2009, 09:48 AM
|
|
Authorized User
|
|
Join Date: May 2007
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
problem in displaying null value to the control in the footer template of gridview
am using sql server 2008.
|
|

February 13th, 2009, 10:44 AM
|
 |
Wrox Author
|
|
Join Date: Jan 2008
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
|
|
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.
|
|
 |