Results 1 to 9 of 9

Thread: Advice of how to do this ...

  1. #1

    Thread Starter
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Advice of how to do this ...

    Hi all

    this is my problem :

    I have DataGridView control which i populate with some rows from the database
    the database table which i work with changes a lot so every 30 seconds I re-query the database to check if there is new record or existing record changed or record deleted, I can't clear and re-populate the grid view because people might work with it and they're might doing some changes in some rows.

    so if the row was changed i just want to update the changes in the existing row without recreating it, if some row deleted i want to remove it and if a row was added i want to add the new row

    anyone did something like this before ?

    Best Regards,
    Moti.
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  2. #2
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: Advice of how to do this ...

    Here is a thought. Add a field to the table(s) LAST_CHANGED as a Date Time field which we have on all of our tables (we use a different name for the field) and use it in the WHERE clause of a SQL statement which would pick up new and changed rows. With that in mind go through the result set one by one and update the row in the underlying source of the DataGridView which would be easy if you are using a BindingSource which would allow you to locate a single row using the find method of the BindingSource and update the row casted as a DataRow. In regards to deleted rows, you could have a second Date Time field that if not NULL would indicate a deleted row and similar logic could be used to remove the row from the BindingSource as with added and edited rows. Any ways making changes as mentioned above to the underlying DataSource of the DataGridView would be reflected in the DataGridView visually to the user.

    We use similar logic in my agency (Oregon Department of Revenue) with billons of records along with tracking who added edited or removed data from one of the databases. BTW we use IBM-DB2 on a massive scale and adding the fields above which was done around eight years ago has not slowed down any data operations.

    Hope this is helpful.

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Advice of how to do this ...

    Look at the Merge function of the datatable... might provide the functionality you need.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Advice of how to do this ...

    How far are you into architecting this?

    This routine does a refresh of a row in a datatable

    Code:
        Private Sub RefreshRow(ByVal VendorId As String, Optional ByVal dr As DataRow = Nothing)
            Dim rrStep As String = "Err @1"
            If VendorId <> "" Then
                Try
                    rrStep = "Err @2"
                    _inrefresh = True
                    Using cmd As New SqlCommand
                        cmd.CommandType = CommandType.Text
                        cmd.CommandText = "Select * From Vendor_V Where VendorId=" & VendorId
                        cmd.Connection = _connection
                        _connection.Open()
                        rrStep = "Err @3"
                        Using rdr As SqlDataReader = cmd.ExecuteReader
                            rrStep = "Err @4"
                            If rdr.Read() Then
                                rrStep = "Err @5"
                                If dr Is Nothing Then dr = _vendortable.Rows.Find(VendorId)
                                rrStep = "Err @6"
                                For Each dc As DataColumn In dr.Table.Columns
                                    If Not dc.ReadOnly Then dr.Item(dc.ColumnName) = rdr.Item(dc.ColumnName)
                                Next
                                rrStep = "Err @7"
                                dr.AcceptChanges()
                            End If
                        End Using
                    End Using
                Catch ex As Exception
                    MessageBox.Show(ex.Message, "Unknown Errors on VendorBal_C.RefreshRow at step " & rrStep, MessageBoxButtons.OK, MessageBoxIcon.Error)
                Finally
                    _inrefresh = False
                    _connection.Close()
                End Try
            End If
        End Sub
    That was one of the battles I fought trying to achieve this.

    As you can see I'm currently debugging multi-user issues and have some debug messages in place...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  5. #5
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Advice of how to do this ...

    Quote Originally Posted by techgnome View Post
    Look at the Merge function of the datatable... might provide the functionality you need.

    -tg
    I've tried MERGE and found I was not in control enough of what was going on.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  6. #6

    Thread Starter
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: Advice of how to do this ...

    thanks for the answers guys, I have an idea of how to do it in my head I just wanted to make sure if there is "build-in" or "streamed" way to do it before I implementing my idea.

    I guess this is my green light of doing it as I thought i should.
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Advice of how to do this ...

    Quote Originally Posted by szlamany View Post
    I've tried MERGE and found I was not in control enough of what was going on.
    Control is one of those things where you need it if you need it but you don't if you don't. If you can do what you need to do with a single method call then you may as well just use a single method call. Here's an example of DataTable.Merge at work:
    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Dim data As DataTable
    4.  
    5.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    6.         Me.data = Me.GetOriginalData()
    7.         Me.BindingSource1.Filter = "IsDeleted = False"
    8.         Me.BindingSource1.DataSource = Me.data
    9.         Me.DataGridView1.DataSource = Me.BindingSource1
    10.     End Sub
    11.  
    12.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    13.         Me.data.Merge(Me.GetUpdatedData(), True)
    14.  
    15.         For Each row As DataRow In Me.data.Select()
    16.             If CBool(row("IsDeleted")) Then
    17.                 Me.data.Rows.Remove(row)
    18.             End If
    19.         Next
    20.     End Sub
    21.  
    22.     Private Function GetTable() As DataTable
    23.         Dim table As New DataTable
    24.  
    25.         With table.Columns
    26.             table.PrimaryKey = New DataColumn() {.Add("ID", GetType(Integer))}
    27.             .Add("Text", GetType(String))
    28.             .Add("UpdatedTime", GetType(Date))
    29.             .Add("IsDeleted", GetType(Boolean))
    30.         End With
    31.  
    32.         Return table
    33.     End Function
    34.  
    35.     Private Function GetOriginalData() As DataTable
    36.         Dim table As DataTable = Me.GetTable()
    37.  
    38.         With table.Rows
    39.             .Add(1, "Row 1, Original Text", Date.Now, False)
    40.             .Add(2, "Row 2, Original Text", Date.Now, False)
    41.             .Add(3, "Row 3, Original Text", Date.Now, False)
    42.         End With
    43.  
    44.         Return table
    45.     End Function
    46.  
    47.     Private Function GetUpdatedData() As DataTable
    48.         Dim table As DataTable = Me.GetTable()
    49.  
    50.         With table.Rows
    51.             .Add(1, "Row 1, Updated Text", Date.Now, False)
    52.             .Add(2, "Row 2, Deleted", Date.Now, True)
    53.             .Add(4, "Row 4, New Text", Date.Now, False)
    54.         End With
    55.  
    56.         Return table
    57.     End Function
    58.  
    59. End Class
    Add a Button, DataGridView and BindingSource to a form and then run that code. You will intially see three rows with their original data. Click the Button and then the data will be updated, adding one new row, updating one existing row and deleting another existing row. The code to remove the deleted rows is not strictly necessary as the Filter will exclude those rows anyway, but it's a good idea if the data will be used for some time.

    Note that records can't actually be deleted from the database if you want to be able to update an application based on database changes. You often can't delete for other reasons anyway though.

    Of course, if you do need to implement logic that isn't supported by the Merge method then you will need to write more code of your own. That's the same with anything though.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: Advice of how to do this ...

    Hi,
    thanks for the example JM, In my case the records is not deleted but moving from one table to table (pre_orders to orders) I also never use datatables instead I use a list of custom class that I get back from my DAL -> BLL layers.

    I'll try to go head and implement what I had in mind today.
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  9. #9
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Advice of how to do this ...

    Quote Originally Posted by jmcilhinney View Post
    Control is one of those things where you need it if you need it but you don't if you don't. If you can do what you need to do with a single method call then you may as well just use a single method call...
    Sorry - I mispoke...

    It was speed and scalability - merge wasn't good for the number of records I was dealing with and it was only in the couple of thousand area.

    Mentioned in this post from May

    http://www.vbforums.com/showthread.p...ighlight=merge

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

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