Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL Query Results into a TextBox

Posted on 2010-08-20
7
Medium Priority
?
1,055 Views
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"
                       runat="server"
                       ConnectionString="<%$ ConnectionStrings:Focus %>"
                       ProviderName="<%$ ConnectionStrings:Focus.ProviderName %>"
                       SelectCommand="SELECT TermsAndConditions FROM tblPPTermsAndConditions                        WHERE ID=1003">
    </asp:SqlDataSource>

Open in new window

0
Comment
Question by:dvargas531
  • 4
  • 3
7 Comments
 
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.
0
 

Author Comment

by:dvargas531
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?
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 2000 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

0
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.

 

Author Comment

by:dvargas531
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();

0
 
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.
0
 

Author Comment

by:dvargas531
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?
0
 
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.
0

Featured Post

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.

Question has a verified solution.

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

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
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…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Loops Section Overview
Suggested Courses

963 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