jose.talagres20
New member
- Joined
- May 26, 2021
- Messages
- 4
- Programming Experience
- Beginner
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:
	
	
	
	
	
	
	
	
	
		
			
			
			
			
			
		
	
	
	
		
	
	
		
	
FORM:
	
	
	
	
	
	
	
	
	
		
			
			
			
			
			
		
	
	
	
		
	
	
		
	
DATA ACCESS:
	
	
	
	
	
	
	
	
	
		
			
			
			
			
			
		
	
	
	
		
	
	
		
	
	
		
			
		
		
	
				
			I have this table structure.
----------
Employee
----------
Id
LastName
FirstName
MiddleName
DivisionId
-----------
Division
-----------
Id
DivisionName
Class:
			
				C#:
			
		
		
		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:
			
				C#:
			
		
		
		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.DivisionId = divisionId;DATA ACCESS:
			
				C#:
			
		
		
		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);
    }
}
			
				Last edited: 
			
		
	
								
								
									
	
		
			
		
		
	
	
	
		
			
		
		
	
								
							
							 
	 
 
		 
 
		 
 
		