Code example:

DBModel curDB = new DBModel(); string RequestsDBPath = saveDir + @"RequestsDB.xml"; if (!File.Exists(RequestsDBPath)) { PersonClass person1 = new PersonClass("Π‘Π΅Ρ€Π²Π΅Ρ€Π½Ρ‹ΠΉ", "ΠšΡ€Π°Π±", "Π£Ρ‚ΠΈΠ»ΠΈΡ‚ΠΎΠ²ΠΈΡ‡", "OUP_2"); PersonClass person2 = new PersonClass("ΠŸΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»ΡŒΡΠΊΠΈΠΉ", "ВюлСнь", "Π€Π»ΡƒΠ΄ΠΈΠ»ΠΎΠ²ΠΈΡ‡", "IT_D112"); DataRow row = curDB.Requests.NewRow(); row["Id"] = 1; row["SM Id"] = "SR366312"; row["Desctription"] = "TestDesc"; row["Initiator"] = null; row["Worker"] = null; row["Status"] = "InProgress"; row["Creation Date"] = DateTime.Now; row["Changing Date"] = DateTime.Now.AddMonths(1); curDB.Requests.Rows.Add(row); curDB.Persons.Rows.Add(person1); curDB.Persons.Rows.Add(person2); curDB.WriteXml(RequestsDBPath, XmlWriteMode.IgnoreSchema); 

on curDB.Persons.Rows.Add (person1); I get an exception about the inadmissibility of null values ​​in the FIO (Table Key) parameter;

 public class PersonClass { public string SecondName; public string Name; public string ThirdName; public string FIO; public string Location; public PersonClass() { } public PersonClass(string SecondName, string Name, string ThirdName, string Location) { this.Name = Name; this.SecondName = SecondName; this.ThirdName = ThirdName; FIO = string.Format(@"{0} {1} {2}", SecondName, !string.IsNullOrEmpty(Name) ? Name.Substring(0, 1) : null, !string.IsNullOrEmpty(ThirdName) ? ThirdName.Substring(0, 1) : null).Trim(); this.Location = Location; } 

enter image description here

Upd: Wrote converter PersonClass => DataRow

  public System.Data.DataRow toDataRow() { var row = MainWindow.curDB.Persons.NewRow(); row["Name"] = Name; row["SecondName"] = SecondName; row["ThirdName"] = ThirdName; row["FullName"] = FullName; row["Location"] = Location; return row; } curDB.Persons.Rows.Add(person1.toDataRow()); 

    1 answer 1

    Try not to pass implicit parameters to the class, this loads the interpreter.

    In your place announcement

      PersonClass person1 = new PersonClass("Π‘Π΅Ρ€Π²Π΅Ρ€Π½Ρ‹ΠΉ", "ΠšΡ€Π°Π±", "Π£Ρ‚ΠΈΠ»ΠΈΡ‚ΠΎΠ²ΠΈΡ‡", "OUP_2"); 

    I would replace with

     PersonClass person1 = new PersonClass(){ Name = "Имя", SecondName = "Ѐамилия", ThirdName = "Patronymic", Location = "РасполоТСни"} 

    And in PersonClass, the name was announced as follows:

      public class PersonalClass { public string Name { get; set; } public string SecondName { get; set; } public string ThirdName { get; set; } public string FIO => $@"{SecondName} {(!string.IsNullOrEmpty(Name) ? Name.Substring(0, 1) : null)} { (!string.IsNullOrEmpty(ThirdName) ? ThirdName.Substring(0, 1) : null)}".Trim(); } 

    Consider, with this approach, the FIO parameter receives only the conditional Get attribute, you cannot write your own variable in it anywhere in the code.

    PS The code worked in the database entry for the PersonalClass database entity without the FIO field, with the FIO field you need to change the CRUD action.

    • Did not quite understand what was going on. CRUD? - DenisJNewb
    • β€œIt loads the interpreter” - which interpreter, excuse me? - VladD
    • Apparently DataTable does not know how to work with custom classes. It is necessary to transfer object as Datarow. - DenisJNewb