Avatar of mshaji
mshajiFlag for India

asked on 

Invalid data has been used to update the list item. The field you are trying to update may be read only.


I have SharePoint blog and I created a custom web part to add the comments for a new post. But when i add comment item by using this web part then its giving an error "Invalid data has been used to update the list item. The field you are trying to update may be read only."

Please see the attached code here

Does any one have an Idea!

regards
string DURL = "http://myURL/";
                        using (SPSite site = new SPSite(DURL))
                        {
                            using (SPWeb web = site.OpenWeb())
                            {
                                web.AllowUnsafeUpdates = true;
                                SPList listLook = web .Lists ["Posts"];
                                SPList list = web.Lists["Comments"];
                                SPListItem Item1 = list.Items.Add();
                                Item1["Title"] = "My testing title";
                               
                                Item1.Fields["Post Id"].ReadOnlyField = false;
                                Item1["Post Id"] = 1;
                                Item1.Update();
                                Item1.Fields["Post Title"].ReadOnlyField = true;
                                Item1.Fields["Post Id"].ReadOnlyField = true; 
                            }
                        }

Open in new window

Microsoft SharePoint

Avatar of undefined
Last Comment
mshaji
Avatar of GeorgeGergues
GeorgeGergues

I think you are misusing the list update and item update

check the changes



  string DURL = "http://myURL/";
                        using (SPSite site = new SPSite(DURL))
                        {
                            using (SPWeb web = site.OpenWeb())
                            {
                                web.AllowUnsafeUpdates = true;
                                SPList listLook = web .Lists ["Posts"];

                                SPList list = web.Lists["Comments"];
                                SPListItem Item1 = list.Items.Add();
                                Item1["Title"] = "My testing title";
                               
                                list.Fields["Post Id"].ReadOnlyField = false;
                                list.Update();
                                Item1["Post Id"] = 1;
                                Item1.Update();
                                
                                list.Fields["Post Title"].ReadOnlyField = true;
                                list.Fields["Post Id"].ReadOnlyField = true; 
                                list.Update();

                            }
                        }

Open in new window




but I think you are going at this the wrong way..


in any case ,let me know if that worked.
ASKER CERTIFIED SOLUTION
Avatar of mshaji
mshaji
Flag of India image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of mshaji
mshaji
Flag of India image

ASKER

no
Microsoft SharePoint
Microsoft SharePoint

Microsoft Sharepoint is a software platform and family of software products used for collaboration and web publishing combined. These capabilities include developing web sites, portals, intranets, content management systems, search engines, wikis, blogs, and other tools for business intelligence and collaboration. SharePoint has a Microsoft Office-like interface, and it is closely integrated with the Office suite.

40K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo