使用第一个下一个上一个按钮点击事件显示数据库到文本框中的数据 [英] DISPLAY DATABASE DATA IN TO TEXT BOXES USING FIRST NEXT PREVIOUS LAST BUTTONS CLICK EVENTS

查看:74
本文介绍了使用第一个下一个上一个按钮点击事件显示数据库到文本框中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI朋友我希望在这里使用第一个下一个上一个按钮显示数据库中的数据...



我已经编写了代码但是它没有工作正确的工作为第一个和最后一个按钮点击事件它显示数据库数据到文本框但是当我点击下一个和以前的按钮它不能正常工作它只工作第二次正确显示第二次点击以前的按钮它不显示以前的记录在TEXTBOX中显示相同的文本框。



这是我的代码请帮助我解决这个问题..



感谢你的朋友....



HI FRIENDS I TO WANT DISPLAY DATABASE DATA IN TO TEXT BOXES HERE IAM USING FIRST NEXT PREVIOUS LAST BUTTONS..

I HAVE WRITTEN THE CODE BUT ITS NOT WORKING PROPERLY IT WORKS FOR FIRST AND LAST BUTTONS CLICK EVENTS IT DISPLAYS DATABASE DATA INTO TEXT BOXES BUT WHEN IAM CLICK NEXT AND PREVIOUS BUTTONS IT DOES NOT WORK PROPERLY IT WORKS ONLY DISPLAYS FIRST TIME CORRECTLY SECOND TIME IAM CLICKING PREVIOUS BUTTON IT DOES NOT DISPLAY PREVIOUS RECORD IN THE TEXTBOXES IT DISPLAYS SAME RECORD IN TEXTBOX.

HERE IS MY CODE PLEASE HELP ME FOR THIS PROBLEM..

THANKING YOU FRIENDS....

Dim rno As Integer
        cmd.CommandText = " select * from customerr"
        Dim da As New SqlDataAdapter
        Dim dt As New DataTable
        da.SelectCommand = cmd
        cmd.Connection = connection
        da.Fill(dt)

        rno = dt.Rows.Count - 1
        If rno > 0 Then
            rno -= 1
            If dt.Rows.Count > "0" Then
                txtcustid.Text = dt.Rows(rno)(0).ToString()
                txtcustname.Text = dt.Rows(rno)(1).ToString()
                txtcustaddress.Text = dt.Rows(rno)(2).ToString()
                
            End If
        End If



        cmd.CommandText = "select * from customerr"
        Dim da As New SqlDataAdapter
        Dim dt As New DataTable
        da.SelectCommand = cmd
        cmd.Connection = connection
        da.Fill(dt)
       
        rno = dt.Rows.Count - 1
        If rno < dt.Rows.Count - 1 Then
            rno += 1
            If dt.Rows.Count > 0 Then
                txtcustid.Text = dt.Rows(rno)(0).ToString()
                txtcustname.Text = dt.Rows(rno)(1).ToString()
                txtcustaddress.Text = dt.Rows(rno)(2).ToString()
               
            End If
        End If

推荐答案

您的解决方案仅适用于第一个和最后一个客户,因为您一直在寻找

rno = dt.Rows.Count -1(即你永远不会前进或后退)



你需要的是把变量放入成员变量(比如_currentRowIndex)然后你会增加/减少。



你会加载表ONCE并将_currentRow设置为零并显示dt.Rows(0)。然后在下一步点击你将

Your solution would work for first and last customer only because you're always looking at
rno = dt.Rows.Count -1 (i.e. you're never going forward or backward)

What you need is put a variable into member variable (say _currentRowIndex) which you would then increase / decrease.

You would load the table ONCE and set _currentRow to zero and show dt.Rows(0). Then on Next click you would
' This will prevent your next from going over last item (alternatively you can set it to zero and not return thus starting from the first)
        If _currentRow +1 > dt.Rows.Count - 1 Then return

        _currentRow += 1
        With dt.Rows(0)
             txtcustid.Text = .Item("customer_id").ToString()
             txtcustname.Text = .Item("customer_name").ToString()
             txtcustaddress.Text = .Item("customer_addresss).ToString()
        End With





使用Prev按钮你也会这样做,除非你与零比较而不是dt.rows.count-1



注意:我使用列名而不是索引填充字段。这有几个优点:

- 你不必记住哪个索引是哪个(在你的情况下文本框名称给出一个线索,但是当你添加新字段或改变现有字段的顺序时会发生什么?)

- 当你回到几个时几个月,您将不必重新检查您的字段顺序 - 特别是如果有超过这三个。



With Prev button you do the same, except you're comparing with zero instead of dt.rows.count-1

NOTE: I used column names instead of indexes filling fields. This has several advantages:
- you don't have to remember which index is which (in your case textbox names give a clue, but what happens when you add new field or change the order of the existing ones?)
- when you get back to it in several months, you will not have to recheck your field order - especially if there are more then those three.


FRIEND here _currentRow是0或1
FRIEND here _currentRow is "0" or "1"


这篇关于使用第一个下一个上一个按钮点击事件显示数据库到文本框中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆