Click here to Skip to main content
15,886,806 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
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

C#
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
C#
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 :(

C#
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
Updated 3-Nov-11 22:47pm
v5
Comments
Nickos_me 3-Nov-11 8:06am    
Please, write ,how do you use this methods?
Member 7791974 3-Nov-11 11:21am    
private void dgvMedical_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
personnameToDataGrid();
}

private void dgvMedical_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
personnameToDataGrid();
}
BillWoodruff 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 4-Nov-11 0:55am    
In your code from where you got this error please specify the line number...
BobJanova 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.

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.....
 
Share this answer
 
Thank you Tejas!
The issue was that

C#
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
 
Share this answer
 
Comments
Tejas Vaishnav 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900