Solved

SQL Query Results into a TextBox

Posted on 2010-08-20
7
1,046 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 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

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

937 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now