Solved

how to databinding a textbox in code-behind

Posted on 2004-08-15
10
571 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

705 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