Question C# - Dapper insert record on table with one to one relationship

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:

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:
OT, this is weird:
C#:
Int32.TryParse(divisionComboBox.SelectedValue.ToString(), out divisionId);
I would assume that the values exposed are type int to begin with, so you should not be converting that to a string and then back to an int. Just cast it as type int in the first place:
C#:
var departmentId = (int) divisionComboBox.SelectedValue;
 
Is u null? What's the value of divisionId? Does the stack trace of the exception indicate that the exception was actually thrown on that line or somewhere else?
I traced the problem. I initialize the class: public Division Division { get; set; } = new Division(); but another error occured in
cnn.Execute(@"insert into [User] ([LastName], [FirstName], [MiddleName], [DivisionId])
values (@LastName, @FirstName, @MiddleName, @DivisionId)", user);

System.NotSupportedException: 'The member DivisionId of type ClassLibrary.Division cannot be used as a parameter value'
 
I traced the problem. I initialize the class: public Division Division { get; set; } = new Division();
For that to have been the problem, your line of code would have had to look something like:
C#:
u.Division.Id = divisionId;
but you said that the problem was due to:
C#:
u.DivisionId = divisionId;

If you don't give us accurate information, helping you is going to be a major struggle.
 
I traced the problem. I initialize the class: public Division Division { get; set; } = new Division(); but another error occured in
cnn.Execute(@"insert into [User] ([LastName], [FirstName], [MiddleName], [DivisionId])
values (@LastName, @FirstName, @MiddleName, @DivisionId)", user);

System.NotSupportedException: 'The member DivisionId of type ClassLibrary.Division cannot be used as a parameter value'
Your Division class in post #1 doesn't even have a DivisionId member. Have you changed the code some more beyond just the fix that you mentioned in post #8.
 

Latest posts

Back
Top Bottom