Solved

VB.Net Custom BindingNavigator with SQL

Posted on 2013-10-23
4
749 Views
Last Modified: 2013-10-23
I am upgrading an access database to vb.net/sql winforms.  Our customers (Police Departments) have a large amount of data in their tables.  They also 'need' the ability to scroll through records.  In access this is simple, in .Net it poses a problem.

I was going to use Datatables with all the table data, however the amount of data would not only consume too much memory, it would be too slow upon startup.

I decided to 'customize' the bindingnavigator.  The datatable(s) will hold only the first 100 records and I would also have a 'RecordPointer' where upon if the user scrolls to record 101, it will then clear out the datatable, load the next 100 records and rebind.  It seems to be working ok for the most part, however I am having a few issues.

When going past record 100, then scrolling back, it reloads the datatable but it not going back more than one record.  Here is a sample of my code that I have, maybe one of you wizards can help me on this?

Form load:


Public RecordPointer As Integer = 0
 Public Sub New()
        Try

Dim qryCreateTableCS As New SqlCommand("SELECT * FROM  (SELECT ROW_NUMBER()  OVER (ORDER BY IncnoID DESC) AS Row, *,CONVERT(VARCHAR, TimeComp, 108) AS newTimeComp,CONVERT(VARCHAR, Dispatched, 108) AS newDispatched,CONVERT(VARCHAR, Arrived, 108) AS newArrived,CONVERT(VARCHAR, TimeClear, 108) AS newTimeClear,CONVERT(VARCHAR, OtherTime, 108) AS newOtherTime FROM CALLSERV) AS CS WHERE Row BETWEEN 1 AND 99", connection)

            adapter.SelectCommand = qryCreateTableCS
            adapter.Fill(SPISDataSet, "dtCallServ")
 CSBindingSource1.DataSource = SPISDataSet.Tables("dtCallServ")

            BindingNavigator1.BindingSource = CSBindingSource1
 Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
End Sub

Private Sub btnNextRecord_Click(sender As Object, e As EventArgs) Handles btnNextRecord.Click
        RecordPointer = RecordPointer + 1

        If RecordPointer Mod 100 = 0 Then
            GetNextRecord()
        Else
            CSBindingSource1.MoveNext()
            Application.DoEvents()
            txtCurrentRecord.Text = txtCurrentRecord.Text + 1
        End If
    End Sub
Private Sub GetNextRecord()
        Dim cmd As SqlCommand
        Dim sql As String

        Dim qryCreateTableCS As New SqlCommand("SELECT * FROM  (SELECT ROW_NUMBER()  OVER (ORDER BY IncnoID DESC) AS Row, *,CONVERT(VARCHAR, TimeComp, 108) AS newTimeComp,CONVERT(VARCHAR, Dispatched, 108) AS newDispatched,CONVERT(VARCHAR, Arrived, 108) AS newArrived,CONVERT(VARCHAR, TimeClear, 108) AS newTimeClear,CONVERT(VARCHAR, OtherTime, 108) AS newOtherTime FROM CALLSERV) AS CS WHERE Row BETWEEN " & RecordPointer & " AND " & RecordPointer + 100, connection)

        SPISDataSet.Tables("dtCallServ").Clear()
        Application.DoEvents()

        adapter.SelectCommand = qryCreateTableCS
        adapter.Fill(SPISDataSet, "dtCallServ")

        CSBindingSource1.DataSource = SPISDataSet.Tables("dtCallServ")
         Application.DoEvents()
    End Sub
    Private Sub btnPreviousRecord_Click(sender As Object, e As EventArgs) Handles btnPreviousRecord.Click
        If RecordPointer > 1 Then
            RecordPointer = RecordPointer - 1

            If RecordPointer Mod 100 = 0 Then
                GetPreviousRecord()
            Else
                CSBindingSource1.MovePrevious()
                Application.DoEvents()
                txtCurrentRecord.Text = txtCurrentRecord.Text - 1
            End If
        End If
    End Sub

    Private Sub GetPreviousRecord()
        Dim cmd As SqlCommand
        Dim sql As String

        Dim qryCreateTableCS As New SqlCommand("SELECT * FROM  (SELECT ROW_NUMBER()  OVER (ORDER BY IncnoID DESC) AS Row, *,CONVERT(VARCHAR, TimeComp, 108) AS newTimeComp,CONVERT(VARCHAR, Dispatched, 108) AS newDispatched,CONVERT(VARCHAR, Arrived, 108) AS newArrived,CONVERT(VARCHAR, TimeClear, 108) AS newTimeClear,CONVERT(VARCHAR, OtherTime, 108) AS newOtherTime FROM CALLSERV) AS CS WHERE Row BETWEEN " & RecordPointer - 100 & " AND " & RecordPointer & " ORDER BY Incno ASC", connection)

        SPISDataSet.Tables("dtCallServ").Clear()
        Application.DoEvents()

        adapter.SelectCommand = qryCreateTableCS
        adapter.Fill(SPISDataSet, "dtCallServ")

        CSBindingSource1.DataSource = SPISDataSet.Tables("dtCallServ")
        Application.DoEvents()
    End Sub

Open in new window

0
Comment
Question by:K-9
  • 3
4 Comments
 

Author Comment

by:K-9
ID: 39594112
I found out why it's only going to the first record, when I go past 100 and then back below, its because of my query.  I actually have to do a 'MoveNext' instead of a 'MovePrevious' if I am going backwards.  However, I need to figure out to only doing this when I am not in my first set of 100 records.

I can't query to see if a IncnoID is within' the first 100 records since larger PDs can use 100 records within a short period of time.  Any ideas?
0
 

Accepted Solution

by:
K-9 earned 0 total points
ID: 39594147
I was able to get it, I set a public variable that just increments on movenext and decrements on moveprevious.

 RecordPointer = RecordPointer - 1
        If RecordPointer Mod 100 = 0 Then
            GetPreviousRecord()
        Else
            If RecordPointer > 0 And RecordPointer < 100 Then
                CSBindingSource1.MoveNext()
            Else
                CSBindingSource1.MovePrevious()
                Application.DoEvents()
            End If
        End If

        txtCurrentRecord.Text = txtCurrentRecord.Text - 1

Open in new window

0
 

Author Closing Comment

by:K-9
ID: 39594148
Was able to resolve it myself.
0
 
LVL 21
ID: 39594561
Thanks for sharing your solution!
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now