Binding Example

M

Miro

I cant seem to find a proper example anywhere that binds a textbox by code.

I have created on the FormLoad a Dataset that has 1 record from my table.
I created a DataRow that holds that record. ( with my columns that were
specified from my commandText.

So what I have done now is I created a textbox on my form.
How do I bind that text box to my DataRow that holds all my columns?
-So It displays the data in the text field, and later so I can put a
"save" button on the form and update the data from the text field back
to the database.

Im using vb.net express and sqlExpress

Thanks

Miro
 
M

Miro

So far, the closest Ive got is this:
TextBox1.Text = CStr(dr("FirstName"))
TextBox2.Text = CStr(dr("LastName"))

its not really binding the value - just displaying it in the textbox.

Im assuming there is a better way however.
(Trying to stay clear of drag and drop wizard )
 
J

Jack Jackson

TextBox1.DataBindings.Add("text", dr, "FirstName")
TextBox2.DataBindings.Add("text", dr, "LastName")
 
M

Miro

Thanks Jack,

That got me on the right track.

I had to put the table name however into the
TextBox1.DataBindings.Add("text", dsStaff.Tables("StaffPerson"),
"FirstName")

Thanks for your help.

Miro
 
J

Jack Jackson

That line of code actually instantiates a New Binding object. You
might want to look up the Binding class in the documentation, it
allows you to do editing of the value when either the textbox is
changed and when the data source is changed. That can be very useful
for some data types such as dates. Databindings.Add also has some
other overloads that might be useful.
 
M

Miro

I must be missing something in their example ( from the help file ) and mine.

Maybe the way I create my dataset.
I dont use a datatable. I use a select command.

-If i dont add my whole line I get an exception error at runtime.

I must be missing some fundamental "key" in the relation with the Dataset and a Datatable.

====
'Dim StaffConnection As New SqlClient.SqlConnection
StaffConnection.ConnectionString = My.Settings.MyConnectionString

' Create the command object
commandStaff.CommandText = "SELECT StaffID, FirstName, LastName, EmailAddress, PhoneNumber, Title, FROM Staff
Where StaffID = @CurStaffID"
commandStaff.Parameters.Add("@CurStaffID", SqlDbType.Int).Value = Me.iStaffMemberID
commandStaff.Connection = StaffConnection

'Open the connection
StaffConnection.Open()

'Fill the dataset
daStaff.Fill(dsStaff, "StaffPerson")

'this just proves to me that there is always only 1 record
If dsStaff.Tables.Count = 1 Then
drStaff = dsStaff.Tables("StaffPerson").Rows(0)
End If

'close the connection
StaffConnection.Close()

'Not binding - just setting value
'TextBox1.Text = dr("FirstName").ToString
'TextBox2.Text = dr("LastName").ToString

TextBox1.DataBindings.Add("text", dsStaff.Tables("StaffPerson"), "FirstName")
TextBox2.DataBindings.Add("text", dsStaff.Tables("StaffPerson"), "LastName")

====
 
J

Jack Jackson

What error do you get?
What line gets the error?
What does "If i dont add my whole line I get an exception error at
runtime." mean?
 
M

Miro

For example if I write this:
TextBox2.DataBindings.Add("text", drStaff, "LastName")
instead of
TextBox2.DataBindings.Add("text", dsStaff.Tables("StaffPerson"), "LastName")

I get the Exception Error ( pasted below ) during runtime. No blue lines or anything during
coding time.

I have a feeling it may be the way I am creating / "binding?" (if thats the right terminology)
my datarow. I must be missing something.


Thanks for your help.



==Exception Error===
System.ArgumentException was unhandled
Message="Cannot bind to the property or column LastName on the DataSource.
Parameter name: dataMember"
ParamName="dataMember"
Source="System.Windows.Forms"
StackTrace:
at System.Windows.Forms.BindToObject.CheckBinding()
at System.Windows.Forms.BindToObject.SetBindingManagerBase(BindingManagerBase lManager)
at System.Windows.Forms.Binding.SetListManager(BindingManagerBase bindingManagerBase)
at System.Windows.Forms.ListManagerBindingsCollection.AddCore(Binding dataBinding)
at System.Windows.Forms.BindingsCollection.Add(Binding binding)
at System.Windows.Forms.BindingContext.UpdateBinding(BindingContext newBindingContext, Binding binding)
at System.Windows.Forms.Binding.SetBindableComponent(IBindableComponent value)
at System.Windows.Forms.ControlBindingsCollection.AddCore(Binding dataBinding)
at System.Windows.Forms.ControlBindingsCollection.Add(String propertyName, Object dataSource, String dataMember,
Boolean formattingEnabled, DataSourceUpdateMode updateMode, Object nullValue, String formatString, IFormatProvider
formatInfo)
at System.Windows.Forms.ControlBindingsCollection.Add(String propertyName, Object dataSource, String dataMember)
....and so on
===end of Exception
 
J

Jack Jackson

What kind of an object is "drStaff"? Whatever it is it has no
property called LastName.
 
M

Miro

I assign dr staff like so ( see code below in thread )
'this just proves to me that there is always only 1 record
If dsStaff.Tables.Count = 1 Then
drStaff = dsStaff.Tables("StaffPerson").Rows(0)
End If

is there something I am missing in my DataRow Assignment?
My Select statement does include it, and I can debug.writeline each column within it.
Perhaps somehow i am missing a binding of column names, and not just column numbers?
 
M

Miro

Jack,

I was just recommended a book that explains binding thru code.
"Programming Microsoft ADO.NET 2.0 Core Reference"
I am going to go pick it up tonight and start reading that thru.

I realize your time is important too, butI think that I should go thru some of the book examples,
before re-attempting to try to bind properly.
I have issues with binding and updating, and i think it would be more fair to you
( and others on this newsgroup ), if i did some more due diligence.

I thank you very much for your help as far, but I think I am missing something key that I
have missed in my reading, and I have to backtrack a bit.

I am trying to "Run before I walk" :)
Im learning vb.net in my spare time as a hobby so getting the answer right away is not
important either.

I appreciate all you have done for me so far.

Miro
 
J

Jack Jackson

I think the explanation for this is as follows.

Since drStaff is apparently just an instance of DataRow, it doesn't
have properties for your specific column names, so you can't bind to
those property names.

When you bind to the DataTable, the DataTable has the names of the
columns and therefore knows how to bind to the column names.
 
M

Miro

Thank you... that does make sense and it is simple explanation.
Ill play with it some more as I crack open the book.

Got the book last night :)

Cheers'

M.
 

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