So this is my problem my fellow programmers: In my C# app, I have one button and a datagrid that is showing info from a sql table. Everytime I add a person into my datagrid, the data replicates instead of updating certain columns for that member. I need to somehow distinguish between inserting a new member, and updating an existing member inside my datagrid.
Below is two methods that are called once the UPDATE button is clicked. Does anyone know how to solve this irritating issue/ see a problem with my code??? Thanks in advance!

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;
            da.SelectCommand = new SqlCommand(sql, cs);
            cs.Open();

            da.SelectCommand.ExecuteNonQuery();

            cs.Close();
        }


private void insertMember()
        {
            insertmembers = "INSERT INTO WaynokaLogger(FirstName, LastName, Membership, AccountID) SELECT Accounts.FirstName AS FirstName, Accounts.LastName AS LastName, AccountTypes.Name AS Membership, Accounts.AccountID AS AccountID FROM Accounts JOIN AccountTypes ON AccountTypes.ID=Accounts.AccountType WHERE AccountID=" + newString;    // newString is variable that holds members AccountID #
            da.InsertCommand = new SqlCommand(insertmembers, cs);
            cs.Open();
            da.InsertCommand.ExecuteNonQuery();
            cs.Close();
        }

Recommended Answers

All 4 Replies

You have to use UPDATE sql statement, not INSERT!!
like:
"UPDATE MyTableName SET @field2, @field3, @filed4 WHERE FieldName1 = @filed1"

Behind the SET keyword specify the field names (columns) you want to update. No need all, just those you want to update. And you must use a WHERE clause, so the code knows which row to update.
As simple as that.

Mitja your awesome. I think in the last week or so I've giving u like 5- 10 rep points lol your a boss. Sorry sometimes when you code and code and code, simple things like that slip your mind. THANKS AGAIN!

hehe, no problem mate, I really like to help.
Cya around.

If you got the answer, please close the thread by marking as salved (answered).
thx in advance ;)

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.