So heres my problem:
I have a datagrid w/ 5 columns(ID, firstname, lastname, Membership, Date). My app takes a 5 digit number from the user and with that number populates my datagrid with the members information. My BIGGEST problem is trying to check to see if the member is already in the datagrid or not. Here is my validating code:

private void findMember()
        {
            table = ds.Tables[0];

            //foreach datagridRow in datagrid
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
               
                // if not a new row
                if (!row.IsNewRow) //checking for a new row
                {
                    string memberDGV = dataGridView1[3, row.Index].Value.ToString();
                    // foreach dataRow in dataTable
                    foreach (DataRow dr in table.Rows)
                    {
                        

                        string memberDT = dr[3].ToString();

                       
                        if (memberDGV == memberDT)
                        {
                            // MessageBox.Show("Member is in dgc");
                            insertDate();
                        }
                        if (memberDGV != memberDT)
                        {
                            // MessageBox.Show("member is not in here");
                            insertMember();
                            insertDate();
                        }
                    }
                    
                }
            }

        }

The problem is that memberDGV & memberDT variables will always come up as the same value when im debugging and stepping through the code. The code will work the first time I try inputting a new member into my datagrid(It will insert the user into my sql table and display my table through the datagrid)However, once I try inserting another new member, it will replicate that members info multiple times.
Can anyone help?!!??!?!?!?! Much appreciation.

You're returning every row in the data table. What this will do, is check one, ok that exists, update the date.

Check the next one (insert) check the next one (insert) check the next one (insert) because the members don't exists for every row, just one.

Use a "break" statement to get out of the foreach loop after you insert or update your member.

commented: likeit says under his name, practically a master poster +2

Hm, you didnt say last time when I did this code for you, you will be using a new member insertion.
If so, you have to do it a bit differently, you have to check if a user from the dataTable exists in DGV, and if not, then you can insert a new member.

Like:

bool bUserExistance = false;
            //foreach datagridRow in datagrid
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                // if not a new row
                if (!row.IsNewRow) //checking for a new row
                {
                    if (!bUserExistance)
                    {
                        string memberDGV = dataGridView1[3, row.Index].Value.ToString();
                        // foreach dataRow in dataTable
                        foreach (DataRow dr in table.Rows)
                        {
                            string memberDT = dr[3].ToString();
                            if (memberDGV == memberDT)
                            {
                                bUserExistance = true;
                                break;
                            }
                        }
                    }
                    else
                        break;
                }
            }

            if (!bUserExistance)
            {
                // MessageBox.Show("member is not in here");
                insertMember();
                insertDate();
            }
            else
            {
                // MessageBox.Show("Member is in dgc");
                insertDate();
            }

SO this code now will go through all the rows of DGV and on each row will check all the rows of DataTable. If the use will be found, it will leave the both loops. If user will not be found will (for sure loop both the loops to the end), and when (in both cases) when leave the Rows of DGV loop, it will execute the code, regarding on the boolean flag.

Hope this will salve your problem.

thanks a lot man. But yea todays Monday in Ohio and i just now got to mess around more with the code you gave me. But Im thinking I just got all the help I needed:) MUCH APPRICIATION guys!! Im so glad I joined this site because everyone on it is professional, curtious, polite and has time to help another out, as opposed to going to other site where they would just give you a link and say goodluck lol . Have a great Monday my peeps!

:)
you too.
I know how it feels like when you want to do something very badly, and you cannot succeed it. Thats why I wanna give others as much as I know.
bye

Ok guys sorry to be the bearer of bad news but im not getting it to work for me right now. And my work day officially ends at 4pm. Soo, heres me code:

private void findMember()
        {
            table = ds.Tables[0];

            bool bUserExistance = false;

            //foreach datagridRow in datagrid
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {

                if (!row.IsNewRow) //checking for a new row
                {
                    if (!bUserExistance)
                    {

                        string memberDGV = dataGridView1[3, row.Index].ToString();
                        // foreach dataRow in dataTable

                        foreach (DataRow dr in table.Rows)
                        {

                            string memberDT = dr[3].ToString();

      // EXAMPLE:member scans card, id# is 44221, memberDGV and memberDT both have that value at execution time:/ 
                                if (memberDGV == memberDT)
                                {
                                    bUserExistance = true;
                                    break;
                                }
                        }    // END 2ND FOREACH BLOCK
                 }   // END IF bUserExistance
                                        else
                                        {
                                            bUserExistance = false;
                                            break;
                                        }


                }   // END IF NewRow exists
            }  // END 1ST FOREACH BLOCK


                    if (!bUserExistance)
                    {
                        insertMember();
                        insertDate();
                    }
                    else
                    {
                        insertDate();
                    }

        } // END findMember()

Hope you guys can help me out again:) Its just disconcerting to get great advice and then it not work lol, but oh well, works over for me. Lter mates!

What exactly is not working?
Does it work anything at all?

If you dont know exactly, set a break point just before this code, and go through it line by line, using F11 key.
This way you will exactly see whats really going on while looping through those two loops.

I double checked code i gave you, and it should work fine - if you are trying to find one person at a time.
PS: check if columns at index 3 are ok, if there isn`t any other index.

// EXAMPLE:member scans card, id# is 44221, memberDGV and memberDT both have that value at execution time:/
if (memberDGV == memberDT)

the code above seems to be my issue, everytime I step through the code, these two variables always remain the same. Which is stopping me from correctly checking:/ but i will report back once I get into my code again, Im a lil woosy as I am sick but am still at work. lter mate

Mitja! Its cool mate I figured it out, so this if(memberDGV == memberDT)statement was bothering me for a while, but, i figured out that I could still check for a member in my dgv using my newString variable, since thats the variable that holds the member account id #, its also a good validating var, as memberDGV tells the accountid value of each specific accountid column. Thanks again for all your help mate!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.