Hello peeps,
This is really getting annoying, Im trying to figure out how to check whether or not a member is in my datagrid. I have 5 columns: firstname, lastname, membership, accountid, date. Can someone help me out here????????

Hi,
What do you mean? How you intend to find it?

I was asking if theres any code to decide if the member is already in the datagrid. My problem is that when I hit update in my app, it will add the member to my datagrid, regardless if the member has already been added or not. Today is a pretty bad Monday for me lol. Not enjoying today at all. So yea, just trying to solve my "duplicate members in datagrid" issue. Below is my methods that are called when I hit the update button:

findMember(); // SELECT statement
updateMember(); // UPDATE statement
insertMember(); // INSERT statement
insertDate(); // UPDATE statement to update Date column for member in datagrid

Need any more info just let me know. But, does anything come to mind??

What I meant is (in my upper question) do you use any textBox or any other control to find the member?
I dont know how you are looking for, what is your criteria.
I need more data...

abd btw, you said you have data in database. This application is actually for searching and updating user`s data, am I right?

when the app starts, control is given to a textbox, the user would scan a members card, members accountid would be in textbox1. The user then hits update. And yes you would be correct. This app is for searching for members, adding that member(whose card has been scanned) to my datagrid, then if that members card is scanned again, it would update that specific members date column to the present date/time.

Well, are you storing the user data in a class of any sort? Or is it being loaded directly from a database to the datagrid?

What does FindMember() do? I would think that it finds a member...so could you not use that do determine if the member already exists (ie if it returns null assume no member)?

Maybe posting a little bit of the problem code would help us help you.

Example:

if (FindMember(myAccountID) != null)
    InsertDate(myAccountID); //Update the existing member's date
else
   AddMember(myAccountID) //Add the new member
RefreshTheGrid(); //Refresh the UI to show the change

Also, you might want to look into this if you want to handle everything in straight SQL.

Sorry i didnt specify that earlier. But the user data that I am retrieving is from a sql database table. Also, once I retrieve the user data, it is getting thrown into the same database, except into a new table.
And yes findMember() uses a SELECT statement to find the member in the first database table. Not the new one my app is populating. But i found out yesterday that I dont even need that method because my INSERT method(insertMember()) does that work for me. So I was thinking I could change that method to validate if the member is already in my datagrid/ sql database table. Below is my findMember() :

 private void findMember()
        {
            sql = "SELECT Accounts.FirstName AS FirstName, Accounts.LastName AS LastName, Accounts.AccountID AS AccountID, AccountTypes.name AS Membership FROM Accounts JOIN AccountTypes ON AccountTypes.ID=Accounts.AccountType WHERE AccountID=" + newString;

            cs.Open();

            da.SelectCommand.ExecuteNonQuery();

            cs.Close();
        }

This is a not a problem code.
I actually really dont understand what exactly are you trying to do here.
Ok, you this SELECT sql query, which gets Users data (fist and last name, and id).
You populated DGV with these data.
Then what?

Please dont just from one issue to another.
Continue from where I stopped.

I wasnt jumping man my second to last post was a response to your last post. But, Im trying to change my selectMember() to validate and check if a member is already in my dgv. However, Im having trouble doing that because iver never done a validating if statement. Thanks for your response.

If its only this, then yoz can do:

// firstname, lastname, membership, accountid, date
            DataTable table = new DataTable(); // get the members (new ones)
            //now lets check if the member from table is already in the DGV:
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                string memeberDGV = dataGridView1[0, row.Index].Value.ToString();
                foreach (DataRow dr in table.Rows)
                {
                    string memberDT = dr[0].ToString();
                    if (memeberDGV == memberDT)
                    {
                        //member already exists in dgv!
                    }
                }
            }

thanks man your awesome. I have a ? though.
1. Will creating the DataTable table = new DataTable() mess with anything else? i.e. will it mess with my other code that will display my dgv? Or is it strictly for validating purposes?

Thanks again for helping me out. The world needs more people like you. There to help a fellow man in need. Have a great day, and if this fixes my issue I will mark it solved later on today:)

Just make sure the able name of the DataTable objects (the variable name, like I used "table") are different. With other words, table names must be different one to each other. Then you can have 100 of them at ones.

so im getting this error:
Object reference not set to an instance of an object.
:( it does validate that the user is not in the dgv. I did that by messagebox.show(). But it loops a bunch of times. Then comes up with the error above. HELP!

I would say this is happening in the last row, where no data in presented.
Try to do some checking for a "NewRow", this is the property which checks if the row is a new one (the last one actually):

DataTable table = new DataTable(); // get the members (new ones)
            //now lets check if the member from table is already in the DGV:
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (!row.IsNewRow) //checking for a new row
                {
                    string memeberDGV = dataGridView1[0, row.Index].Value.ToString();
                    foreach (DataRow dr in table.Rows)
                    {
                        string memberDT = dr[0].ToString();
                        if (memeberDGV == memberDT)
                        {
                            //member already exists in dgv!
                        }
                    }
                }
            }

so I binded my datatable table = new datatable() to ds.Tables[0]; Which helped with a previos error. But now that I added that if statement it is just looping my messagebox continuously and just keeps adding a member:/

Show me your whole code of this.

Heres my method. These foreach statements are the cause of my problem. As it cause the app to loop for each row in my dgv.

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

            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (row.IsNewRow == false) //checking for a new row
                {
                    string memberDGV = dataGridView1[0, row.Index].Value.ToString();

                    foreach (DataRow dr in table.Rows)
                    {
                        string memberDT = dr[0].ToString();
                        if (memberDGV == memberDT)
                        {
                            MessageBox.Show("member is already in datagrid");
                            // member is already in dgv, update date field for that user
                        }
                        else
                        {
                            MessageBox.Show("not in dg");
                        }
                    }
                }
            }

        }

Sure you are keep on getting messages. For every single check of the user, no matter if its in the "list" of dgv, or not.

Do you want to show the meesage only for the user which is already in the list, or you would like to show the message only ones - on the end, which will tell "There is/are usres in the list??

I would suggest you to do show the message only ONES - after the checking is all done.
So do it like:

int count = 0;
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (!row.IsNewRow) //checking for a new row
                {
                    string memeberDGV = dataGridView1[0, row.Index].Value.ToString();
                    foreach (DataRow dr in table.Rows)
                    {
                        string memberDT = dr[0].ToString();
                        if (memeberDGV == memberDT)
                        {
                            //member already exists in dgv!
                            count++;
                        }
                    }
                }
            }
            if (count > 0)
                MessageBox.Show(String.Format("{0} {1} already in the list. {2} not coppied.", count, (count == 1 ? "member" : "members"), count == 1 ? "It was" : "They were"));

the messagebox.show was just for my checking purposes only. Making sure that the code did what it is suppose to do. But I dont need them and am taking them out right now:) ill let you know what i figure out

Remember, messages boxes, like you had them inside the code, will fire each row when looping through the dataTable. This is ok and normal (I reapeat, like you had them inside the loop).
I suggest you to use my code from the previos post, to notifly the user about duplications (if nothing else).

thanks again man. Im getting it I think. Btw I think wit my help alone you have been promoted to Posting Virtuoso lol niiiice. I'll hit u up if anything else comes up. Lter mate!

One last thing man, So this line of code:
if (memberDGV == memberDT)

actually doesnt work for me. When I step through the code it is giving my the same number value for each variable(memberDGV and memberDT) no matter if its a new member or not?

nice :)

foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                        string memberDT = dr[0].ToString(); //0 is the index for the 1st column, so you have to adapt the code to suit the column indexes
                        if (memeberDGV == memberDT)
                        {
                            //member already exists in dgv!
                            count++;
                        }
                    }

the same goes for the columns in the dgv. Just set the indexes or columns names, so it will check the same ones.

DUDE! Just wanna say thanks a lot! Seriously. You made my Friday. So have a great Friday yourself mate! I set the index to accountid for dt and dgv. Picked it up right away, validated a member, then updated the appropriate field! Once again thanks a lot and have a great weekend! U on facebook?

:)
I am so glad if I see people I who I helped them glad.
Have a great Friday your self too.
bye, bye

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.