NEWBIE Q re binding a textbox to a dynamically created datset fiel

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
 
G

Guest

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
 
B

Bart Mermuys

Hi,

Suzie said:
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):
[code snipped]

When you bind, the DataSource is ds.Tables("Authors") :
txtLName.DataBindings.Add("Text", ds.Tables("Authors"), "au_lname")

But when you ask for the CurrencyManager the DataSource is ds :
Me.BindingContext(ds, "Authors").Position = 0

The reason it doesn't work is because the DataSource isn't the same. So you
have to change either the binding or the way you get the currencymanager, so
that they both use the same DataSource, eg.:

Both use ds.Tables("Author") :
txtLName.DataBindings.Add( "Text", ds.Tables("Authors"), "au_lname" )
Me.BindingContext( ds.Tables("Authors") ).Position = 0
...

---- Or both use ds :
txtLName.DataBindings.Add( "Text", ds, "Authors.au_lname" )
Me.BindingContext( ds, "Authors" ).Position = 0
...


HTH,
Greetings
 
G

Guest

Hi Bart

THANK U so much!

I would have thought that
Me.BindingContext(ds, "Authors")
and Me.BindingContext( ds.Tables("Authors") )
would be two different but equivalent ways of referencing the 'Authors'
table inside the 'ds' dataset.

I tried what you suggested and it worked, so clearly it DOES make a big
difference!

thanks so much once again. I'm sure i'll be here soon again with another
beginners' problem ;)




Bart Mermuys said:
Hi,

Suzie said:
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):
[code snipped]

When you bind, the DataSource is ds.Tables("Authors") :
txtLName.DataBindings.Add("Text", ds.Tables("Authors"), "au_lname")

But when you ask for the CurrencyManager the DataSource is ds :
Me.BindingContext(ds, "Authors").Position = 0

The reason it doesn't work is because the DataSource isn't the same. So you
have to change either the binding or the way you get the currencymanager, so
that they both use the same DataSource, eg.:

Both use ds.Tables("Author") :
txtLName.DataBindings.Add( "Text", ds.Tables("Authors"), "au_lname" )
Me.BindingContext( ds.Tables("Authors") ).Position = 0
...

---- Or both use ds :
txtLName.DataBindings.Add( "Text", ds, "Authors.au_lname" )
Me.BindingContext( ds, "Authors" ).Position = 0
...


HTH,
Greetings


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top