Why am I getting this element null error?
I'm getting this error, not really sure why. Please help, this is urgent.
UPDATE
if (flights.Count() >= 1)
{
int count = flights.C开发者_JAVA技巧ount();
lblNumResults.Text = count.ToString();
gvAvailableFlights.DataSource = flights;
gvAvailableFlights.DataBind();
}
Server Error in '/WebSite3' Application. Value cannot be null. Parameter name: element 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.ArgumentNullException: Value cannot be null. Parameter name: element
Source Error:
The source code that generated this unhandled exception can only be shown when compiled in debug mode. To enable this, please follow one of the below steps, then request the URL:
Add a "Debug=true" directive at the top of the file that generated the error. Example:
<%@ Page Language="C#" Debug="true" %>
or:
2) Add the following section to the configuration file of your application:
Note that this second technique will cause all files within a given application to be compiled in debug mode. The first technique will cause only that particular file to be compiled in debug mode.
Important: Running applications in debug mode does incur a memory/performance overhead. You should make sure that an application has debugging disabled before deploying into production scenario.
Stack Trace:
[ArgumentNullException: Value cannot be null. Parameter name: element]
System.Xml.Linq.XElement.op_Explicit(XElement element) +116474 searchresult.b__1d(XElement f) +64 System.Linq.WhereSelectEnumerableIterator2.MoveNext() +151 System.Linq.Enumerable.Count(IEnumerable
1 source) +201 searchresult.FillAvailableFlightsGridView() +721 searchresult.Page_Load(Object sender, EventArgs e) +37 System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14 System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35 System.Web.UI.Control.OnLoad(EventArgs e) +91 System.Web.UI.Control.LoadRecursive() +74 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.1
In your code you have function called FillAvailableFlightsGridView
and in that function you call Count()
of some collection which is null.
Before calling the Count()
check if it's null and in such case, assume zero length or do whatever handling you want.
For example:
int myCount = 0;
if (myCollection != null)
myCount = myCollection.Count();
Edit: optimized code in your case would be:
int flightsCount = (flights == null) ? 0 : flights.Count();
if (flightsCount >= 1)
{
lblNumResults.Text = flightsCount.ToString();
gvAvailableFlights.DataSource = flights;
gvAvailableFlights.DataBind();
}
No need to call Count()
twice, as you saw it contains internal code that might be heavy.
Change this:
if (flights.Count() >= 1)
{
int count = flights.Count();
lblNumResults.Text = count.ToString();
gvAvailableFlights.DataSource = flights;
gvAvailableFlights.DataBind();
}
To this:
if (flights != null && flights.Count() >= 1)
{
lblNumResults.Text = (string)flights.Count();
gvAvailableFlights.DataSource = flights;
gvAvailableFlights.DataBind();
}
精彩评论