Does anyone see why this SharePoint code returns Null?
I keep getting a "Value cannot be null." Ive tried changing several things but just cant figure it out any help.
List<int> selecteditems = new List<int>();
int itemnum = 0;
double totalsiteitems1 = (Double)ViewState["NoFiles"];
for (int i = 1; i <= totalsiteitems1; i++)
{
itemnum = Int32.Parse(Request.Form["option" + i]);
selecteditems.Add(itemnum);
}
foreach (int pubitem in selecteditems)
{
SPSite oSite = new SPSite(site);
SPWeb oWeb = oSite.OpenWeb();
SPList oList = oWe开发者_JS百科b.Lists[list];
SPListItem oListItem = oList.Items.GetItemById(pubitem);
oListItem["_ModerationStatus"] = 0;
oListItem.Update();
}
- Be sure that
Request.Form["option" + i]
returns something and that it can be parsed to integer. - Also be sure that
ViewState["NoFiles"]
returns something.
I would suggest using TryParse
http://msdn.microsoft.com/en-us/library/system.int32.tryparse.aspx
if(Int32.TryParse(Request.Form["option" + i], out itemnum)) {
selecteditems.Add(itemnum);
}
精彩评论