_site = new SPSite("http:\\MySite");
_web = site.OpenWeb();
{
list = _web.Lists[sListName];
_web.AllowUnsafeUpdates = true;
items = list.Items;
item = items.Add();
item["Title"] = "new Title";
item["UserName"] = CurrentUser.ToString();
item["Configuration"] = sConfiguration.ToString();
}
item.Update();
_web.AllowUnsafeUpdates = false;
views:
247answers:
2
+1
A:
You could get ArgumentNullException if
- CurrentUser is null
- sConfiguration is null
And you must write code like this:
using (_site = new SPSite("http:\\MySite")) //Disposing correctly to prevent memory leaks
using (_web = _site.OpenWeb())
{
try {
list = _web.Lists[sListName]; //SPException may be thrown if sListName does not exist
_web.AllowUnsafeUpdates = true;
items = list.Items;
item = items.Add(); //Before doing this, check if you have permissions with list.DoesUserHavePermissions to add items to prevent exception here
item["Title"] = "new Title";
item["UserName"] = CurrentUser.ToString(); //Not sure which, but some exception may be thrown if such field does not exist and ArgumentNullException may be thrown if CurrentUser is null
item["Configuration"] = sConfiguration.ToString(); //ArgumentNullException may be thrown
item.Update();
}
finally {
_web.AllowUnsafeUpdates = false;
}
}
Janis Veinbergs
2010-04-08 06:40:10
That's not an answer to the original question. You should have posted it as a comment to Janis's answer.
Flo
2010-04-08 07:52:17
how come its not an answer?
Shoban
2010-04-08 08:33:13
Weird, but is that code compileable then?
Janis Veinbergs
2010-04-08 10:06:46