Results 1 to 2 of 2

Thread: Saving Datagridview Records if not(already) exists and deleting any other records.

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2014
    Posts
    33

    Saving Datagridview Records if not(already) exists and deleting any other records.

    Hi to all,
    I have always had this problem where: if I have a datagrid with records like

    RecID Name
    2 Tom
    5 Rich
    6 Harry

    To Save I will loop through the rows using the sql
    IF NOT EXISTS(SELECT * FROM myTable WHERE RecID= dgv.rows(i).cells(0).value say)
    BEGIN INSERT the row.(the use INSERT COMMAND)


    At the end of the loop I have to delete all records whose RecID are not in the Datagrid.
    My Problem is:
    How do I create a kind of list of datagrid RecIDs and do something like
    DELETE FROM myTable WHERE RecID NOT IN (2,5,6)

    Thanks in advance

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Saving Datagridview Records if not(already) exists and deleting any other records

    Here's an example of doing it the proper way, using parameters:

    http://www.vbforums.com/showthread.p...-SQL-IN-Clause

    Given that you're only using your own IDs though, rather than user-entered text, you probably don't really need parameters. As such, you can take out the parameter part of that and just concatenate the actual values into the SQL if you want.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width