how to databinding a textbox in code-behind

hi,all
I created a textbox and want to databinding it's text to a field of dataset.
Any solution?
thanks.
LVL 1
fatchoiAsked:
Who is Participating?
 
razoCommented:
the only way u could do that is to use
textbox1.text=ds.tables(0).rows(0)(fieldname)
this wont bind the textbox but it will get the requested value...u have to update the dataset manually when changes is done
u cant programtically do the databinding
0
 
hamoodCommented:
In order to add new records into a dataset, a new data row must be created and added to the DataRow collection of a data table. The following procedure details how to insert additional rows into a DataTable object in a dataset. For this example, it is assumed the ExistingTable is in a dataset and has two columns named FirstName and LastName.
To add new records to a typed or untyped dataset
Call a data table's NewRow method to create a new, blank record.
This new record inherits its column structure from the data table's DataColumnCollection.
' Visual Basic
Dim anyRow as DataRow = ExistingTable.NewRow

// C#
DataRow anyRow = ExistingTable.NewRow();
Update the new row as if it were an existing record.
' Visual Basic
anyRow(0) = text1.text
anyRow(1) = text2.text
anyRow("FirstName") = "Jay"
anyRow("LastName") = "Stevens"

// C#
anyRow[0] = "Jay";
anyRow[1] = "Stevens";
// or
anyRow["FirstName"] = "Jay";
anyRow["LastName"] = "Stevens";
Add the new record to the table by calling the Add method of the DataRowCollection object.
' Visual Basic
ExistingTable.Rows.Add(anyRow)

// C#
ExistingTable.Rows.Add(anyRow);
Inserting New Records with Typed Datasets
Typed datasets expose the column names as properties of the DataRow object.
To add new records using typed datasets
The following example illustrates the same three steps above, except this time the code is modified for use with a typed dataset:
' Visual Basic
Dim anyRow as DataRow = DatasetName.ExistingTable.NewRow
anyRow.FirstName = "Jay"
anyRow.LastName = "Stevens"
ExistingTable.Rows.Add(anyRow)

// C#
DataRow anyRow = DatasetName.ExistingTable.NewRow();
anyRow.FirstName = "Jay";
anyRow.LastName = "Stevens";
ExistingTable.Rows.Add(anyRow);

for data updation

When you make changes to column values in a DataRow, the changes are immediately placed in the Current state of the row. The RowState is then set to Modified and the changes are accepted or rejected using the AcceptChanges or RejectChanges methods of the DataRow. The DataRow also provides three methods that you can use to suspend the state of the row while you are editing it. These methods are BeginEdit, EndEdit, and CancelEdit.
When you modify column values in a DataRow directly, the DataRow manages the column values using the Current, Default, and Original row versions. In addition to these row versions, the BeginEdit, EndEdit, and CancelEdit methods use a fourth row version: Proposed. For more information about row versions, see Row States and Row Versions.
The Proposed row version exists during an edit operation that is begun by calling BeginEdit and that is ended by using either EndEdit or CancelEdit or by calling AcceptChanges or RejectChanges.
During the edit operation you can apply validation logic to individual columns by evaluating the ProposedValue in the ColumnChanged event of the DataTable. The ColumnChanged event holds DataColumnChangeEventArgs that keep a reference to the column that is changing and the ProposedValue. After you evaluate the proposed value, you can either modify it or cancel the edit. When the edit is ended, the row moves out of the Proposed state.
You can confirm edits by calling EndEdit or you can cancel them by calling CancelEdit. Note that while EndEdit does confirm your edits, the DataSet does not actually accept the changes until AcceptChanges is called. Note also that if you call AcceptChanges before you have ended the edit with EndEdit or CancelEdit, the edit is ended and the Proposed row values are accepted for both the Current and Original row versions. In the same manner, calling RejectChanges ends the edit and discards the Current and Proposed row versions. Calling EndEdit or CancelEdit after calling AcceptChanges or RejectChanges has no effect because the edit has already ended.
The following example demonstrates how to use BeginEdit with EndEdit and CancelEdit. The example also checks the ProposedValue in the ColumnChanged event and decides whether to cancel the edit.
[Visual Basic]
Dim workTable As DataTable = New DataTable
workTable.Columns.Add("LastName", Type.GetType("System.String"))

AddHandler workTable.ColumnChanged, New DataColumnChangeEventHandler(AddressOf OnColumnChanged)

Dim workRow As DataRow = workTable.NewRow()
workRow(0) = "Smith"
workTable.Rows.Add(workRow)

workRow.BeginEdit()
' Causes the ColumnChanged event to write a message and cancel the edit.
workRow(0) = ""     
workRow.EndEdit()

' Displays "Smith, New".
Console.WriteLine("{0}, {1}", workRow(0), workRow.RowState)  


Private Shared Sub OnColumnChanged(sender As Object, args As DataColumnChangeEventArgs)
  If args.Column.ColumnName = "LastName" Then
    If args.ProposedValue.ToString() = "" Then
      Console.WriteLine("Last Name cannot be blank.  Edit canceled.")
      args.Row.CancelEdit()
    End If
  End If
End Sub


hamood
0
 
razoCommented:
textBox1.DataBindings.Add (New Binding("Text", ds, "filedname"))
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
razoCommented:
then add
textbox1.databind()
0
 
fatchoiAuthor Commented:
hi,hamood,
I want databind to textbox.

razo,
textbox doesn't have databindings and binding.
must i import some namespace?
0
 
razoCommented:
sorry i messed up with windowws forms
what u can do is
in the html
<asp:textbox id=textbox1 runat=server text='<%#Databinder.eval(ds,"fieldname)%>'>


in the load sub
textbox1.databind


0
 
fatchoiAuthor Commented:
razo,
i want to do it in code-behind because I want to create the databind dynamically.
thanks a lot.



0
 
razoCommented:
or in the html
<asp:TextBox id="myTextBox" runat="server"
   Text='<%# ds1 ( 0 ) ( "fieldname" ) %>'>
</asp:TextBox>


in the load sub
textbox1.databind

0
 
razoCommented:
try also using
 textbox1.Text=databinder.Eval(ds,"fieldname") in the code
0
 
fatchoiAuthor Commented:
textbox1.Text=databinder.Eval(ds,"fieldname") doesn't work

textbox1.text=ds.tables(0).rows(0)(fieldname) works

thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.