I'm working with an InfoPath form that has some C# code in it. My question is how can i extract a chunk of data from a string value? I've made it work using a .substring method and it returns the value that i'm looking for, however, i'm not 100% sure of the continuity of that field in Active Directory and i'm not sure that removing those characters would work for everyone.
Right now, the code retrieves the Address from Active Directory. In active dirrectory (at least for the handful of users i checked) the address is "2575 Sand Hill Road - MS XX" (where XX can be a 2 or 3 digit number).
I want to extract just the MS XX and display that in the InfoPath field. I know we have a lot of collaborators and people outside that have Active Directory accounts, which may or may not have an address listed and i'm not even 100% sure that our internal employees have the same entry (i'd imagine some of them might have 2575 Sand Hill Rd. - MS XX or something like that. Is there a way i cen delimit it on the "MS" and take everything from that point to the right to account for all 2 and 3 digit numbers and allow for any possible 4 digit numbers i might not know about?
Here's the code i am using right now to remove the first 22 characters: (I'm a c# noob, so any help is appreciated!!)
string
Address = employee.Properties["streetAddress"].Value.ToString(); string MailStop = Address.Substring(22);//Displays the mailstop in the infopath form
xnMyForm.SelectSingleNode(
"/my:myFields/my:RequestorMailstop", ns).SetValue(MailStop);
Jan MontanoPosted Feb 5, 2009, 7:20 PM
string searchKey = "MS";
int searchKeyIndex = address.IndexOf(searchKey);
string mailStop = address.Substring(searchKeyIndex + searchKey.Length).Trim();
Jan MontanoPosted Feb 9, 2009, 7:53 PM
Tyler AdamsPosted Feb 9, 2009, 1:59 PM
I did start another thread about it a week or so but havent had any replies yet. Here's the link in case you might be able to help - http://www.c-sharpcorner.com/Forums/ShowMessages.aspx?ThreadID=53765
Thanks!
Jan MontanoPosted Feb 8, 2009, 10:51 PM
Tyler AdamsPosted Feb 6, 2009, 5:17 PM
Oh...thank you! thank you! thank you!
That worked perfectly, it cut everything off except for the MailStop Number, which is exactly what i wanted.
You wouldnt happen to know how to make it leave the form field empty if the field in Active Directory is empty. No i run into the issue, when the form loads and it starts retrieving the information, when it tries to retrieve from a field that is empty, it throws an error and then opens the form and doesnt auto-populate any of the fields. I'd prefer if it populated what it could find and just leave the others blank so the user can enter it in manually. I've posted this to the forum before and get lots of views, but no responses yet =(.
Thanks again for your help with this one!!!