Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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;

            base.CreateChildControls(); 

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


            Controls.Add(PeoplePickerControl1);

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

            Controls.Add(PeoplePickerControl2);

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

            foreach (SPListItem item in items)
            {
                dpProblemType.Items.Add(item.Name);
            }
            Controls.Add(dpProblemType);

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

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

            btButtonSave.Click += new EventHandler(btButtonSave_Click);



            SaveButton FormSaveButton = new SaveButton();
            FormSaveButton.ControlMode = SPControlMode.Edit;
            Controls.Add(FormSaveButton);
        }

        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();

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





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

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



            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;
            pt.Update();
            web.AllowUnsafeUpdates = false;
        }

    }
}
share|improve this question
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 '13 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. –  Ayrton Senna Apr 26 '13 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 '13 at 6:06

1 Answer 1

up vote 1 down vote accepted

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)?

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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