locked
How to set and read value of Yes/No column programmatically? RRS feed

  • Question

  • Hello

    I have 1 checkbox field as Yes/No field. I need to do following action using programmatically?

    1. How do I save check box value in the list if Checkbox is selected or not selected in a customized aspx form

    2. How to retrieve Yes/No column value from sharepoint list and set in a customized aspx form

    Monday, May 17, 2010 7:59 PM

Answers

  • Below is some example code that shows how to convert the value of a yesno SharePoint field to a bool value, and some code on how to set the value.

     public static void GetFieldBoolean()
     {
    
          using (SPSite site = new SPSite("http://basesmcdev2/sites/tester1"))
          {
            using (SPWeb web = site.OpenWeb())
            {
    
              //get field value
              SPListItem listItem = web.Lists["tester4"].GetItemById(99);
              SPFieldBoolean boolField = listItem.Fields["yesnofield"] as SPFieldBoolean;
              bool CheckBoxValue = (bool)boolField.GetFieldValue(listItem["yesnofield"].ToString());
    
              //set field value
              listItem["yesnofield"] = true;
              listItem.Update();
    
            
            }      
          
    }
        
        
        
        }

    certdev.com
    • Marked as answer by Lenzar Tuesday, May 18, 2010 7:28 PM
    Tuesday, May 18, 2010 2:57 AM
  • In addition to Steve Replies, you can use following code

    To Retriev the value you can use following code

                if (yourListItem["CheckBoxFieldName"].ToString() == "True")
                {
                    checkBox1.Checked = true;
                }
                else
                {
                    checkBox1.Checked = false;
                }


    To Set the value you can use following code

               if (checkBox1.Checked == false)
                {
                    item["CheckBoxFieldName"] = "No";
                }
                else
                {
                    item["CheckBoxFieldName"] = "Yes";
                }


    Ashish Kanoongo, MCP, MCSD, MCTS
    • Proposed as answer by .Net Frenzy Tuesday, May 18, 2010 11:51 AM
    • Marked as answer by Lenzar Tuesday, May 18, 2010 7:28 PM
    Tuesday, May 18, 2010 3:16 AM

All replies

  • Lenzar,

             for Yes/No field,

    the Value for Yes = 1

    No = 0

    Please use the as usual way of reading/writing the value to the list.

    PS:Didnt get a chance to try in code, as of now out of my dev env.

    Thanks,
    ~Uday, http://www.mstechblogs.com/sharepoint

    Tuesday, May 18, 2010 2:20 AM
  • Below is some example code that shows how to convert the value of a yesno SharePoint field to a bool value, and some code on how to set the value.

     public static void GetFieldBoolean()
     {
    
          using (SPSite site = new SPSite("http://basesmcdev2/sites/tester1"))
          {
            using (SPWeb web = site.OpenWeb())
            {
    
              //get field value
              SPListItem listItem = web.Lists["tester4"].GetItemById(99);
              SPFieldBoolean boolField = listItem.Fields["yesnofield"] as SPFieldBoolean;
              bool CheckBoxValue = (bool)boolField.GetFieldValue(listItem["yesnofield"].ToString());
    
              //set field value
              listItem["yesnofield"] = true;
              listItem.Update();
    
            
            }      
          
    }
        
        
        
        }

    certdev.com
    • Marked as answer by Lenzar Tuesday, May 18, 2010 7:28 PM
    Tuesday, May 18, 2010 2:57 AM
  • In addition to Steve Replies, you can use following code

    To Retriev the value you can use following code

                if (yourListItem["CheckBoxFieldName"].ToString() == "True")
                {
                    checkBox1.Checked = true;
                }
                else
                {
                    checkBox1.Checked = false;
                }


    To Set the value you can use following code

               if (checkBox1.Checked == false)
                {
                    item["CheckBoxFieldName"] = "No";
                }
                else
                {
                    item["CheckBoxFieldName"] = "Yes";
                }


    Ashish Kanoongo, MCP, MCSD, MCTS
    • Proposed as answer by .Net Frenzy Tuesday, May 18, 2010 11:51 AM
    • Marked as answer by Lenzar Tuesday, May 18, 2010 7:28 PM
    Tuesday, May 18, 2010 3:16 AM
  • @Steve, I used your code, it is giving me error for getting the value in Line 2

    Object reference not set to an instance of an object.

    I am using followingcode, let meknow what I am missing

                SPFieldBoolean boolField = retrieveItem.Fields["Active"] as SPFieldBoolean;
                bool CheckBoxValue = (bool)boolField.GetFieldValue(retrieveItem["Active"].ToString());

    @Ashish, I will test your code later. Let me test Steve suggestion first.

    Tuesday, May 18, 2010 3:45 AM
  • Make sure retrieveItem["Active"] is not null before calling retrieveItem["Active"].ToString()

    bool CheckBoxValue = false;

    if(retrieveItem["Active"] != null)

     CheckBoxValue = (bool)boolField.GetFieldValue(retrieveItem["Active"].ToString());


    certdev.com
    Wednesday, May 19, 2010 3:05 AM
  • Hi,

    Please look at the below code screen shot, what I tried. Its working for me.


    Sekar - Our life is short, so help others to grow

    Whenever you see a reply and if you think is helpful, click "Alternate TextVote As Helpful"! And whenever you see a reply being an answer to the question of the thread, click "Alternate TextMark As Answer

    • Proposed as answer by SP1013Dev Sunday, November 4, 2012 6:37 PM
    Sunday, November 4, 2012 6:16 PM
  • Depending on the length of the list,

    oListItem = oList.GetItemById(intId)

    may not be safe, at least in MOSS 2007.  We've noticed extremely long waits for getting values using that.

    This is longer and from memory and based on SekThang's answer, but it's better to use CAML, and an SPQuery object to get the SPListItem:

    SPQuery query = new SPQuery();

    query.Query = "<Where><Eq><FieldRef Name='ID' /><Value Type='Integer'>"+intId+"</Value></Eq></Where>";

    query.RowLimit = 1;

    SPListItemCollection splic = oList.GetItems(query);

    if (splic.Count > 0)

    {

    foreach (SPListItem oListItem in splic)

    {

    oListItem["IsActive"] = true;

    oListItem.Update();

    }

    }





    • Edited by navyjax2 Thursday, September 25, 2014 2:04 AM
    Thursday, September 25, 2014 2:01 AM