0

I was having SharePoint custom list.List name list1.

For storing Column names I was using another list list2

In product list I want to add hashtable values.Please someone help for this.

Here is my code :

enter code here  foreach (DataRow dr in dt.Rows)
                        {
                            Hashtable htValues = new Hashtable();
                            string ITEMNMBR = dr["ITEMNMBR"].ToString();
                            string ItemDesc = dr["Item Desc"].ToString();
                            SPQuery oQuery = new SPQuery();
                            //<Where><Eq><FieldRef Name='ITEMNMBR'/><Value Type='Text'>" + ITEMNMBR + "</Value></Eq></Where>";

                            oQuery.Query = "<Where><Eq><FieldRef Name='ITEMNMBR' /><Value Type='Text'>" + ITEMNMBR + "</Value></Eq></Where>";
                            SPListItemCollection oItemColl = olist.GetItems(oQuery);
                            SPListItem oItems = null;
                            if (oItemColl.Count > 0)
                            {
                                oItems = oItemColl[0];
                            }




                            if (oItems != null)
                            {

                                string strContractID = oItems.ID.ToString();
                                foreach (SPListItem items in sqlfields)
                                {
                                    string Sqlcolumns = items["SqlFields"].ToString();
                                    string econtractscolumns = items["eContractsFields"].ToString();
                                    if (dt.Columns.Contains(Sqlcolumns))
                                    {
                                        if (!htValues.Contains(econtractscolumns))
                                            htValues.Add(econtractscolumns, dr[Sqlcolumns]);
                                    }
                                    ///Update Contracts list witH SQL data's

                                }
                       //here how we can pass the htvalues to oitems.update
                                oItems.Update()
                                     }
                            else
                            {
                                Hashtable htnewValues = new Hashtable();
                                foreach (SPListItem items in sqlfields)
                                {
                                    string Productcolumns = items["SqlFields"].ToString();
                                    string econtractscolumns = items["eContractsFields"].ToString();
                                    if (dt.Columns.Contains(Productcolumns))
                                    {
                                        if (!htnewValues.Contains(econtractscolumns))
                                            htnewValues.Add(econtractscolumns, dr[Productcolumns]);
                                    }
                                }
                                SPListItem itemToAdd = list.Items.Add();
                       //here how can i pass the htnewvalues to itemtoadd
                                itemToAdd.Update();
}
}
1

I have updated your code to insert & update item using Dictionary<string, List<string>> instead of HashTable.

Kindly check it.

foreach (DataRow dr in dt.Rows)
{
    static Dictionary<string, string> htValues = new Dictionary<string, string>();
    string ITEMNMBR = dr["ITEMNMBR"].ToString();
    string ItemDesc = dr["Item Desc"].ToString();
    SPQuery oQuery = new SPQuery();
    //<Where><Eq><FieldRef Name='ITEMNMBR'/><Value Type='Text'>" + ITEMNMBR + "</Value></Eq></Where>";

    oQuery.Query = "<Where><Eq><FieldRef Name='ITEMNMBR' /><Value Type='Text'>" + ITEMNMBR + "</Value></Eq></Where>";
    SPListItemCollection oItemColl = olist.GetItems(oQuery);
    SPListItem oItems = null;
    if (oItemColl.Count > 0)
    {
        oItems = oItemColl[0];
    }




    if (oItems != null)
    {

        string strContractID = oItems.ID.ToString();
        foreach (SPListItem items in sqlfields)
        {
            string Sqlcolumns = items["SqlFields"].ToString();
            string econtractscolumns = items["eContractsFields"].ToString();
            if (dt.Columns.Contains(Sqlcolumns))
            {
                if (!htValues.Contains(econtractscolumns))
                    htValues.Add(econtractscolumns, Sqlcolumns);
            }
            ///Update Contracts list witH SQL data's

        }

        foreach (KeyValuePair<string, string> item in sourceList) {
           oItems.id = item.key;
           //you need to compare these values coming from dictionary to each item.
           oItems["item1"] = item.Value;
        }

        //here how we can pass the htvalues to oitems.update
        oItems.Update()

             }
    else
    {
        static Dictionary<string, string> htnewValuesValues = new Dictionary<string, string>();
        foreach (SPListItem items in sqlfields)
        {
            string Productcolumns = items["SqlFields"].ToString();
            string econtractscolumns = items["eContractsFields"].ToString();
            if (dt.Columns.Contains(Productcolumns))
            {
                if (!htnewValues.Contains(econtractscolumns))
                    htnewValues.Add(econtractscolumns, Productcolumns);
            }
        }
        foreach (KeyValuePair<string, string> item in sourceList) {
        SPListItem itemToAdd = list.Items.Add();
        itemToAdd.id = item.key;
           //you need to compare these values coming from dictionary to each item.
           itemToAdd["item1"] = item.Value;
        itemToAdd.Update();
        }
    }
}

Please let me know if any question.

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.