I have some code which simply gets a list, creates a new item with the appropriate fields, and adds the item to the list. The code worked perfect in the dev and UAT environments but since we deployed it to production it has stopped working.
The overview of how it was set up is a user fills out a form on the page and an ajax call sends the information to a code behind. Then the code behind creates a new item and adds that to the list.
This is the template i am using for the code:
try
{
using (SPSite site = new SPSite(SPContext.Current.Web.Url))
{
using (SPWeb web = site.OpenWeb())
{
web.AllowUnsafeUpdates = true;
SPList list = web.Lists["List Name"];
SPListItem itemToAdd = list.Items.Add();
itemToAdd["Title"] = "Some Value";
itemToAdd.Update();
web.AllowUnsafeUpdates = false;
}
}
}
The dev servers are 100% on an intranet. The prod server is in the intranet, but has alternate access mappings to an extranet. The code does not execute when attempting from either though.
Does anyone have an idea for this situation?
Thanks
Solved it,
The list i was referencing in production had been altered by someone while i was working in dev. One of the column names was changed and i did not see it because there were 20 or so fields.
Thanks Dave Wise for the advice on the rootweb/openroot. I was not aware of the differences so you have saved me many future headaches.
Thanks Thantos for recommending the logging to the ULS logs, that was how i was able to discover the error.
using()
block on the 'web' variable but are getting its value value viasite.RootWeb
instead of the .OpenWeb() (which is when a using() is required) RootWeb merely returns an handle to an existing object and by using it in this manner you are destroying the .RootWeb object before page execution completes which could cause any number of issues for downstream code