The complete code for the AddMemberviaCSV() method.

Nb: The UpdateMemberCsv() method code is about 80% the same.

    public async Task<CsvResult> AddMemberviaCSV(string[] Lines)
    {
        string Last_Name = "";
        string First_Name = "";
        DateTime DOB = new DateTime();
        if (Lines.Count() < 2)
            return CsvResult.addFailed;

        string[] fieldnames= new string[0];
        string[] values = new string[0];
        if (Lines[0].Contains(','))
        {
            fieldnames = Lines[0].Replace(' ','_').Split(',');
        }
        else if (Lines[0].Contains('\t'))
        {
            fieldnames = Lines[0].Replace(' ', '_').Split('\t');
        }
        else
            return CsvResult.addFailed;
        if (Lines[1].Contains(','))
        {
            // Handle values in quotes, can have , inside
            values = Lines[1].Split('"');
            string modifiedLine1 = Lines[1];
            if (values.Count() > 1)
            {
                modifiedLine1 = "";
                foreach (var segment in values)
                {
                    string editedSegment = segment;
                    if ( segment.Count(f => f == ',') == 1)
                        editedSegment =  segment.Replace(',', Settings.COMMMA_REPLACEMENT);
                    modifiedLine1 += editedSegment;
                }
            }
            values = modifiedLine1.Split(',');
        }
        else if (Lines[1].Contains('\t'))
        {
            values = Lines[1].Split('\t');
        }
        else
            return CsvResult.addFailed;


        PropertyInfo[] properties = typeof(Member).GetProperties();
        var subProperties = (from p in properties where p.CanWrite select p).ToList();

        int found = 0;
        int requiredFields = 0;
        int modelRequiredFields = 0;
        Member member = new Member();
        foreach (PropertyInfo property in properties)
        {
            var customattributes = property.CustomAttributes;
            if (customattributes != null)
            {
                foreach (var attr in customattributes)
                {
                    if(attr.AttributeType.Name == "RequiredAttribute")
                    {
                        modelRequiredFields++;
                        break;
                    }
                }
            }
            if (property.Name != "Id")
            {
                if (property.CanWrite)
                {
                    int index = Array.FindIndex(fieldnames, n => n == property.Name);
                    // Skip if not in csv
                    if (index == -1)
                        continue;
                    // Skip if no corresponding value
                    if (index > (values.Count()-1))
                        continue;
                    var value = values[index];

                    if (value != null)
                    {
                        int? ivalue = null;
                        int ivalue2 = 0;
                        bool isint = false;
                        DateTime dvalue = DateTime.Now;
                        bool isdatetime = false;
                        double? dbl = null;
                        double dbl2 = 0;
                        bool isdouble = false;
                        System.Globalization.CultureInfo cultureinfo = new System.Globalization.CultureInfo(Settings.TwSetting.Locale);
                        System.Globalization.DateTimeStyles styles = System.Globalization.DateTimeStyles.None;
                        if (int.TryParse(value, out ivalue2))
                        {
                            ivalue = ivalue2;
                            isint = true;
                        }
                        else  if (DateTime.TryParse(value, cultureinfo, styles, out dvalue))
                        {
                            isdatetime = true;
                        }
                        else if (double.TryParse(value, out dbl2))
                        {
                            dbl = dbl2;
                            isdouble = true;
                        }
                        try
                        {
                            // Abort adding new Member on match of name and DOB
                            if (property.Name == "Last_Name")
                            {
                                if ((from m in _context.Members where m.Last_Name == (string)value select m).Count() != 0)
                                {
                                    Last_Name = (string)value;
                                    found++;
                                }
                            }
                            else if (property.Name == "First_Name")
                            {
                                if ((from m in _context.Members where m.First_Name == (string)value select m).Count() != 0)
                                {
                                    First_Name = (string)value;
                                    found++;
                                }
                            }
                            else if (property.Name == "Date_of_Birth")
                            {
                                if (!isdatetime)
                                    continue;
                                if ((from m in _context.Members where m.Date_of_Birth == dvalue  select m).Count() != 0)
                                {
                                    DOB = dvalue;
                                    found++;
                                }
                            }
                            if (found == 3)
                            {
                                // This is a member to be updated not added
                                return await UpMemberviaCSV(Lines, Last_Name, First_Name, DOB);
                            }


                            if (property.PropertyType == typeof(int?))
                            {
                                if (isint)
                                {
                                    property.SetValue(member, ivalue);
                                }
                                else if (value == "")
                                {
                                    //OK ignore blank. May be set 0?
                                }
                                else 
                                {
                                    //ignore
                                }
                            }
                            else if ((property.PropertyType == typeof(DateTime)) && (isdatetime))
                            {
                                property.SetValue(member, dvalue);
                            }
                            else if ((property.PropertyType == typeof(Double?)) && (isint))
                            {
                                property.SetValue(member, (double)ivalue);
                            }
                            else if (property.PropertyType == typeof(Double?))
                            {
                                if (isint)
                                    property.SetValue(member, (double)ivalue);
                                else if (isdouble)
                                    property.SetValue(member, dbl);
                            }
                            else
                            // Note setting ,back into the string property
                                property.SetValue(member, (value.Replace(Settings.COMMMA_REPLACEMENT, ",")));

                            // value was assigned. Keep tab of required fields
                            if (property.Name == "Last_Name")
                            {
                                requiredFields++;
                            }
                            else if (property.Name == "First_Name")
                            {
                                requiredFields++;
                            }
                            else if (property.Name == "Date_of_Birth")
                            {
                                requiredFields++;
                            }
                        }
                        catch (Exception ex)
                        {
                            //CanWrite should take care of this.
                            if (ex.Message == "Property set method not found.")
                                return CsvResult.addFailed;
                            continue;
                        }
                    }
                }
            }
        }

        if (requiredFields != modelRequiredFields)
        {
            return CsvResult.addFailed;
        }
        // This handles the generation a list of group Ids for the member types for this member; and serialise to text property
        member.AddGroupsFrom_Member_Type_Csv(_context.MemberGroups.ToList());
        
        // Add member to database
        _context.Members.Add(member);
        await _context.SaveChangesAsync();
        return CsvResult.added;
    }

 TopicSubtopic
   
 This Category Links 
Category:Blazor Index:Blazor
  Next: > Blazor Helpers App Members
<  Prev:   Blazor Helpers App members