Click here to Skip to main content
15,897,226 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a caml query in my code to return some items of SharePoint list. I added the IF statement in case if query not find any matching item or returns NULL.
C#
SPListItemCollection Items = RiskAssesment.GetItems(new SPQuery()
                  {
                      Query = @"<Where>
                                       <Eq>
                                          <FieldRef       Name='Department'/>
                                          <Value Type='Text'>"+Department+"</Value>                           </Eq></Where>"
                  });

                  foreach (SPListItem item in Items)
                  {
                      if (item != null)
                      {
                          item["Name"]="abcd";
                          item.Update();
                      }
                      else
                      {
                          newListItem["Name"] = "xyz";
                          newListItem.Update();
                      }
                  }

if it not finds Department in the list, it doesn't goes to ELSE statement.
Posted
Updated 11-Oct-12 7:24am
v2

1 solution

Did you tried that query with caml query builder? I think while giving that variable Department, it is giving some problem. Just remove that variable and include in the query as below.

XML
@"<Where>
         <Eq>
          <FieldRef       Name='Department'/>
          <Value Type='Text'>Department</Value>                                       </Eq>
</Where>"


Now verify the result is coming correctly or not. If not then entire query need to be changed. If yes, then the problem coming when we insert that variable into the query.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900