Beginner Question - Data Binding to a TextBox - Follow-up

J

jimscott77

Can someone help with the following problem?

Original Question:
TextBox1 is bound to a dataset and at runtime shows a value of "5"
I am trying to use that value as follows:
TextBoxA.Text = TextBox1.Text
The result that I'd expect is that TextBoxA.Text would equal 5.
The result that I get is that TextBoxA.Text equals "", or wit a
Val(function) it equals 0.

How do I get TextBoxA to equal 5?

Update:
I actually have three such values that I am trying to use and am having
the same problem on all 3.

At runtime
TextBox1.Text shows a value of 5 but has a value of ""
TextBox2.Text shows a value of 18 but has a value of ""
TextBox3.Text shows a value of 24 but has a value of ""

However:
If I change the value in any one (not all three) of the textboxes then
the values of all the textboxes are updated.
When I change the value in TextBox1 to 6 then
TextBox1.Text shows a value of 6 and has a value of 6
TextBox2.Text shows a value of 18 but has a value of 18
TextBox3.Text shows a value of 24 but has a value of 24
(I am testing this by having the values of 3 other textboxes reflect
the values of TextBox1, 2, & 3)
How do I get the values to reflect what is shown without changing one
of the original values?
Thanks again,

Jim (the beginner)

I am using Visual Basic in Visual Studio.Net
 
J

jimscott77

tried me.validate on form load. No effect.
I am using a tabcontrol with multiple tabs. TextBox1-3 are on a
"Settings" tab. The target textboxes are on an "Accounts" tab.
I realize now that I do not have to edit one of TextBox1-3 for the
update to work. I do have to click on the "Settings" tab. Once I do
so all is well.
I have tried to use the focus method [tabsettings.focus()] during form
load but focus is not changed to the "Settings" tab.

Any help would be appreciated.

Thanks, Jim
 
C

Cor Ligthert [MVP]

Jim,

Are you sure of what you write, this code gives for me "WhatEver" in both
textboxes.

\\\
Dim dt As New DataTable
dt.Columns.Add("MyColumn")
dt.LoadDataRow(New Object() {"WhatEver"}, True)
TextBox1.DataBindings.Add("Text", dt, "MyColumn")
TextBox2.Text = TextBox1.Text
///

(However of course not when I change the text in textbox1, it is a string
that is placed not a reference)

I hope this helps,

Cor
 
J

jimscott77

The application I'm putting together uses a tabcontrol and the
textboxes I'm trying to read from are on a different tab ("Settings")
than the ones I'm trying to update ("Accounts"). TextBox1 is on
tabSettings and TextBox2 is on tabAccounts. The data is coming from an
Access data base through an OleDbConnection and the textboxes on
tabSettings are bound to particular fields in the dataset. Once
tabSettings receives focus then TextBox2.Text = TextBox1.Text works,
but not before. Note that when I click on the tabSettings the values
that I want do show in the textboxes, its just that until I click on
that tab the values are not available to the rest of the tabs on the
form.

I did find a work around though. On Form Load I used a datareader to
pull in the same data and assign it individually to the text property
of the textboxes

OleDbConnection1.Open()
objCommand = New OleDbCommand("select * from tblsettings",
OleDbConnection1)
objReader = objCommand.ExecuteReader
objReader.Read()

txtRateDefault.Text = objReader("ratedefault")
txtRateMin.Text = objReader("ratemin")
txtRateMax.Text = objReader("ratemax")

Once the textboxes had been updated by code then the information was
available elsewhere in the form without having to first go to the tab
in question

I would like an easier solution though and to understand why this was
an issue at all. It seems it should be straight forward task.

Regardless, thanks for your help.

Jim (the beginner)
 
G

Guest

Hi Jim,

Have you tried using a datatable instead of a dataset? I tried it and it
works fine for me. I created a basic form with a tabcontrol and a button.
Each tab has a text box on it.

Dim sqlStr As String
Dim cnString As String
Dim cn As OleDb.OleDbConnection
Dim cmd As OleDb.OleDbCommand
Dim da As OleDb.OleDbDataAdapter
Dim dt As DataTable

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
initializeConnection()
End Sub

Public Function initializeConnection()
Try
sqlStr = "SELECT * FROM Metrics"
cnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=c:\METRIC\MetricEngine.mdb"
cn = New OleDb.OleDbConnection(cnString)
cmd = New OleDb.OleDbCommand(sqlStr, cn)
cn.Open()

da = New OleDb.OleDbDataAdapter(cmd)
dt = New DataTable
da.Fill(dt)

cn.Close()
cmd.Dispose()
cn.Dispose()
cmd = Nothing
cn = Nothing

Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
fill()
End Sub

Private Sub fill()
TextBox1.Text = dt.Rows(0).Item(1)
TextBox2.Text = TextBox1.Text
MessageBox.Show(TextBox2.Text)
'the value displayed is the same value displayed in text box one
End Sub
 
C

Cor Ligthert [MVP]

Jim,

Can you tell next time that your textbox was not visible when you tried it.

This is a kind of normal behaviour.

Cor
 
J

jimscott77

I created the dataconnection, dataadapter, and the dataset using the
gui interface (drag and drop from Toolbox and Server Explorer) rather
than through code. The text boxes were then each bound to the dataset
in their respective properties windows. The only code I had to write
was:
DStblSettings1.Clear()
DAtblSettings.Fill(DStblSettings1, "tblSettings")
And then I ran into my problem.

Using code to directly assign values to the textbox (same values as
were there in the first place) solved the problem. Your code does that
assignment as well and appears to be easier to work with, especially on
larger problems.

I would still like to understand why the data binding process worked
(or didn't work) the way it did or if someone else, using the same
process, generated the same problem.

Thanks for your input. I appreciate your response.

Jim
 
J

jimscott77

THIS WORKS!!!!

ORIGINAL CODE:
DStblSettings1.Clear()
DAtblSettings.Fill(DStblSettin­gs1, "tblSettings")

IF I ADD THE FOLLOWING ALL IS WELL:
Me.tabControlMain.SelectedTab = tabSettings
Me.tabControlMain.SelectedTab = tabHome

By programatically cycling to the tabSettings tab and then back to
tabHome (where I want to start) before I do my update it seems to
initiallize the values in TextBox1, 2, & 3 and now the following works.
TextBoxA.Text = TextBox1.Text

Don't ask me why.

Thanks for everyone's help.

Jim (the beginner)
 

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