I have a custom web part form which takes a few values from text boxes and adds the new row to a list. The problem is that when I click 'save', the values are being saved but some fields like 'Company', 'Priority', etc are being saved with a Comma appended to the end. For example: Company1 => Company1,

Why is the comma being added? Not sure if this is related but I noticed this started happening when I added a people picker control.

Any help would be appreciated.

Here is the code snippet -

        protected override void CreateChildControls()
            web = SPContext.Current.Web;


           PeoplePickerControl1 = new PeopleEditor();
            PeoplePickerControl1.AllowEmpty = true;
            PeoplePickerControl1.AllowTypeIn = true;


            PeoplePickerControl2 = new PeopleEditor();
            PeoplePickerControl2.AllowEmpty = true;
            PeoplePickerControl2.AllowTypeIn = true;


            dpProblemType = new DropDownList();
            SPList lookupList = web.Lists["Type"];
            SPListItemCollection items = lookupList.Items;

            foreach (SPListItem item in items)

            Label lblCompany = new Label();
            lblCompany.Text = "Company: ";
            tbCompany = new TextBox();

            btButtonSave = new Button();
            btButtonSave.Text = "Save";

            btButtonSave.Click += new EventHandler(btButtonSave_Click);

            SaveButton FormSaveButton = new SaveButton();
            FormSaveButton.ControlMode = SPControlMode.Edit;

        void btButtonSave_Click(object sender, EventArgs e)

            SPListItemCollection listItems = web.Lists["ProblemTicket"].Items;
            SPListItem pt = listItems.Add();

            string UsersSeperated1 = PeoplePickerControl1.CommaSeparatedAccounts;
            SPFieldUserValueCollection UserCollection1 = new SPFieldUserValueCollection();

            SPUser UserCreatedBy = web.SiteUsers[UsersSeperated1];
            SPFieldUserValue UserNameCreatedBy = new SPFieldUserValue(web, UserCreatedBy.ID, UserCreatedBy.LoginName);

            string UsersSeperated2 = PeoplePickerControl2.CommaSeparatedAccounts;
            SPFieldUserValueCollection UserCollection2 = new SPFieldUserValueCollection();

                SPUser UserCreatedFor = web.SiteUsers[UsersSeperated2];
                SPFieldUserValue UserNameCreatedFor = new SPFieldUserValue(web, UserCreatedFor.ID, UserCreatedFor.LoginName);

            pt["CreatedBy"] = UserCollection1.ToString();
            pt["CreatedFor"] = UserCollection2.ToString();
            pt["Company"] = tbCompany.Text;
            pt["Office"] = tbOffice.Text.ToString();
            pt["Phone Number"] = tbPhoneNumber.Text.ToString();
            pt["Date Created"] = tbDateCreated.Text.ToString();
            pt["Date Needed"] = tbDateNeeded.Text.ToString();
            pt["Problem Type"] = dpProblemType.SelectedIndex + 1;
            pt["Priority"] = tbPriority.Text.ToString();
            pt["Brief Description"] = tbBriefDescription.Text.ToString();

            web.AllowUnsafeUpdates = true;
            web.AllowUnsafeUpdates = false;

  • 1
    People picker control won't affect Text box control. You get comma appended to people picker value or text box value?
    – Suren
    Apr 26, 2013 at 5:40
  • @suren Not the people picker value, just the text box values. That is why I doubt that is the reason too. Also, There is no comma in the dates but the other fields like company, office, phone number, etc have it. Apr 26, 2013 at 5:54
  • Try changing PeoplePickerControl1 to some other name like peControl1. I think there is problem in adding controls to webpart. I used the same scenario with controls added in design page and rendered the values in code behind.
    – Suren
    Apr 26, 2013 at 6:06

1 Answer 1


I dont know why your getting the comma in the first place! you realy need to debug your code and see the varibles in real time to assess where its being placed!

a work around if its an issue and you cant fix it you can use this:

private string removeComma(string com)
    //return the last character of ,
    int index = value.LastIndexOf(",");

    //check to make sure its not the first and only the last and that its a comma
    if (com.Substring(index2) == "," && index != -1)
        com = com.TrimEnd(','); // remove comma

    return com;

if you want to remove any comma

private string removeComma(string com)
    com = com.Replace(",",""); // remove comma
    return com;

in your code then send any string through that function:

        pt["CreatedBy"] = removeComma(UserCollection1.ToString());
        pt["CreatedFor"] = removeComma(UserCollection2.ToString());
        pt["Company"] = removeComma(tbCompany.Text);
        pt["Office"] = removeComma(tbOffice.Text.ToString());
        pt["Phone Number"] = removeComma(tbPhoneNumber.Text.ToString());
        pt["Date Created"] = removeComma(tbDateCreated.Text.ToString());
        pt["Date Needed"] = removeComma(tbDateNeeded.Text.ToString());
        pt["Problem Type"] = dpProblemType.SelectedIndex + 1;
        pt["Priority"] = removeComma(tbPriority.Text.ToString());
        pt["Brief Description"] = removeComma(tbBriefDescription.Text.ToString());

If any string has a comma at the end it will remove it for you ;)

as for your code I have a feeling its doing at this line

string UsersSeperated1 = PeoplePickerControl1.CommaSeparatedAccounts;

debug on that line and see the values, then move on to see the values being appended from the textbox! Am i guessing correct that the textbox values are being populated by the user or by you(code)?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.