【发布时间】:2010-10-22 15:06:49
【问题描述】:
我正在尝试将一组值传递到一个表对象的数组中,以便我可以将它们写入数据库。
我的数据库看起来像这样 ->
tblCaseNotes
案例注释ID |个人身份 |等等等等
tblCaseNotesContactType
rowguid |案例备注ID |联系人类型ID
tblMaintItems
项目ID |类别ID
Maint 表中的 itemID 是与当前 CaseNoteID 一起写入 tblCaseNotesContactType 的内容。每个 CaseNote 可以有多个 ContactType。
到目前为止,我所拥有的是在我的 btnNew_Click 事件中创建的 CheckListBox ContactType 的值数组:
// Contact Type check list box
int cTypeCount = chkContactType.CheckedItems.Count;
int [] contactTypes = new int[cTypeCount];
// reusable generic counter for looping thru the check lists
int cMaintCounter = 0;
foreach (int checkedItem in chkContactType.CheckedIndices)
{
contactTypes[cMaintCounter] = (int)chkContactType.GetItemValue(checkedItem);
cMaintCounter++;
}
CurrentCaseNote.AddCNote(Program._CurrentPerson.PersonID, Convert.ToDecimal(tbxTimeSpentUnits.Text), chkIsCaseLog.Checked, Convert.ToDateTime(datContactDate.Text), memContactDetails.Text, contactTypes);
然后我将其传递给我的 CurrentCaseNote 对象的 AddCNote 方法。
public static void AddCNote(int personID, decimal tsUnits, bool isCaseLog, DateTime cDate, string cDetails, int[] cTypes)
{
var caseNoteToAdd = new tblCaseNote
{
CaseNoteID = Guid.NewGuid(),
PersonID = personID,
TimeSpentUnits =tsUnits,
IsCaseLog =isCaseLog,
ContactDate =cDate,
ContactDetails =cDetails,
InsertDate = DateTime.Now,
InsertUser = Environment.UserName
};
tblCaseNotesContactType[] cTypeToAdd = new tblCaseNotesContactType[cTypes.Length];
cTypeToAdd[0].CaseNoteID = caseNoteToAdd.CaseNoteID;
cTypeToAdd[0].ContactTypeID =cTypes[0];
cTypeToAdd[0].rowguid = Guid.NewGuid();
CaseNoteDAL.addCNote(caseNoteToAdd,cTypeToAdd);
然后将其传递给 DAL 以写入本地数据库:
public static void addCNote(tblCaseNote caseNote, tblCaseNotesContactType[] cType)
{
foreach (var type in cType)
{
caseNote.tblCaseNotesContactTypes.Add(type);
}
dcCaseNotes.tblCaseNotes.InsertOnSubmit(caseNote);
//dcCaseNotes.tblCaseNotes.InsertOnSubmit(caseNoteToAdd);
dcCaseNotes.SubmitChanges();
}
它给了我一个 NUllReferenceException 在这一行出现未处理错误 -->cTypeToAdd[0].CaseNoteID = caseNoteToAdd.CaseNoteID;
是因为我只使用 [0] 吗?我这样做是为了简化对此的测试。当我单步执行代码时,我的值数组是正确的,并且有一个 caseNoteToAdd.CasNoteID 的 Guid
有人可以给我一些关于我在这里出错的地方以及可能导致错误的原因吗?正如您从我的代码中可以看出的那样,我对此并不陌生,而且我正在快速学习。
谢谢,
~P
【问题讨论】: