G
Guest
Hi!
I am new to Vb.net, so pls excuse if the Q seems silly! It is to illustrate
a problem i am having with navigating through a dataset table, and having the
bound text fields not showing the current record, but only showing (being
stuck) on the first record permanently!
[If I create the connection, dataadapter and datset objects in design time,
and then bind the text property of the text boxes to the appropriate fields
in design time, then i do not have this problem. however, my requirements
are to be able to do this in runtime - as illustrated in this example - and i
just cannot get it right]
I have a very simple form having two textbox fields showing the First Name
and Last Name and 4 buttons to navigate thru the records. I populate the
dataset in the formload event. The form code is shown below (am using VS.Net
2003):
Imports System.Data
Imports System.Data.SqlClient
Public Class Form1
Inherits System.Windows.Forms.Form
Private sqlconn As SqlConnection
Private da As SqlDataAdapter
Private ds As DataSet
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
sqlconn = New SqlClient.SqlConnection
sqlconn.ConnectionString = "Integrated Security=True;" & _
"Data Source=LocalHost;Initial Catalog=Pubs;"
sqlconn.Open()
ds = New DataSet
da = New SqlDataAdapter("Select au_lname, au_fname from authors",
sqlconn)
da.Fill(ds, "authors")
txtFName.DataBindings.Add("Text", ds.Tables("Authors"), "au_fname")
txtLName.DataBindings.Add("Text", ds.Tables("Authors"), "au_lname")
End Sub
Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnFirst.Click
Me.BindingContext(ds, "Authors").Position = 0
End Sub
Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnLast.Click
Me.BindingContext(ds, "Authors").Position = _
Me.BindingContext(ds, "Authors").Count - 1
End Sub
Private Sub btnPrev_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnPrev.Click
Me.BindingContext(ds, "Authors").Position -= 1
End Sub
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnNext.Click
Me.BindingContext(ds, "Authors").Position += 1
End Sub
End Class
I am new to Vb.net, so pls excuse if the Q seems silly! It is to illustrate
a problem i am having with navigating through a dataset table, and having the
bound text fields not showing the current record, but only showing (being
stuck) on the first record permanently!
[If I create the connection, dataadapter and datset objects in design time,
and then bind the text property of the text boxes to the appropriate fields
in design time, then i do not have this problem. however, my requirements
are to be able to do this in runtime - as illustrated in this example - and i
just cannot get it right]
I have a very simple form having two textbox fields showing the First Name
and Last Name and 4 buttons to navigate thru the records. I populate the
dataset in the formload event. The form code is shown below (am using VS.Net
2003):
Imports System.Data
Imports System.Data.SqlClient
Public Class Form1
Inherits System.Windows.Forms.Form
Private sqlconn As SqlConnection
Private da As SqlDataAdapter
Private ds As DataSet
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
sqlconn = New SqlClient.SqlConnection
sqlconn.ConnectionString = "Integrated Security=True;" & _
"Data Source=LocalHost;Initial Catalog=Pubs;"
sqlconn.Open()
ds = New DataSet
da = New SqlDataAdapter("Select au_lname, au_fname from authors",
sqlconn)
da.Fill(ds, "authors")
txtFName.DataBindings.Add("Text", ds.Tables("Authors"), "au_fname")
txtLName.DataBindings.Add("Text", ds.Tables("Authors"), "au_lname")
End Sub
Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnFirst.Click
Me.BindingContext(ds, "Authors").Position = 0
End Sub
Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnLast.Click
Me.BindingContext(ds, "Authors").Position = _
Me.BindingContext(ds, "Authors").Count - 1
End Sub
Private Sub btnPrev_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnPrev.Click
Me.BindingContext(ds, "Authors").Position -= 1
End Sub
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnNext.Click
Me.BindingContext(ds, "Authors").Position += 1
End Sub
End Class