Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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);
        }
    }


What I have tried:

So far I have inserted and retrieve records in a single table with no problem.
Posted
Updated 26-May-21 1:46am
v2
Comments
SeanChupas 26-May-21 8:02am    
What line of code gives you the error?

1 solution

The message "Object reference not set to an instance of an object" is telling you that you are trying to use a reference that has not been initialised. You need to run your code in the debugger to find out where it occurs, and why.
 
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