0

I am adding a list item in from an application page which is redirected from web part page.

My code is :

protected void Page_Load(object sender, EventArgs e)
    {
        SPSite site = new SPSite("http://bipc11:1234/sites/webparts");
        SPWeb web = site.RootWeb;
        web.AllowUnsafeUpdates = true;
        SPUser currentUser = web.CurrentUser;
        SPList list = web.Lists["Cart"];
        SPListItem item = list.AddItem();
        item["Picture"] = Request.QueryString["title"];
        Response.Write("<script> alert('Pictrue Updated')</script>");
        item["User"] = currentUser;
        Response.Write("<script> alert('User Updated')</script>");
        item.Update();
        Response.Write("<script> alert('Item Updated')</script>");
        web.Update();
        web.AllowUnsafeUpdates = false;
        Response.Write("<script> alert('Value Added To Cart Successfully')</script>");
        Response.Redirect(Microsoft.SharePoint.Utilities.SPUrlUtility.CombineUrl(web.Url, "/SiteAssets/WebPartPage.aspx"));

    }

Here field Picture is of type look up in Cart list. User is the type of person or group. Java script to redirect to the application page is:

function myfunction(title)
     {
        window.location.assign("http://bipc11:1234/sites/webparts/_layouts/AddValueToCartList/AddValueToCartList.aspx?title="+title);
     }

When the page is redirected to the application page I am getting above mentioned error.

What am I missing? Please help.

1

I am unable to find the exact reason for the error: However pls go through the following points:

1) How to update a Readonly field.

First set Readonly to false and update the field and then revert it back as:

Field.ReadOnlyField = false;
Field.Update();

2) Updating the Lookupfield:

new SPFieldLookupValue(1, "ASP.Net");
10
  • I made the changes as per your suggestion: SPList list = web.Lists["Cart"];list.Fields["User"].ReadOnlyField = false;list.Fields["Picture"].ReadOnlyField = false;SPListItem item = list.AddItem();item["Picture"] = Request.QueryString["title"];item["User"] = new SPFieldLookupValue(Request.QueryString["title"]);item.Update();
    – users1100
    Sep 20 '13 at 5:44
  • Now, error is dipslaying, Value does not fall within the expected range. at line item["User"]=new SPFieldLookupValue(Request.QueryString["title"]);
    – users1100
    Sep 20 '13 at 5:44
  • is it the same error ? why don't u test each field at one time. It will save and ur time and will help find the problem.
    – Pushpendra
    Sep 20 '13 at 5:46
  • No I debugged the program and started displaying error at mentioned line.
    – users1100
    Sep 20 '13 at 5:47
  • Browser displays the error "an unexpected error has occurred"
    – users1100
    Sep 20 '13 at 5:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.