0

I want to update some information when a save button is clicked.

I got an error

On : command.Parameters.Add("@doctorID", SqlDbType.Int).Value = resident.Doctor.DoctorID; Saying: Object reference not set to an instance of an object.

Im guessing I need to create some kind of object?

Button code:

  private void btnSave_Click(object sender, RoutedEventArgs e)
    {
        Resident hello = new Resident();
        hello.Doctor = new Doctor();
        Resident residentID;

        txtAdditionalInformation.Text = hello.addtionalInformation;
        txtForename.Text = hello.FirstName;
        txtSurname.Text = hello.Surname;
        txtTitle.Text = hello.Title;



        ResidentData.Update(hello);
    }

update code (ResidentData Class):

   public static void Update(Resident resident, SqlConnection connection,                  SqlTransaction transaction)
    {
        StringBuilder sqlString = new StringBuilder();
        SqlCommand command;

        sqlString.Append("UPDATE [Resident] SET ");
        sqlString.Append("title = @title, ");
        sqlString.Append("firstName = @firstName, ");
        sqlString.Append("surname = @surname, ");
        sqlString.Append("dateOfBirth = @dateOfBirth, ");
        sqlString.Append("photo = @photo, ");
        sqlString.Append("doctorID = @doctorID, ");
        sqlString.Append("roomID = @roomID, ");
        sqlString.Append("allergies = @allergies, ");
        sqlString.Append("additionalInformation = @additionalInformation ");
        sqlString.Append("WHERE residentID = @residentID ");
        command = new SqlCommand(sqlString.ToString(), connection);
        if ((transaction != null)) command.Transaction = transaction;

        command.Parameters.Add("@residentID", SqlDbType.Int).Value = resident.ResidentID;
        command.Parameters.Add("@title", SqlDbType.VarChar, 50).Value = Helper.GetValue(resident.Title);
        command.Parameters.Add("@firstName", SqlDbType.VarChar, 100).Value = Helper.GetValue(resident.FirstName);
        command.Parameters.Add("@surname", SqlDbType.VarChar, 100).Value = Helper.GetValue(resident.Surname);
        command.Parameters.Add("@dateOfBirth", SqlDbType.DateTime).Value = Helper.GetValue(resident.DateOfBirth);
        command.Parameters.Add("@photo", SqlDbType.Image, 2147483647).Value = Helper.GetValue(resident.Photo);
        command.Parameters.Add("@doctorID", SqlDbType.Int).Value = resident.Doctor.DoctorID;
        command.Parameters.Add("@roomID", SqlDbType.Int).Value = resident.Room.RoomID;
        command.Parameters.Add("@allergies", SqlDbType.NText).Value = resident.Allergies;
        command.Parameters.Add("@additionalInformation", SqlDbType.NText).Value = resident.addtionalInformation;
        int rowsAffected = command.ExecuteNonQuery();

        if (!(rowsAffected == 1))
        {
            throw new Exception("An error has occurred while updating Resident details.");
        }
    }

*Residence Class:

 {
    public class Resident
    {

    private int residentID;
    private string title;
    private string firstName;
    private string surname;
    private string searchText;
    private System.DateTime dateOfBirth;
    private byte[] photo;
    private Room room;
    private Doctor doctor;
    private string nhs;
    private string residentBarcode;
    private string allergies;
    private string additionalInformation;

    public Resident()
        : base()
    {
    }

    public Resident(int newResidentID, string newTitle, string newFirstName, string newSurname, string newSearchText, System.DateTime newDateOfBirth, byte[] newPhoto, Room newRoom, Doctor newDoctor, string newNhs, string newResidentBarcode, string newAllergies, string newAdditionalInformation)
        : base()
    {
        residentID = newResidentID;
        title = newTitle;
        firstName = newFirstName;
        surname = newSurname;
        searchText = newSearchText;
        dateOfBirth = newDateOfBirth;
        photo = newPhoto;
        room= newRoom;
        doctor = newDoctor;
        nhs = newNhs;
        residentBarcode = newResidentBarcode;
        allergies = newAllergies;
        additionalInformation = newAdditionalInformation;
    }

    public int ResidentID
    {
        get { return residentID; }
        set { residentID = value; }
    }

    public string Title
    {
        get { return title; }
        set { title = value; }
    }

    public string FirstName
    {
        get { return firstName; }
        set { firstName = value; }
    }

    public string Surname
    {
        get { return surname; }
        set { surname = value; }
    }

    public string SearchText
    {
        get { return searchText; }
        set { searchText = value; }
    }

    public System.DateTime DateOfBirth
    {
        get { return dateOfBirth; }
        set { dateOfBirth = value; }
    }

    public byte[] Photo
    {
        get { return photo; }
        set { photo = value; }
    }


    public Room Room
    {
        get { return room; }
        set { room = value; }
    }

    public Doctor Doctor
    {
        get { return doctor; }
        set { doctor = value; }
    }

    public string NHS
    {
        get { return nhs; }
        set { nhs = value; }
    }

    public string ResidentBarcode
    {
        get { return residentBarcode; }
        set { residentBarcode = value; }
    }

    public string Allergies
    {
        get { return allergies; }
        set { allergies = value; }
    }
    public string addtionalInformation{

        get { return additionalInformation; }
        set { additionalInformation = value; }

    } 


}

}

5
  • Show your resident class Commented Jul 4, 2013 at 10:13
  • 2
    My guess (and it's an educated one) is that resident.Doctor is null - you can't access DoctorID if Doctor is a null value... hence the null ref exception Commented Jul 4, 2013 at 10:13
  • Can you verify that in resident.Doctor.DoctorID, neither resident or Doctor is null? Commented Jul 4, 2013 at 10:14
  • I'm making the assumption that since it got to the aforementioned line, that the resident.ResidentID line executed successfully and therefore resident is not null - so logically it must be Doctor that's null Commented Jul 4, 2013 at 10:15
  • 2
    Off topic: but you can have that query in a string using the @ string literal Commented Jul 4, 2013 at 10:16

2 Answers 2

2

Looking at the way you've created your Resident:

Resident hello = new Resident();
ucMedicationCheckIn.SaveCheckedInMedication();
ResidentData.Update(hello);
ucMedicationCheckIn.HideDetails();

you probably haven't assigned it a doctor which is why it fails on this line:

command.Parameters.Add("@doctorID", SqlDbType.Int).Value = resident.Doctor.DoctorID;

You need to initialize the Doctor type

Edit*

After seeing your Resident class I can see that you haven't initialized Room either.

You could provide a constructor to initialize these to default values:

public Resident()
{
    this.Doctor = new Doctor();
    this.Room = new Room();
    //etc...   
}

Though to do anything meaningful you will probably want to set these up with actual data before saving.

Sign up to request clarification or add additional context in comments.

5 Comments

Yes, I can see that. The issue is that you're accessing the Doctor/Room members before initializing them. Providing a default constructor that sets these up will fix the immediate problem, however you still need to provide real data for your resident/doctor/room objects before you can save.
I have given it meaning full data please check the code I am getting the same error
No you haven't. You've created a new Doctor, you haven't set the Doctor up with data. You also haven't set up the Room which is also going to cause you issues judging by this line: command.Parameters.Add("@roomID", SqlDbType.Int).Value = resident.Room.RoomID;
How do I set an ID up with data? please see my question above where I set a method with data
You need to set the object up with data, this includes the ID property. Unfortunately, it will be very difficult to tell exactly how without seeing most of the code. You'll need to read through the source and figure that part out yourself or speak with the original developer.
0

That is because your Doctor and Room are not initialized anyhere:

Resident hello = new Resident();
hello.Doctor = new Doctor();
hello.Room = new Room();

but that update will probably fail as there is no meaningful data in resident and rowsAffected will return 0 probably.

3 Comments

@user2122032 Read this part: but that update will probably fail as there is no meaningful data in resident and rowsAffected will return 0 probably.. You need to create a Resident correctly.
How do I go about getting meaningful data?
You have to answer it yourself, see how your program works, how you want to achieve that. Or ask someone who wrote that code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.