SQL Query Results into a TextBox

Posted on 2010-08-20
Last Modified: 2012-05-10
Hello all,

I'm trying to get a sqldatasource value to input into a textbox. How can this be achieved? This column contains terms and conditions, so it contains a large amount of text which is why I need it in the textbox.

<asp:TextBox id="txtTerms" TextMode="multiline" Wrap="true" ReadOnly="true" Height="150px" Width="100%" ForeColor="GrayText" Font-Size="11px" runat="server"></asp:TextBox>

 <asp:SqlDataSource ID="SqlTerms"
                       ConnectionString="<%$ ConnectionStrings:Focus %>"
                       ProviderName="<%$ ConnectionStrings:Focus.ProviderName %>"
                       SelectCommand="SELECT TermsAndConditions FROM tblPPTermsAndConditions                        WHERE ID=1003">

Open in new window

Question by:dvargas531
  • 4
  • 3
LVL 52

Expert Comment

by:Carl Tawn
ID: 33484852
You'd have to handle the Selected event of the SqlDataSource and write the data to the textbox. Otherwise you need to use a control, such as the FormView, that supports direct binding to a data source.

Author Comment

ID: 33485087
Thanks for the quick response, do you know if there is some code or a link that I can reference to that is an example of this?
LVL 52

Accepted Solution

Carl Tawn earned 500 total points
ID: 33485273
Actually scratch that. What you need to do is manually invoke the Select method of the SqlDataSource object, which will return a DataView containing the query results:
System.Data.DataView view = (System.Data.DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
MyTextBox.Text = view[0][1].ToString();

Open in new window

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.


Author Comment

ID: 33485492
I get "System.Data.DataRowView" as the output in the text box.

This is what I've inserted:
System.Data.DataView view = (System.Data.DataView)SqlTerms.Select(DataSourceSelectArguments.Empty);
txtTerms.Text = view[0].ToString();

LVL 52

Expert Comment

by:Carl Tawn
ID: 33485524
You need two sets of square brackets (as in my last post); the first indicates which row you want to take data from (zero if there is only one row), the second is the column.

Author Comment

ID: 33485578
I get an error on the page:

Cannot find column 1. The line in question is:
Line 24: txtTerms.Text = view[0][1].ToString();

Could it be because I'm only calling for 1 column in the query?
LVL 52

Expert Comment

by:Carl Tawn
ID: 33486051
Yes. The snippet was only an example, you will need to change the index values to match your data. So, if you're only pulling back one column, then you will need to specify zero as the column index.

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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…
User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

830 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