Click here to Skip to main content
15,893,266 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an issue where I'm trying to import data from one 1 table to another using a web form. My problem is that only about 75% of the data is being imported. One issue that exists is that for worker name something like 'Contractor' has been entered. As of right now I split the names if there is a first and last name. (no middle names, foriegn, etc.)

However, I want to assign 'Contractor' to a default I.D of '50124'. (see code below)
I know if statements ending tag basically skips that assignment. But I put it there because I still have access to other necessary fields.

C#
protected void uxTransferBtn_Click(object sender, EventArgs e)
{
    var importLog = new System.Text.StringBuilder();

    var totalRecords = 0;
    var recordsProcessed = 0;
    var dcWorkerMatched = 0;
    var dcWorkerNotMatched = 0;
    var insertSuccess = 0;
    var insertFailed = 0;

    var accountTrackerImportSVC = new AccountTrackerImportSVC();
    
    var dtInfo = GetContent(); // Populated dtInfo With Table

    totalRecords = dtInfo.Rows.Count;

    foreach (DataRow drItem in dtInfo.Rows)
    {
        recordsProcessed++;


        // Get Worker Num From LastName_PreferredFirstName
        var worker= drItem["WorkerName"].ToString();
        var fullName = worker.Split(' ', '/');
        var allName = fullName;

        if (allName.Length == 2) // Believe my problem begins here
        {
            var firstName = fullName[0];
            var lastName = fullName[1];
            var dtWorkerNum = AccountTrackerImportDAL.ViewWorkerNumByLastNameFirstName(lastName, firstName);

            var dcWorkerNum = "";

            if (dtWorkerNum.Rows.Count == 1)
            {
                dcWorkerNum = dtWorkerNum.Rows[0]["WorkerNum"].ToString();

                dcWorkerMatched++;
            }
            else
            {
                dcWorkerNum = "50124";

                dcWorkerNotMatched++;
            }

            var connectionType = drItem["ConnectionTypeDesc"].ToString();
            var dtConnectionType = AccountTrackerImportDAL.ViewConnectonTypeByDesc(connectionType);
            var connectionTypeID = "";

            if (dtConnectionType.Rows.Count == 1)
            {
                connectionTypeID = dtConnectionType.Rows[0]["ConnectionTypeID"].ToString();
            }
            else
            {
                var connectionTypeSVC = new ConnectionTypeSVC();
                connectionTypeSVC.InsertConnectionType(connectionType, "System Admin");

            }

                    
            var result = new AccountTrackerEntrySVC().InsertAccountTrackerEntry(//SafeValueAccessor.GetControlValue(uxDataCustodianDdl),
                          dcWorkerNum,
                          drItem["Data1"].ToString(),
                          drItem["Data2"].ToString(),
                          drItem["Data3"].ToString(),
                          drItem["Data4"].ToString(),
                          drItem["Data5"].ToString(),
                          drItem["Data6"].ToString(),
                          drItem["Data7"].ToString(),
                          drItem["Data8"].ToString(),
                          "System Admin",
                          connectionTypeID,
                         (bool)drItem["Data9"],
                         (bool)drItem["Data10"],
                         drItem["Data11"].ToString(),
                         drItem["Number"].ToString(),
                         (bool)drItem["Requirement"]);

            if (result.Successful)
            {
                insertSuccess++;
            }
            else
            {
                importLog.Append("<br/>Error Processing Line Number: " + drItem["Number"].ToString());
                importLog.Append(result.Message);

                insertFailed++;
            }

        } //ending if bracket

    }

    importLog.Append("<br/><br/>");
    importLog.Append("---------------------------------------");
    importLog.Append("<br/>");
    importLog.Append("Summary");
    importLog.Append("<br/>");
    importLog.Append("Total Records Found: " +totalRecords);
    importLog.Append("<br/>");
    importLog.Append("Total Records Processed: " + recordsProcessed);
    importLog.Append("<br/>");
    importLog.Append("Total Data Custodians Matched:" + dcWorkerMatched);
    importLog.Append("<br/>");
    importLog.Append("Total Data Custodians Not Matched:" + dcWorkerNotMatched);
    importLog.Append("<br/>");
    importLog.Append("Total Success:" + insertSuccess);
    importLog.Append("<br/>");
    importLog.Append("Total Failed:" + insertFailed);

    //-........

    importLog.Append("<br/>");
    importLog.Append("---------------------------------------");

    var emailSVC = new Email();
    emailSVC.SendApplicationError("Data Import", "Import Summary", importLog.ToString());

    Master.HideProgressIndicator();
}


Where can I make adjustments to account for 'Contractor' being item = Fullname? Please include psuedo code. I'm horrible at coding but need to pass a summer class.
Posted
Updated 7-Apr-18 13:10pm
v2
Comments
virusstorm 12-Aug-15 12:41pm    
No one here will help you with your homework. It is designed to be challenging so you can learn something. As a part time college professor, my advice is to talk to the instructor of the class.

Also, if you are horrible with coding, why are taking a programming class?
Member 11820531 12-Aug-15 12:52pm    
I could see why people wouldn't want to help if it was a test, but you would never be able to know. I took the class to challenge myself and get an elective credit. It's an online class where the professor is difficult to get a hold of. I did really well in the beggining the material has gotten a lot more difficult.
Member 11820531 12-Aug-15 12:52pm    
Either way I'll figure it out.

I am not a specialist of C#, but I know a few universal things.
1) The problem can be linked to your database. Like a constraint of unique values in a field.
2) There is a way to know where your code fail and why. Use the debugger.

With the debugger, you will be able to see your code running step by step and follow every variable changes.
This way your own code is no more a black box, you will see what what your code is doing and you can compare with what you expect.
The immediat result is that you narrow the "what is wrong" by seeing when it goes wrong.

Give it a try, the experience is invaluable.
 
Share this answer
 
Here is a solution where you can use System.Linq to check for existing records in a list or in EntityFramework.

The trick is to read the obect from the list and check if the result was null, as in this code snippet:

var entityFirst = myUniqueInstance.SingleOrDefault(p => p.FirstName == newPersonFirst.FirstName && p.FamilyName == newPersonFirst.FamilyName);
if (entityFirst != null)
{
    // We found an existing person in the List of objects that matches our condition
    Console.WriteLine($"Rejected First: {newPersonFirst}");
    throw new Exception($"Person {newPersonFirst} already exists in our List!");
}
else
{
    // We did NOT find an existing person that matches out conditions
    // Now it is "pretty" save to add this person
    myUniqueInstance.Add(newPersonFirst);
    Console.WriteLine($"Added First: {newPersonFirst}");
}







using System;
using System.Collections.Generic;
using System.Linq;

namespace FindIfRecordExist
{
    public class MyUniqueClass
    {
        public int MyUniqueClassID { get; set; }
        public string FirstName { get; set; }
        public string FamilyName { get; set; }

        public MyUniqueClass()
        {
        }

        public MyUniqueClass(string FirstName, string FamilyName)
        {
            this.FirstName = FirstName;
            this.FamilyName = FamilyName;
        }

        public override string ToString()
        {
            //return $"{MyUniqueClassID} {FirstName} {FamilyName}";
            return $"{FirstName} {FamilyName}";
        }
    }


    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                // This example is made with a list but this works in Entity Framework
                // And many other solution when LINQ is involved, check You 
                // include "using System.Linq;" 
                List<MyUniqueClass> myUniqueInstance = new List<MyUniqueClass>();

                myUniqueInstance.Add(new MyUniqueClass("Donald", "Truman"));
                myUniqueInstance.Add(new MyUniqueClass("Ronald", "Clinton"));
                myUniqueInstance.Add(new MyUniqueClass("Bill", "Reagan"));
                myUniqueInstance.Add(new MyUniqueClass("Hillary", "Bold"));

                foreach(var p in myUniqueInstance)
                {
                    Console.WriteLine($"Added: {p}");
                }

                try
                {
                    MyUniqueClass newPersonFirst = new MyUniqueClass("Raymond", "Kennedy");

                    // Check if there is already anyone with the same name as our new person
                    var entityFirst = myUniqueInstance.SingleOrDefault(p => p.FirstName == newPersonFirst.FirstName && p.FamilyName == newPersonFirst.FamilyName);
                    if (entityFirst != null)
                    {
                        // We found an existing person in the List of objects that matches our condition
                        Console.WriteLine($"Rejected First: {newPersonFirst}");
                        throw new Exception($"Person {newPersonFirst} already exists in our List!");
                    }
                    else
                    {
                        // We did NOT find an existing person that matches out conditions
                        // Now it is "pretty" save to add this person
                        myUniqueInstance.Add(newPersonFirst);
                        Console.WriteLine($"Added First: {newPersonFirst}");
                    }

                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }



                // ------------------------------------------------------------
                // Now lets run this again and se if our new person exists....
                // ------------------------------------------------------------

                try
                {
                    MyUniqueClass newPersonSecond = new MyUniqueClass("Raymond", "Kennedy");

                    // Check if there is already anyone with the same name as our new person
                    var entitySecond = myUniqueInstance.SingleOrDefault(p => p.FirstName == newPersonSecond.FirstName && p.FamilyName == newPersonSecond.FamilyName);
                    if (entitySecond != null)
                    {
                        // We found an existing person in the List of objects that matches our condition
                        Console.WriteLine($"Rejected Second: {newPersonSecond}");
                        throw new Exception($"Person {newPersonSecond} already exists in our List!");
                    }
                    else
                    {
                        // We did NOT find an existing person that matches out conditions
                        // Now it is "pretty" save to add this person
                        myUniqueInstance.Add(newPersonSecond);
                        Console.WriteLine($"Added Second: {newPersonSecond}");
                    }


                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }




        }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

        }
    }
}
 
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