2013-07-04 74 views
0

單擊save按鈕時,我想更新一些信息。在添加參數時未將對象引用設置爲對象的實例

我得到一個錯誤

開:command.Parameters.Add( 「@ doctorID」,SqlDbType.Int)。價值= resident.Doctor.DoctorID;說:對象引用未設置爲對象的一個​​實例的 。

我猜我需要創建某種對象嗎?

按鈕的代碼:

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); 
    } 

更新代碼(ResidentData類):

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."); 
     } 
    } 

* 住宅類:

{ 
    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; } 

    } 


} 

}

+0

顯示您的居民艙 –

+2

我的猜測(它是一個受過教育的)是'resident.Doctor'爲空 - 如果'Doctor'爲空值,則無法訪問'DoctorID'因此null ref例外 – Charleh

+0

您可以驗證在'resident.Doctor.DoctorID'中,居民或醫生都不爲空嗎? – Anton

回答

2

看着你所創建的方式你Resident

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

你可能還沒有賦予它的醫生這就是爲什麼它在這條線失敗:

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

您需要初始化Doctor

編輯*

看到你的Resident班後,我可以看到你還沒有初始化Room

你可以提供一個構造函數來初始化這些爲默認值:

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

雖然做任何有意義的事情,你可能會想在保存前用實際數據來設置這些。

+0

已經有醫生和房間類 – user2122032

+0

是的,我可以看到。問題在於你在初始化之前訪問了「Doctor/Room」成員。提供默認的構造函數可以解決當前的問題,但是您仍然需要爲居民/醫生/房間對象提供真正的數據,然後才能保存。 – DGibbs

+0

我給它的意思是完整的數據,請檢查代碼我得到相同的錯誤 – user2122032

0

那是因爲你DoctorRoom不被初始化anyhere:

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

但更新可能會失敗,因爲在resident無意義的數據和rowsAffected可能會返回0。

+0

仍然給我錯誤 – user2122032

+0

@ user2122032閱讀本部分:'但是那個更新可能會失敗,因爲駐留中沒有有意義的數據,而rowsAffected可能會返回0。您需要正確創建一個「Resident」。 – DGibbs

+0

如何獲取有意義的數據? – user2122032

相關問題