I am trying to pass an array of values into an array of a table object so that I can write them to the database.
My database looks like this ->
tblCaseNotes
CaseNoteID | PersonId | etc, etc
tblCaseNotesContactType
rowguid | CaseNoteID | ContactTypeID
tblMaintItems
itemID | CategoryID
The itemID from the Maint table is what is being written to the tblCaseNotesContactType along with the current CaseNoteID. There can be multiple ContactTypes per CaseNote.
What I have so far is an array of the Values for the CheckListBox ContactType created in my btnNew_Click Event:
// 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);
Which I then pass to my CurrentCaseNote object AddCNote method.
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);
It is then passed to the DAL to be written to the local database:
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();
}
It is giving me a NUllReferenceException was unhandled error on this line --> cTypeToAdd[0].CaseNoteID = caseNoteToAdd.CaseNoteID;
Is it because I am only working with the [0]? I just did that to simplify my testing of this. When I step through the code my value array is correct and there is a Guid for caseNoteToAdd.CasNoteID
Can someone give me a few pointers on where I am going wrong here and what might be causing the error? As you can tell from my code I am new to this and I am learning on the fly.
Thanks,
~P