Solved

how to databinding a textbox in code-behind

Posted on 2004-08-15
10
567 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Connection String 16 58
jquery progress bar 3 41
Save ms data to server side. 19 43
No Data for DropDown List 2 25
Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

773 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