Bind a TextBox to a SQL DataSource

How do I bind a textBox to a SQL Datasource?  
Who is Participating?
kraffayConnect With a Mentor Commented:
With this example, I get a one-row, one-field recordset and bind it to a repeater.  Than I get a handle to the textbox in the repeater by referencing the control collection of the repeater and casting a control to a Textbox.  From there I can get the Text property:

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<script runat="server" >

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)

        Dim txtCategory As TextBox = Me.Repeater1.Controls(0).Controls(1)
    End Sub

<html xmlns="" >
<head runat="server">
    <title>Untitled Page</title>
    <form id="form1" runat="server">
        <asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">
            <asp:TextBox ID="txtCategory" runat="server" Text='<%# Bind("CategoryName")%>' ></asp:TextBox>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:aspnet_starterKits_TimeTracker %>"
            SelectCommand="SELECT CategoryName FROM Category WHERE (CategoryID = 1)"></asp:SqlDataSource>
        <asp:Button ID="Button1" runat="server" Text="Get Value" OnClick="Button1_Click" />
Make sure you have AutoGenerateColumnNames off and add a TemplateField:

<asp:GridView ....

<asp:TemplateField HeaderText="EventID">
                                    <asp:TextBox ID="txtBox" runat="server" Text='<%# Bind("YourDataField") %>'></asp:TextBox>

intchauspeAuthor Commented:
actually what I want to do is return a value to a textbox.  I don't have a gridview on the page.   I just have a SQL data source and I want to display the result of the sql query in the textbox?  Can I do that or do I have to have a Gridview ?
7 new features that'll make your work life better

It’s our mission to create a product that solves the huge challenges you face at work every day. In case you missed it, here are 7 delightful things we've added recently to monday to make it even more awesome.

You could use a repeater with just a text box, that is the control with the least markup needed.  But a textbox by itself cannot bind to SQL DataSource.
intchauspeAuthor Commented:
well that seems to be good solution.  I only have one problem.  I have an emailing function that reads the value of the fields.  Previously I had used it with labels and text boxes like so....

        strText = Replace(strText, "REPLACE_uid", lblUserName.Text)
        strText = Replace(strText, "REPLACE_startdate", Date.Now)
        strText = Replace(strText, "REPLACE_title", txtTitle.Text)
        strText = Replace(strText, "REPLACE_username", lblUserName.Text)

I want to use the same email function but I can't figure out how to access the values that are in the repeaters.  

When I made the item templates I gave them the same names they had as before - ie: txtTitle.text but I guess since it is inside the repeater control it can't access it because when I use the code above it will tell me that txtTitle.text is not defined.

Any idea how to fix that ?
intchauspeAuthor Commented:
I haven't had a chance to try this yet but it seems like it should work... I'm going to work on it on Monday.  I'll let you know
intchauspeAuthor Commented:
Awesome.. that works.  THANK YOU !
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.