Link to home
Start Free TrialLog in
Avatar of HaryMary
HaryMaryFlag for Saudi Arabia

asked on

VB.Net Dataset Navigation buttons

Dears,

1- I have a textbox1 in my aspx page with only one button. The textbox1 is bound to a dataset already filled with data through data adapter. I want to move to the next record in the dataset every time I click the button. How can I,

I tried using this each time the button is pressed, but I failed.

static x as integer
textbox1.text = MyDataset.tables.rows(x)
x= x+1

2- Is it possible to use static variables in the aspx pages, so the variable keep its value.
Avatar of L00M
L00M
Flag of United States of America image

I accomplish this using a dataview as follows (I have four buttons: Go First, Go Previous, Go Next, Go Last - Let me know if you need further clarification):

' Global
    Dim CurrentRow As Integer

...
Dim dvHeader As DataView
dvHeader = ds0.Tables(0).DefaultView
...

    Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.Click
        GoFirst()
    End Sub

    Private Sub GoFirst()
        CurrentRow = 0
        Me.BindingContext(dvHeader).Position = 0
    End Sub

    Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
        GoPrevious()
    End Sub

    Private Sub GoPrevious()
        CurrentRow -= 1
        If CurrentRow < 0 Then CurrentRow = 0
        Me.BindingContext(dvHeader).Position = CurrentRow
    End Sub

    Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
        GoNext()
    End Sub

    Private Sub GoNext()
        CurrentRow += 1
        If CurrentRow > ds0.Tables(0).Rows.Count - 1 Then CurrentRow = ds0.Tables(0).Rows.Count() - 1
        Me.BindingContext(dvHeader).Position = CurrentRow
    End Sub

    Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLast.Click
        GoLast()
    End Sub

    Private Sub GoLast()
        CurrentRow = ds0.Tables(0).Rows.Count() - 1
        Me.BindingContext(dvHeader).Position = CurrentRow
    End Sub


Neil Brewer
Also Global:
    Dim dvHeader As DataView

In your code, there is an error. when you are using static variables, the value will not change, and any incrementation will not work on that. Instead use a normal variable and increment it.

I go with @L00M

You can use a part of his code:

    Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
        GoNext()
    End Sub

    Private Sub GoNext()
        CurrentRow += 1
        If CurrentRow > ds0.Tables(0).Rows.Count - 1 Then CurrentRow = ds0.Tables(0).Rows.Count() - 1
        Me.BindingContext(dvHeader).Position = CurrentRow
    End Sub


to achevie the result you are expecting. Thats more clear and you need not maintain any variables, application takes care of it.
BTW, the reason I put GoNext into a different function is so I could wire up the scroll wheel on the mouse easily:

    Private Sub PatientCard_MouseWheel(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseWheel
        If e.Delta < 0 Then
            GoNext()
        Else
            GoPrevious()
        End If
    End Sub

That can be improved upon, but works ok.
NB
Looks like you are an expert in dotnet programming @L00M
I wouldn't say Expert... more like, thrown into the deep end... swim or drown. ;) It's taken many many hours to even learn how to tread. .NET is nice for some tasks, but it seems like killing a mouse with an elephant gun for so many simple tasks. :)
hmm, thats true. But, unfortunately, we have to go with it sometimes, till we find an alternative.
Hope that we get a way out soon..
Avatar of HaryMary

ASKER

Friends,

The variable CurrentRow wouldn't keep or store the last value. I tried using global.asax file to declare the CurrentRow variable, but my webform wouldn't have access to that variable ???
 
Hey, there is some issue that I too noticed with Global Variables in .NET.  As a turnaround, I used a hidden field which was assigned the value of the variable and the value is retrieved wheever required in the post back.

lets say, you have current row=-1 initially, pass on that value to the hidden valriable from CodeBEhind.

When ever you do post back, retrieve the value and again assign it to the hidden field.

Let me check if there are any alternatives available
It would be useful for me also.
Dears,

The solution is in using ASP.Net and its ViewState property where you can initialize it as follows

If not IsPostBack Then
   Me.ViewState("xx") = 0
Else
   Me.ViewState("xx") = me.ViewState("xx") + 1
End If


Cheers
ASKER CERTIFIED SOLUTION
Avatar of modulo
modulo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial