none
Code Optimization Question RRS feed

  • Question

  • There must be a quicker way to achieve this.  I am needing to update my SharePoint list with CSOM and only update the information if it is different than the textbox value. I have roughly 15 text boxes and variables that I am needing to check, but this is the schema that I am following.



    private void Update()
        {
            bool updName = false;
            bool updAdd = false;
            bool updCity = false;
            bool updState = false;
            bool updZip = false;
    
            using (var ctx = new ClientContext(URL))
            {
                var web = ctx.Web;
                List list = web.Lists.GetByTitle(Site);
                CamlQuery caml = CamlQuery.CreateAllItemsQuery();
                ListItemCollection items = list.GetItems(caml);
                ctx.Load(items);
    
                string searchExpression = "ID = " + txtID.Text;
                DataRow[] foundRows = dtDataTableOne.Select(searchExpression);
    
                if (foundRows.Length >= 1)
                {
                    foreach (DataRow dr in foundRows)
                    {
                        if (txtName.Text != Convert.ToString(dr["Name"]))
                            updName = true;
                        if (txtAddress.Text != Convert.ToString(dr["Address"]))
                            updAdd = true;
                        if (txtCity.Text != Convert.ToString(dr["City"]))
                            updCity = true;
                        if (txtState.Text != Convert.ToString(dr["State"]))
                            updState = true;
                        if (txtZip.Text != Convert.ToString(dr["Zip"]))
                            updZip = true;
                    }
                    foreach (ListItem item in items)
                    {
                        if (updName)
                            item["xrx_Name_2000"] = txtName.Text;
                        if (updAdd)
                            item["xrx_Address_Main_x2"] = txtAddress.Text;
                        if (updCity)
                            item["xrx_City_Main_X2"] = txtCity.Text;
                        if (updState)
                            item["xrx_State_rza1" = txtState.Text;
                        if (updZip)
                            item["zip"] = txtZip.Text;
    
                        item.Update();
                    }
    
                    ctx.ExecuteQuery();
                }
            }


    Wednesday, May 30, 2018 11:57 PM

All replies

  • Hi  IndigoMontoya,

    For your question of code optimization, firstly you should check whether this block of codes can work properly. Then it is necessary for you to provide comments for the codes in detail so that others can understand what you want to do and give advice. Also, a simple demo is appreciated.

    Besides, in the two statements below, what do the txtID and dtDataTableOne separately represent and their functions? And what role do they play in this piece of codes?

                string searchExpression = "ID = " + txtID.Text;
                DataRow[] foundRows = dtDataTableOne.Select(searchExpression);

    Dolen


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, June 1, 2018 9:37 AM
  • Convert.ToString() could be problem. It is unnecessary to use Convert.ToString if you can use (string) casting or you could type dr["Name"].ToString(). 

    If you want to use (string) casting there could be null value as result or exception could be thrown. You must be sure that there will be null value or valid string value. If there will be int value in column invalid cast exception will be thrown. 

    If you want to use ToString() method, you need to check if column value is not null. 

    dr["Name"]==DbNull.Value? string.Empty : dr["Name"].ToString();

    Believe or not (string) cast or use ToString() method with check to null value is faster than using Convert.ToString() method.

    Friday, June 1, 2018 6:48 PM
  • FYI: You don't have to check for DbNull.Value, since dr["Name"].ToString() returns an empty string if the value is DbNull.Value.

    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Saturday, June 2, 2018 4:23 PM
    Moderator
  • Let me elaborate on my last reply. Basically change this:

    if (txtName.Text != Convert.ToString(dr["Name"]))

    to this:

    if (txtName.Text != dr["Name"].ToString())


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Saturday, June 2, 2018 4:26 PM
    Moderator
  • You are right. 
    Saturday, June 2, 2018 5:03 PM
  • Hi  IndigoMontoya,

    For your question of code optimization, firstly you should check whether this block of codes can work properly. Then it is necessary for you to provide comments for the codes in detail so that others can understand what you want to do and give advice. Also, a simple demo is appreciated.

    Besides, in the two statements below, what do the txtID and dtDataTableOne separately represent and their functions? And what role do they play in this piece of codes?

                string searchExpression = "ID = " + txtID.Text;
                DataRow[] foundRows = dtDataTableOne.Select(searchExpression);

    Dolen


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Hello - thank you for your reply.  In your above snippet, I am finding a specific row in the data table and "selecting" all the data for that row, so I can check and/or manipulate the data.
    Monday, June 11, 2018 2:44 PM
  • Hi Indigo,

    Could you tell us what is your UI? WinForm, WPF, Web?


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Wednesday, June 13, 2018 3:24 PM
    Moderator