So far I have inserted and retrieve records in a single table with no problem. My current situation is, I have two tables which has a one to one relationships. I have the following table structure and code, when I insert the record in the database I have this error: System.NullReferenceException: 'Object reference not set to an instance of an object.'I am all ears right now in hearing your suggestions and recommendations in moving forward since I am only new to dapper. Thank you in advance. I have this table structure.
---------- Employee ---------- Id LastName FirstName MiddleName DivisionId ----------- Division ----------- Id DivisionName Class: public class Employee { public int Id {get;set;} public string LastName {get;set;} public string FirstName {get;set;} public string MiddleName {get;set;} public Division Division {get;set;} } public class Divison { public int Id {get;set;} public string DivisionName {get;set;} } FORM: UserModel u = new UserModel(); int departmentId = 0; Int32.TryParse(divisionComboBox.SelectedValue.ToString(), out divisionId); u.LastName = lastNameText.Text.ToUpper(); u.FirstName = firstNameText.Text.ToUpper(); u.MiddleName = middleNameText.Text.ToUpper(); u.Division.Id = divisionId; DATA ACCESS: public static void SaveUser(UserModel user) { using (IDbConnection cnn = new SqlCeConnection(LoadConnectionString())) { cnn.Execute("insert into [User] ([LastName], [FirstName], [MiddleName], [DivisionId]) values (@LastName, @FirstName, @MiddleName, @DivisionId)", user); } }
So far I have inserted and retrieve records in a single table with no problem.
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)