0

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

1 Answer 1

6

The problem is that you've created an array of reference types, but not populated it.

In other words, after this line:

tblCaseNotesContactType[] cTypeToAdd = new tblCaseNotesContactType[cTypes.Length];

you've got an array of null references - the value of each element is null.

You then need to write:

cTypeToAdd[0] = new tblCaseNotesContactType();

(or a similar statement) before you can start changing its properties.

An alternative would be to use an object initializer and do it in one statement (after creating the array):

cTypeToAdd[0] = new tblCaseNotesContactType
{
    CaseNoteID = caseNoteToAdd.CaseNoteID,
    ContactTypeID =cTypes[0],
    rowguid = Guid.NewGuid()
}:
Sign up to request clarification or add additional context in comments.

1 Comment

Wow even an amateur like me should have seen that! Thanks!

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.