473,395 Members | 1,377 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Bind Textbox to DataSet

Hi. I have a dataset on my webform which I successfully fill by calling a
Sub that occurs after the Page_Load. My question is - I have a textbox
control which I need to populate with the contents of one of the Dataset
columns. If I set set the binding properties using the properties window,
this isn't successfull since the textbox attempts to bind at page_load, at
which time the dataset is not filled.

How can I use VB code to specify that the textbox's text property should be
set to a column within the dataset? Thanks!
Nov 19 '05 #1
3 17730
How can I use VB code to specify that the textbox's text property should
be
set to a column within the dataset? Thanks!


If you're using a typed dataset (are you?), you can easily bind the textbox
to a column by setting the "databindings" property of the textbox to point
to a column in the typed dataset's defaultView.

You do this design-time.

Then, whenever you fill the typed dataset, you can afterwards call
myTextbox.databind(), which will get the data from the datatable.

Hope it helps,
Jeppe Jespersen
Nov 19 '05 #2
Hi Mike,

You should be able to bind to a textbox by binding the page. Here's some
code that might give you the idea. Let us know if it helps?

Ken
Microsoft MVP [ASP.NET]
Toronto
Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
' From Ken Cox Microsoft MVP [ASP.NET]
If Not IsPostBack Then
' Create a dataset
Dim ds As New DataSet
' Add a table to the dataset
ds.Tables.Add(CreateDataSource())
' Create a filter so that we only get one row
ds.Tables(0).DefaultView.RowFilter = "IntegerValue = 5"
' Pass the dataset and an expression to DataBinder.Eval
' so that it returns the string called StringValue in
' the default dataview
TextBox1.Text = DataBinder.Eval(ds, _
"Tables(0).DefaultView(0).StringValue")
' Bind everything on the page
Page.DataBind()
End If
End Sub

Private Sub Button1_Click _
(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button1.Click
Label1.Text = TextBox1.Text
End Sub

Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("IntegerValue", GetType(Int32)))
dt.Columns.Add(New DataColumn _
("StringValue", GetType(String)))
dt.Columns.Add(New DataColumn _
("CurrencyValue", GetType(Double)))
dt.Columns.Add(New DataColumn _
("Boolean", GetType(Boolean)))
Dim i As Integer
For i = 0 To 5
dr = dt.NewRow()
dr(0) = i
dr(1) = "Item " + i.ToString()
dr(2) = 1.23 * (i + 1)
dr(3) = (i = 4)
dt.Rows.Add(dr)
Next i
Return dt
End Function
<form id="Form1" method="post" runat="server">
<p>
<asp:textbox id="TextBox1" runat="server"></asp:textbox></p>
<p>
<asp:button id="Button1" runat="server"
Text="Button"></asp:button></p>
<p>
<asp:label id="Label1" runat="server"></asp:label></p>
</form>

"MrMike" <Mr****@discussions.microsoft.com> wrote in message
news:B1**********************************@microsof t.com...
Hi. I have a dataset on my webform which I successfully fill by calling a
Sub that occurs after the Page_Load. My question is - I have a textbox
control which I need to populate with the contents of one of the Dataset
columns. If I set set the binding properties using the properties window,
this isn't successfull since the textbox attempts to bind at page_load, at
which time the dataset is not filled.

How can I use VB code to specify that the textbox's text property should
be
set to a column within the dataset? Thanks!


Nov 19 '05 #3
Thank you both! I have this working now after learning from your examples.
Thanks...

"Ken Cox [Microsoft MVP]" wrote:
Hi Mike,

You should be able to bind to a textbox by binding the page. Here's some
code that might give you the idea. Let us know if it helps?

Ken
Microsoft MVP [ASP.NET]
Toronto
Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
' From Ken Cox Microsoft MVP [ASP.NET]
If Not IsPostBack Then
' Create a dataset
Dim ds As New DataSet
' Add a table to the dataset
ds.Tables.Add(CreateDataSource())
' Create a filter so that we only get one row
ds.Tables(0).DefaultView.RowFilter = "IntegerValue = 5"
' Pass the dataset and an expression to DataBinder.Eval
' so that it returns the string called StringValue in
' the default dataview
TextBox1.Text = DataBinder.Eval(ds, _
"Tables(0).DefaultView(0).StringValue")
' Bind everything on the page
Page.DataBind()
End If
End Sub

Private Sub Button1_Click _
(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button1.Click
Label1.Text = TextBox1.Text
End Sub

Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("IntegerValue", GetType(Int32)))
dt.Columns.Add(New DataColumn _
("StringValue", GetType(String)))
dt.Columns.Add(New DataColumn _
("CurrencyValue", GetType(Double)))
dt.Columns.Add(New DataColumn _
("Boolean", GetType(Boolean)))
Dim i As Integer
For i = 0 To 5
dr = dt.NewRow()
dr(0) = i
dr(1) = "Item " + i.ToString()
dr(2) = 1.23 * (i + 1)
dr(3) = (i = 4)
dt.Rows.Add(dr)
Next i
Return dt
End Function
<form id="Form1" method="post" runat="server">
<p>
<asp:textbox id="TextBox1" runat="server"></asp:textbox></p>
<p>
<asp:button id="Button1" runat="server"
Text="Button"></asp:button></p>
<p>
<asp:label id="Label1" runat="server"></asp:label></p>
</form>

"MrMike" <Mr****@discussions.microsoft.com> wrote in message
news:B1**********************************@microsof t.com...
Hi. I have a dataset on my webform which I successfully fill by calling a
Sub that occurs after the Page_Load. My question is - I have a textbox
control which I need to populate with the contents of one of the Dataset
columns. If I set set the binding properties using the properties window,
this isn't successfull since the textbox attempts to bind at page_load, at
which time the dataset is not filled.

How can I use VB code to specify that the textbox's text property should
be
set to a column within the dataset? Thanks!


Nov 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Steve | last post by:
C# I have some combo boxes, full of lookup descriptions. When I retrieve a dataset for my record, the values that need binding to the combos are the actual record IDs that relate to these...
4
by: David P. Donahue | last post by:
When I bind a dataset to a SQL query that contains a blob field it comes back as a byte array. Is there any way to have it interpreted as a string or somehow convert it to a string? I'm using the...
2
by: OldNewbie | last post by:
Hello All I have a textbox control. I would like this text box to automatically update to contain the currently selected item located in a listbox which is on the same form. Do I need a...
4
by: John Rose | last post by:
I have one databound TextBox on a page with one button. The TextBox loads the correct SQL record data but typing a new string into the Textbox fails to change the DataSet. Any ideas? There must...
0
by: rbutch | last post by:
hey guys got a question. im trying to bind a textbox "at runtime". im creating the conn string, dataset, dataAdapter etc dynamically. and it keeps erroring out. <asp:TextBox ID="txtDay1"...
17
by: A_PK | last post by:
I have problem databinding the DataGrid with DataView/DataSet after the filter... I create the following proceudre in order for user to filter as many as they want, but the following code is only...
1
by: Adam J Knight | last post by:
Hi all, I am assuming there may be a number of ways to bind a text box to a db field using c#. My current approach is: txtPostCode.Text = rdrInstitution.ToString(); Can anyone let me if...
3
by: raamay | last post by:
I have a module where i have specified the connection string as below: Public Function Connection() As String Return "Data Source=192.168.0.1,1433;Network Library=DBMSSOCN;Initial...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.