0
class Vote
    {
        public static void DoneVote(DataTable table)
        {
            string siteUrl = "...";    
            try
            {                   
                using (SPSite site = new SPSite(siteUrl))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        foreach (DataRow row in table.Rows)
                        {
                            SPList list = web.Lists.TryGetList("ListName");
                            if (list != null) {
                                SPListItem itemToAdd = list.AddItem();
                                itemToAdd["Title"] = "DA0000";
                                itemToAdd["IdProject"] = "10";
                                itemToAdd["DateOf"] = DateTime.Today;
                                itemToAdd["Type"] = "1";
                                itemToAdd["Project"] = "1";
                                itemToAdd.Update();
                            }
                        }    
                    }
                }
            }
            catch (Exception e)
            {
                //Log errors
                Console.WriteLine(e);
            }
        }

And I am making call like below:

Vote.DoneVote(table);

I havn't got any error, but itemToAdd not adding in ListName. When I debug it I see, what table has correct data.

3

Try adding web.AllowUnsafeUpdates = true; before for loop and then after for loop add web.AllowUnsafeUpdates = false;

So the code will be:

 try
            {
            using (SPSite site = new SPSite(siteUrl))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    web.AllowUnsafeUpdates = true;
                    SPList list = web.Lists.TryGetList("ListName");
                    foreach (DataRow row in table.Rows)
                    {

                        if (list != null) {
                            SPListItem itemToAdd = list.AddItem();
                            itemToAdd["Title"] = "DA0000";
                            itemToAdd["IdProject"] = "10";
                            itemToAdd["DateOf"] = DateTime.Today;
                            itemToAdd["Type"] = "1";
                            itemToAdd["Project"] = "1";
                            itemToAdd.Update();
                        }
                    }
                    web.AllowUnsafeUpdates = false;
                }
            }
        }
        catch (Exception e)
        {
            //Log errors
            Console.WriteLine(e);
        }
2

You should not update column ID as you have done in your code

Remove this line itemToAdd["ID"] = "10"; from your code

Entire code will be

class Vote
{
    public static void DoneVote(DataTable table)
    {
        string siteUrl = "...";
        try
        {
            using (SPSite site = new SPSite(siteUrl))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    foreach (DataRow row in table.Rows)
                    {
                        SPList list = web.Lists.TryGetList("ListName");
                        if (list != null) {
                            SPListItem itemToAdd = list.AddItem();
                            itemToAdd["Title"] = "DA0000";
                            itemToAdd["DateOf"] = DateTime.Today;
                            itemToAdd["Type"] = "1";
                            itemToAdd["Project"] = "1";
                            itemToAdd.Update();
                            Console.WriteLine(Convert.ToString(itemToAdd["ID"]));
                        }
                    }
                }
            }
        }
        catch (Exception e)
        {
            //Log errors
            Console.WriteLine(e);
        }
    }
}
  • thank for answer, but it doesn't work – DisguisePerceptron Mar 28 '16 at 8:55
  • 1
    debug the entire code line by line and see where it breaks or throws exception – Gaurravs Mar 28 '16 at 8:56
  • 1
    Its weird. Have you tried updated code? it should print the newly created ItemdID. While debugging check list.Items.Count once list object is created & not null. – Gaurravs Mar 28 '16 at 9:08
  • 1
    Can you write in more detail? What it created and not null, list object or item ?? If list object.. then what is "list.Items.Count" returning??? – Gaurravs Mar 28 '16 at 9:19
  • 1
    Also make sure your list view don't have any filters in it. Those may be preventing items to display. – P S Mar 28 '16 at 9:25

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.