Click here to Skip to main content
11,496,146 members (468 online)
The site is currently in read-only mode for maintenance. Posting of new items will be available again shortly.
Hi all, im having mad problems here...i have a datagrid that i have added two new columns of unbound data. I want to make it work so that it displays given name and last name in a table that only has ID and other information so that the viewer knows what information belongs to who.
this is the original code for events which works

private void addColumnDataToEventInvitees()
            // Load in custom columns for event invitees fNames and lNames
            if (dgvEvents.Rows.Count > 0 && dgvEventInvitees.Rows.Count > 0)
                for (int i = 0; i < dgvEventInvitees.Rows.Count; i++)
                    DataGridViewRow selectedRow = dgvEventInvitees.Rows[i];
                    int personID = Convert.ToInt16(selectedRow.Cells[0].Value);
                    DataRow findRow = data.Tables["People"].Rows.Find(personID);
                    dgvEventInvitees["GivenNames", i].Value = findRow["GivenNames"].ToString();
                    dgvEventInvitees["LastName", i].Value = findRow["LastName"].ToString();

this is my very similar code but it is for the medical forms. I have looked at blogs and i hear talk of adding a
instance of the object

I am just not sure how to implement this...Any help would be great!!
the error pops up in here. It says "there is already an open datareader associated with this command which must be closed first"

it tells me to close the datareader but i can't figure out how to close it Frown | :(

private void personnameToDataGrid()
            if (dgvMedical.Rows.Count > 0)
                for (int i = 0; i < dgvMedical.Rows.Count; i++)
                    DataGridViewRow selectedRow = dgvMedical.Rows[i];
                    int personID = Convert.ToInt16(selectedRow.Cells[0].Value);
                    DataRow findRow = data.Tables["People"].Rows.Find(personID);
                    dgvMedical["MDGivenNames", i].Value = findRow["GivenNames"].ToString();
                    dgvMedical["MDLastName", i].Value = findRow["LastName"].ToString(); //my error was appearing on this //line
Posted 2-Nov-11 9:28am
Edited 3-Nov-11 23:47pm
Nickos_me at 3-Nov-11 8:06am
Please, write ,how do you use this methods?
Member 7791974 at 3-Nov-11 11:21am
private void dgvMedical_CellValueChanged(object sender, DataGridViewCellEventArgs e)

private void dgvMedical_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
BillWoodruff at 3-Nov-11 22:13pm
I think it would be helpful if you specify the exact line where you get the exception. You are, I assume, single-stepping through this code, and able to see exactly where the exception happens ... yes ?
Tejas_Vaishnav at 4-Nov-11 0:55am
In your code from where you got this error please specify the line number...
BobJanova at 4-Nov-11 5:15am
Why are you filling a table adapter? That code is doing client-side lookups and doesn't need to do that – the data is already loaded by this point.
Tejas_Vaishnav at 4-Nov-11 5:41am
Please Use improve solutions or Have Question Or Comment, instead of posting solution to your question...
For your solution of your error please do this...

1) put a break point of your line at this...

 DataRow findRow = data.Tables["People"].Rows.Find(personID);

and press F10 then browse your findRow object if it will contain some thing or not...

then user immediate solution window and paste your line of code inside that
check this...

what it will give you.....

if it will give null then your code at datarow creation have something wrong and correct it there.....
Thank you Tejas!
The issue was that

int personID = Convert.ToInt16(selectedRow.Cells[0].Value);

was looking for stuff in the first column. What it was meant to be doing is looking in the number column where the ID was. so if id was in the third column then the selected row cells value would be 3

All fixed now
Tejas_Vaishnav at 4-Nov-11 8:20am
that's great that your issue is solved...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 10,401
1 OriginalGriff 8,910
2 Sascha Lefèvre 3,899
3 Maciej Los 3,422
4 Richard Deeming 2,600

Advertise | Privacy | Mobile
Web04 | 2.8.150520.1 | Last Updated 4 Nov 2011
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100