简体   繁体   English

Vb.net下一个和上一个按钮代码

[英]Vb.net next and previous button code

I am making a vb.net crud web application. 我正在制作一个vb.net crud Web应用程序。 I do not want to use any grid rather want to fetch records from sql database into text boxes. 我不想使用任何网格,而是想从sql数据库中获取记录到文本框中。 So far I have succeeded to show a record in a text box on pressing a button from the sql database. 到目前为止,按sql数据库中的一个按钮,我已经成功在文本框中显示了一条记录。 Now my question is can we design a next and previous button which would show next or previous records from sql database. 现在我的问题是我们是否可以设计一个下一个和上一个按钮,以显示sql数据库中的下一个或上一个记录。 It would be great if any body can share code to do this. 如果任何机构都可以共享代码来做到这一点,那就太好了。

Below is my code to fetch a single record in the textbox 下面是我在文本框中获取单个记录的代码

Protected Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
     Dim myConn As SqlConnection = New     SqlConnection("Server=.\SQLEXPRESS;AttachDbFilename=D:\MyFolder\MyDatabaseData.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True")

    Dim dt As New DataTable
    Dim a As Integer
    a = 2

    Try
        myConn.Open()

        Dim SelectCommand As New SqlClient.SqlCommand("SELECT names FROM enames", myConn)

        txtOEID.Text = CStr(SelectCommand.ExecuteScalar())
        MsgBox("data is selected and show in the textbox and the connection is also closing")
        myConn.Close()


    Catch ex As Exception
        MsgBox(ex.ToString())
            End Try

End Sub

You can use where clause. 您可以使用where子句。

For example in your table you have : id (integer), last_name (char), first_name(char). 例如,在您的表中,您具有:id(整数),last_name(字符),first_name(字符)。 All that you have to do is to "play" with select command. 您要做的就是使用select命令“播放”。 Your select will be like: "select * from aTable where id between first_index and last_index" . 您的选择将类似于: "select * from aTable where id between first_index and last_index" first_index and last_index are limits of records that you want to fetch, and in buttons action you have to increment/decrement that indexes. first_index和last_index是要获取的记录的限制,并且在按钮操作中必须递增/递减该索引。

Another solution can be copying values that you obtain into a list. 另一种解决方案是将获得的值复制到列表中。

Good luck. 祝好运。

if you already have datatable, you can continue with datarow from the table 如果已经有数据表,则可以从表中继续进行数据行

Dim dr As Datarow 

dr = dt.rows(currentRowIndex)
txtName = dr.item("name")
txtProf = dr.item("profession")

etc 

Hope it helps ! 希望能帮助到你 !

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM