?
Solved

Enable button based on event

Posted on 2013-11-24
8
Medium Priority
?
282 Views
Last Modified: 2014-01-04
Hi

I need some help as I am a new user to vb.net with enabling a button based on an event. The scenario is I have a listview which populates 100 records at a time based on a access query.

I then have 2 buttons, 'btnNextPostings' and 'btnPreviousPostings' which are disabled at form_load. The idea is when the list is populated the next button becomes active which shows the next 100 postings. Think of it as navigating backwards and forwards 100 records at a time. You will see from my code, that I enable the next button 'btnNextPostings' when I populate the list.

I would be grateful if someone could help with this or point me in the direction for further help. Many thanks


Sub filllistview()
        Try
            'creatconn()
            oledbCnn.ConnectionString = My.Settings.storageConnectionString
            oledbCnn.Open()
            Dim oledbCmd As OleDbCommand = New OleDbCommand("Select TOP 100 * from Postings WHERE Customer = '" & newvar & "' ORDER BY Date DESC", oledbCnn)
            dr = oledbCmd.ExecuteReader()

            Dim tmpColor As Color = Color.Green

            'clear items in the list before populating with new values
            ListView1.Items.Clear()

            While dr.Read()

                ListView1.Items.Add(CDate(dr(4)).ToShortDateString()).UseItemStyleForSubItems = False
                ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(dr(1).ToString())
                ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(dr(11).ToString())
                ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(dr(7).ToString())
                With ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(CDbl(dr(5)).ToString("C"))
                    If CDbl(dr(5)) < 0 Then
                        .ForeColor = Color.Red
                        .BackColor = Color.Gainsboro
                        '.Font = New Font(Font.FontFamily, Font.Size, FontStyle.Bold)
                    Else
                        .ForeColor = tmpColor
                    End If
                End With
                ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(dr(14).ToString())
                ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(dr(3).ToString())


            End While

            'autosize each column in listview
            For Each col As ColumnHeader In ListView1.Columns
                col.Width = -2
            Next col

            'refresh the list with new data
            ListView1.Refresh()

            'Enable the posting previous button
            btnNextPostings.Enabled = True

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            dr.Close()
            oledbCnn.Close()
        End Try
    End Sub

Open in new window

0
Comment
Question by:peter-cooper
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39672798
So you need to do 2 things

1) Enable Next button but only if there are more than 100 records in database. This means that you need to run a query first to get the count of total rows. Store this count in a class level variable and then when you populate the list, if this count is greater than 100 then enable the next button. You then also need to maintain the current page number count. When user presses the Next button, you load next 100 rows and then check if you still need to enable the Next button by checking if PageNumber*100 is less than row count.

2) You need to enable the Previous button if current page number is not 1.
0
 

Author Comment

by:peter-cooper
ID: 39672876
That is correct Codecruiser. Thanks
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39672993
Have you made a start on 1?
0
DFW AZURE MEETUP TONIGHT FRI 6PM

We will be discussing what Azure Stack is, how does it fit into the suit of offerings that Azure has currently, and where can it fit into your organizations technology stack. We will also be discussing limitations of the platform while covering various applicable scenarios.

 

Author Comment

by:peter-cooper
ID: 39673155
This is all I have so far. I am new user to vb.net. I use this after try statement. The biggest problem I have is finding a place to sart. Should it be a class, a module, or inline. So any guidance would be great. Thanks

btnNextPostings.Enabled = ListView1.Items.Count > 0
        If (ListView1.Items.Count < 10) Then
            btnNextPostings.Enabled = False
        End If

Open in new window

0
 

Author Comment

by:peter-cooper
ID: 39675811
Any further help with this please. Thanks
0
 

Author Comment

by:peter-cooper
ID: 39677316
I have tried this code but throws an error of:

The SELECT statement includes a reserved word or an argument name that is misspelled or missing, or the punctuation is incorrect.

   
Dim Row_Per_Page As Integer = 4
    Dim TotRows As Integer = 17
    Dim Page_Number As Integer = 2

    Dim oledbCmd As OleDbCommand = New OleDbCommand("Select TOP '" & Row_Per_Page & "' *, Count(*) As '" & TotRows & "' From [Select Top('" & TotRows & "' - (('" & Page_Number & "' - 1) * '" & Row_Per_Page & "'))From Postings Order By [Date] DESC] Order By [Date] ASC", oledbCnn)

Open in new window


I have checked and double checked but cannot find the error. can you help. Thanks
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 2000 total points
ID: 39724699
You cannot use * with any aggregate function such as Count without using Group By clause.
0
 

Author Closing Comment

by:peter-cooper
ID: 39756555
Thanks
0

Featured Post

Stressed Out?

Watch some penguins on the livecam!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

770 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