Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL Query Results into a TextBox

Posted on 2010-08-20
7
Medium Priority
?
1,054 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
[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 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 Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
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…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

704 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