Solved

how to databinding a textbox in code-behind

Posted on 2004-08-15
10
557 Views
Last Modified: 2012-05-05
hi,all
I created a textbox and want to databinding it's text to a field of dataset.
Any solution?
thanks.
0
Comment
Question by:fatchoi
  • 6
  • 3
10 Comments
 
LVL 4

Expert Comment

by:hamood
ID: 11807522
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
 
LVL 8

Expert Comment

by:razo
ID: 11807549
textBox1.DataBindings.Add (New Binding("Text", ds, "filedname"))
0
 
LVL 8

Expert Comment

by:razo
ID: 11807567
then add
textbox1.databind()
0
 
LVL 1

Author Comment

by:fatchoi
ID: 11807600
hi,hamood,
I want databind to textbox.

razo,
textbox doesn't have databindings and binding.
must i import some namespace?
0
 
LVL 8

Expert Comment

by:razo
ID: 11807676
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Author Comment

by:fatchoi
ID: 11807708
razo,
i want to do it in code-behind because I want to create the databind dynamically.
thanks a lot.



0
 
LVL 8

Expert Comment

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


in the load sub
textbox1.databind

0
 
LVL 8

Accepted Solution

by:
razo earned 50 total points
ID: 11807843
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
 
LVL 8

Expert Comment

by:razo
ID: 11807856
try also using
 textbox1.Text=databinder.Eval(ds,"fieldname") in the code
0
 
LVL 1

Author Comment

by:fatchoi
ID: 11808122
textbox1.Text=databinder.Eval(ds,"fieldname") doesn't work

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

thanks.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now