Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Bind a TextBox to a SQL DataSource

Posted on 2006-11-14
7
Medium Priority
?
1,715 Views
Last Modified: 2012-06-27
How do I bind a textBox to a SQL Datasource?  
0
Comment
Question by:intchauspe
[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
  • 4
  • 3
7 Comments
 
LVL 9

Expert Comment

by:kraffay
ID: 17942495
Make sure you have AutoGenerateColumnNames off and add a TemplateField:

<asp:GridView ....

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

</gridView>
0
 

Author Comment

by:intchauspe
ID: 17946982
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 ?
0
 
LVL 9

Expert Comment

by:kraffay
ID: 17947434
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.
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!

 

Author Comment

by:intchauspe
ID: 17948576
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 ?
0
 
LVL 9

Accepted Solution

by:
kraffay earned 2000 total points
ID: 17948954
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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<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)
        Response.Write(txtCategory.Text)
       
    End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">
        <ItemTemplate>
            <asp:TextBox ID="txtCategory" runat="server" Text='<%# Bind("CategoryName")%>' ></asp:TextBox>
            </ItemTemplate>
        </asp:Repeater>
   
    </div>
        <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" />
           
    </form>
</body>
</html>
0
 

Author Comment

by:intchauspe
ID: 17968735
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
0
 

Author Comment

by:intchauspe
ID: 17979952
Awesome.. that works.  THANK YOU !
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

604 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