添加参数时未将对象引用设置为对象的实例
本文关键字:对象 实例 设置 对象引用 参数 添加 | 更新日期: 2023-09-27 18:20:55
我想在单击save
按钮时更新一些信息。
我收到一个错误
打开:命令。参数.Add("@doctorID",SqlDbType.Int).Value=居民博士.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; }
}
}
}
查看创建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...
}
尽管要做任何有意义的事情,您可能需要在保存之前用实际数据进行设置。
这是因为您的Doctor
和Room
没有初始化:
Resident hello = new Resident();
hello.Doctor = new Doctor();
hello.Room = new Room();
但是该更新可能会失败,因为CCD_ 8中没有有意义的数据并且CCD_。