Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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();
                }
                peopleEventTableAdapter.Fill(data.PeopleEvent);
            }
        }
 

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
new
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
                }
                medicalTableAdapter.Fill(data.Medical);
 
            }
        }
Posted 2-Nov-11 8:28am
Edited 3-Nov-11 22:47pm
v5
Comments
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)
{
personnameToDataGrid();
}
 
private void dgvMedical_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
personnameToDataGrid();
}
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...
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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...
 
findRow["LastName"].ToString()
what it will give you.....
 
if it will give null then your code at datarow creation have something wrong and correct it there.....
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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
  Permalink  
Comments
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 550
1 mhegazy94 460
2 Ravi Bhavnani 230
3 Kornfeld Eliyahu Peter 185
4 Shemeemsha RA 160
0 Sergey Alexandrovich Kryukov 7,295
1 OriginalGriff 6,841
2 CPallini 5,420
3 George Jonsson 3,654
4 Gihan Liyanage 2,797


Advertise | Privacy | Mobile
Web03 | 2.8.140922.1 | Last Updated 4 Nov 2011
Copyright © CodeProject, 1999-2014
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