Solved

SQL Query Results into a TextBox

Posted on 2010-08-20
7
1,048 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

770 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