using webpart upload image in sharepoint 2013 RRS feed

  • Question

  • Issue:

    i am trying to add items into sharepoint list using webpart ( custom code ).

    its giving me error :

    An exception of type 'Microsoft.SharePoint.SPException' occurred in Microsoft.SharePoint.dll but was not handled in user code

    Additional information: The data is invalid. (Exception from HRESULT: 0x8007000D).

    My code is :


     private bool InsetInAdsList()
                bool result = true;
                    //using (SPWeb web = SPContext.Current.Web)
                        SPWeb web = SPContext.Current.Web;

                        SPList list = web.Lists.TryGetList("Ads");
                        if (list != null)
                            SPListItem NewItem = list.Items.Add();
                                web.AllowUnsafeUpdates = true;
                                NewItem["Title"] = txtTitle.Text;
                                NewItem["Category"] = ddlCategory.SelectedItem.Value;
                                NewItem["City"] = ddlCity.SelectedItem.Value;
                                NewItem["Price"] = Double.Parse(txtPrice.Text);
                                NewItem["Contact"] = txtContact.Text;
                                NewItem["Description"] = txtDescription.Text;
                                ltrAlert.Text = "Registration Successful";

                catch (Exception ex)
                    result = false;
                    ltrAlert.Text = ex.Message;

                return result;


    Guide me to resolve this 


    Thursday, January 19, 2017 7:54 AM

All replies

  • Hi qasimidl,

    Are you attaching some document as well while adding the item? What is the code that you are using inside AddAttachments(NewItem) method. Can you please run your code after commenting AddAttachments(NewItem) and check whether item gets added or not.

    Additionally check your fields type as well and make sure you passing the correct data.


    Thursday, January 19, 2017 8:43 AM
  • AddAttachments code


    private void AddAttachments(SPListItem NewItem)

                    HttpFileCollection uploadedFiles = HttpContext.Current.Request.Files;

                    for (int i = 0; i < uploadedFiles.Count; i++)
                        HttpPostedFile file = uploadedFiles[i];
                        byte[] fileData = null;
                        using (var binaryReader = new BinaryReader(file.InputStream))
                            fileData = binaryReader.ReadBytes(file.ContentLength);
                        string fileName = file.FileName;
                        NewItem.Attachments.Add(fileName, fileData);


    After commenting this method its working good .

    Thursday, January 19, 2017 9:46 AM
  • Hi qasimidl,

    I suggest you can debug the code snippet to check in which function the error exception thrown:

    How to debug Web Part

    A demo about creating new list item with attachment would be helpful:

    Insert new item with attachment to sharepoint list programmatically


    Best Regards

    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact

    Friday, January 20, 2017 7:42 AM