Click here to Skip to main content
15,937,127 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Click the Link to View

I have a page with User picker control which can take more than one names and filter out on click of Filter button for those particular entered names in UserPicker but it is filtering out one by one using dataview in the below code , I have a written the code but while looping it is taking the last name and displaying the records.

In this case how to Append a row for the given existing dataview.

What I have tried:

IEnumerator users = UserPicker.ResolvedEntities.GetEnumerator();
              while (users.MoveNext())
                  SPUser selectedUser = web.EnsureUser(((Microsoft.SharePoint.WebControls.PickerEntity)(users.Current)).Key);
                  string strVendorUserName = selectedUser.LoginName.ToString();

                  if (strVendorUserName != null || ddlPermissionType.SelectedItem != null)
                      StringBuilder sb = new StringBuilder(string.Format("UserLoginName = '{0}'", strVendorUserName.Trim()));

                      dv.RowFilter = sb.ToString();

                      return dv;

Updated 5-Sep-17 6:57am

1 solution

Declare the StringBuilder class before the while loop. Combine multiple filters with OR. Assign the RowFilter and return the result after the while loop.

You should also be able to use a foreach loop, rather than working directly with the IEnumerator interface.
var sb = new StringBuilder();

foreach (Microsoft.SharePoint.WebControls.PickerEntity item in UserPicker.ResolvedEntities)
    SPUser selectedUser = web.EnsureUser(item.Key);
    string strVendorUserName = selectedUser.LoginName;
    if (strVendorUserName != null || ddlPermissionType.SelectedItem != null)
        if (sb.Length != 0) sb.Append(" OR ");
        sb.AppendFormat("UserLoginName = '{0}'", strVendorUserName.Trim()); // <-- Possible NullReferenceException here!

dv.RowFilter = sb.ToString();
return dv;

NB: Looking at your if block, you believe that LoginName could return null. But if it does, and ddlPermissionType.SelectedItem is not null, then strVendorUserName.Trim() will fail with a NullReferenceException.
Share this answer
jags@212 6-Sep-17 5:31am    
Null reference exception was thrown , isn't there any way like we there is one row coming post dataview row filter , if we can capture one row into datatable and append subsequent row in datatable which will be coming from indivisual dataview row filter then that can serve the purpose.
Richard Deeming 6-Sep-17 11:22am    
As I explained in my answer, you'll get a NullReferenceException if selectedUser.LoginName returns null, and ddlPermissionType.SelectedItem is not null.

Since you don't seem to be using the ddlPermissionType value anywhere, just remove that check from the if block.

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