Click here to Skip to main content
15,885,890 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm importing data given to me into a connected database. However rather than use the wizard it's required that I do the import using a web form. The data given to me is not in the best format.

My code is posted below along with comments of what the code is doing and comments of where I am having issues

C#
protected void uxImportBtn_Click(object sender, EventArgs e)
        {
            var accountTrackerImportSVC = new AccountTrackerImportSVC();
            // Imports Table
            var dtInfo = GetContent();

            foreach (DataRow drItem in dtInfo.Rows)
            {
                // Get Worker Num From LastName_PreferredFirstName
                var dataCustodian = drItem["DataCustodian"].ToString();
                var fullName = dataCustodian.Split(' ','/');
                var firstName = fullName[0];
                var lastName = fullName[1]; // Error Thrown Here

                var dtWorkerNum = AccountTrackerImportDAL.ViewWorkerNumByLastNameFirstName(lastName, firstName);

When I debug my web page I can see that 'Contractor' is being passed on to the dataCustodian variable. With the code that I have and by the format of that particular field, I won't be able to split on a 'space' or '/'.

My var firstName = 'Contractor' and the lastName be grabbed is one for the next entry. I don't have middle names, middle initials, or foreign names. I just need to know how to solve this issue.

Any ideas on how account for this in my code?
Posted
Comments
Daniel Pfeffer 11-Aug-15 10:40am    
If dataCustodian does not contain any spaces or slashes, fullName[] will have only one element. This is possible e.g. if you have only a last name, but no first name. I don't see a test for this condition.
Member 11820531 11-Aug-15 10:50am    
That's the exact issue that I'm going through right now. I take it a test condition would consist of if else statement right? Sorry if my question seems elementary, I'm still learning the ins and outs of C#.

1 solution

"That's the exact issue that I'm going through right now. I take it a test condition would consist of if else statement right? Sorry if my question seems elementary, I'm still learning the ins and outs of C#."
Yes:
C#
string dataCustodian = drItem["DataCustodian"].ToString();
string[] fullName = dataCustodian.Split(' ','/');
if (fullName.Length >= 2)
   {
   string firstName = fullName[0];
   string lastName = fullName[1];
   ...
 
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