Hi!
I am newbie 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 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
Suzie - 14 Aug 2005 14:13 GMT
The form opens with the text boxes show the values relating to the first
record (as expected):
FName= Abraham
LName= Bennet
What i cant understand however is why pressing the navigation buttons does
not change the text in these textboxes - after all they are bound to the
underlying dataset in the form load.
When putting a breakpoint in the button handlers i can verify that the
dataset is indeed moving on through the records by using
? BindingContext(ds, "Authors").Position
in the immediate window.
It clearly shows that the position is NOT on 0 (the first record) - however
only the fiorst record's details show in the textbox.
WHAT AM I DOING WRONG or MISSING??
PLEASE HELP ME SOMEBODY!
What is even more incredulous is that if i place a datagrid on the form and
then
bind it in the form load event handler with the statements
DataGrid1.DataSource = ds
DataGrid1.DataMember = "Authors"
then it is plain to see the navigation buttons working as the records move
from one record to the next in the datagrid.
HOWEVER - the textboxes are still blisffully stuck on the first record thru
all of this!
HELP pls! ;)
thks
Suzie
Suzie - 14 Aug 2005 14:19 GMT
The form opens with the text boxes show the values relating to the first
record (as expected):
FName= Abraham
LName= Bennet
What i cant understand however is why pressing the navigation buttons does
not change the text in these textboxes - after all they are bound to the
underlying dataset in the form load.
When putting a breakpoint in the button handlers i can verify that the
dataset is indeed moving on through the records by using
? BindingContext(ds, "Authors").Position
in the immediate window.
It clearly shows that the position is NOT on 0 (the first record) - however
only the fiorst record's details show in the textbox.
WHAT AM I DOING WRONG or MISSING??
PLEASE HELP ME SOMEBODY!
What is even more incredulous is that if i place a datagrid on the form and
then
bind it in the form load event handler with the statements
DataGrid1.DataSource = ds
DataGrid1.DataMember = "Authors"
then it is plain to see the navigation buttons working as the records move
from one record to the next in the datagrid.
HOWEVER - the textboxes are still blisffully stuck on the first record thru
all of this!
HELP pls! ;)
thks
Suzie