Question How to select values from database in textboxes and move them on buttons

yshiraskar

New member
Joined
Jul 15, 2010
Messages
2
Programming Experience
Beginner
Hi all,
I am creating a website for online examination system. I want ot selet QuestionID and Question field values in textboxes namely txtQID and txtQue From database table Question. I am having two buttons namely Prev and next on the web form. How can I move the values on click on the buttons.
 

JayWalker

Member
Joined
Jun 20, 2011
Messages
15
Programming Experience
Beginner
First and foremost, I’m not a VB.NET pro so bare with me. :) And secondly, this isn’t a web based program but since ur thread has 0 replies, I thought of sharing this might give you at least a slight idea on how to accomplish what ur asking.
This is a little windows based program which I’ve created. It does pretty much the what you have described. Displaying data from a database and you can switch between records using buttons.

What I have done is, I’ve added a SqlDataAdapter and pointed it to the database. And generated a Dataset. (what you see in the code as ‘Master’ is the table I’ve chosen) Next, I’ve created the UI and named the controls appropriately. (shown below) and bound the two textboxes to the data coming from the database. To do that, select the DataBindings property from the textbox properties and choose Text and then the field (in this case), ‘Code’. (Refer the screenshot if this is a bit confusing)

pic.pngdata binding.png

And then there is the coding part which isn’t hard to understand at all. :)

VB.NET:
Public Class Exercise_01

    Private Sub Exercise_01_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        SqlDataAdapter1.Fill(DataSet11)
        chPos()
    End Sub

    'sub procedure
    'lblDisplay is the Label I've put in the form to show the sequence number of the record.
    Sub chPos()  
        lblDisplay.Text = (Me.BindingContext(DataSet11, "Master").Position + 1).ToString
    End Sub

    Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.Click
        Me.BindingContext(DataSet11, "Master").Position = 0
        chPos()
    End Sub

    Private Sub btnPrev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrev.Click
        Me.BindingContext(DataSet11, "Master").Position -= 1
        chPos()
    End Sub

    Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
        Me.BindingContext(DataSet11, "Master").Position += 1
        chPos()
    End Sub

    Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLast.Click
        Me.BindingContext(DataSet11, "Master").Position = Me.BindingContext(DataSet11, "Master").Count - 1
        chPos()
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        Me.BindingContext(DataSet11, "Master").AddNew()
    End Sub

    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        Dim chDataset As DataSet
        Me.BindingContext(DataSet11, "Master").EndCurrentEdit()

        If DataSet11.HasChanges Then
            chDataset = DataSet11.GetChanges
            SqlDataAdapter1.Update(chDataset)
        End If
        DataSet11.AcceptChanges()
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        Me.BindingContext(DataSet11, "Master").RemoveAt(Me.BindingContext(DataSet11, "Master").Position)
        chPos()
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.BindingContext(DataSet11, "Master").CancelCurrentEdit()
    End Sub

    Private Sub btnCancelAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelAll.Click
        DataSet11.RejectChanges()
    End Sub
End Class

Well, that’s about it. Hope it helped you at least a little bit. :)
Cheers!
I.J
 
Top Bottom