protected override void CreateChildControls()
{
using (SPSite site = new SPSite(SPContext.Current.Web.Url))
{
using (SPWeb web = site.OpenWeb())
{
SPContentType contype=site.RootWeb.ContentTypes["SYFLocation"];
IList
foreach (SPContentTypeUsage uses in usages)
{
if (uses.IsUrlToList)
{
SPList list = web.GetList(uses.Url);
SPQuery qry = new SPQuery();
qry.Query = "
"
"
"
"
SPListItemCollection itemcoll = list.GetItems(qry);
HtmlGenericControl ulTab = new HtmlGenericControl("ul");
using (ulTab)
{
foreach (SPListItem item in itemcoll)
{
if (itemcoll.Count > 0)
{
var containerDiv = new HtmlGenericControl("div");
HtmlControl liItem = BindHtml(item);
ulTab.Controls.Add(liItem);
containerDiv.Controls.Add(ulTab);
this.Controls.Add(containerDiv);
}
else
{
HtmlGenericControl errorDiv = new HtmlGenericControl("div");
HtmlGenericControl h2 = new HtmlGenericControl("h2");
h2.InnerText = "There are no items in Calender list";
errorDiv.Controls.Add(h2);
Controls.Add(errorDiv);
}
}
}
}
}
}
}
}
private HtmlControl BindHtml(SPListItem item)
{
HtmlGenericControl liItem = new HtmlGenericControl("li");
HtmlAnchor link = new HtmlAnchor();
string url = item["URL"] != null && item["URL"].ToString() != string.Empty ? item["URL"].ToString() : "#";
string[] urls = url.Split(',');
link.HRef = urls[0].Trim(' ');
link.Target = "blank";
link.InnerText = item["Title"] != null && item["Title"].ToString() != string.Empty ? item["Title"].ToString() : "";
link.InnerHtml = item["SYFLocation"] != null && item["SYFLocation"].ToString() != string.Empty ? item["SYFLocation"].ToString() : "";
liItem.Controls.Add(link);
return liItem;
}
}
when i deploy web part after when i try to use webpart on oage it display error
faceing error like:
Server Error in '/' Application.
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.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. |
Stack Trace:
|
Version Information: Microsoft .NET Framework Version:2.0.50727.5485; ASP.NET Version:2.0.50727.5483
Prasham SabadraPosted Mar 6, 2015, 12:14 AM
I would debug the code first and will check on which line I am getting error. Which object getting null. Please try to debug once.
If still problem persist then, please share more details.
sumit kumarPosted Mar 2, 2015, 5:55 AM
Please try like this creating SPSite and SPWeb objects:
SPSite site = SPContext.Current.Site;
SPWeb web = site.RootWeb;
Disposing SPSite object creates problem as you have used in using statement