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

x
?
Solved

Beginner - How to populate asp literal from Access database

Posted on 2009-05-05
4
Medium Priority
?
502 Views
Last Modified: 2012-05-06
Hey there,

I'm trying to populate an asp literal control on my asp.net page with data from an Access database.

I've got this to connect to my database:

<asp:AccessDataSource id="AccessDataSource2" runat="server" DataFile="app_data/pedigrees.mdb" SelectCommand="SELECT [Horse] FROM [Pedigrees] WHERE ([ID] = ?)">
                  <SelectParameters>
                        <asp:parameter DefaultValue="1" Name="ID" Type="Int32" />
                  </SelectParameters>
</asp:AccessDataSource>

I'd like to use an asp literal to get the value of "Horse" and display it on my page.

Any help will be greatly appreciated.

PS - I don't have a clue what I'm doing.
0
Comment
Question by:ttist25
  • 2
4 Comments
 
LVL 6

Expert Comment

by:M3mph15
ID: 24311021
Hi,

Is there any reason behind using a <asp:Literal>. If not why not use a TextBOx or a Label.
0
 
LVL 10

Accepted Solution

by:
Christian de Bellefeuille earned 1200 total points
ID: 24311080
I don't know with an Access File, and from ASPX file, but if i would do it in code behind, i would do this like this:


    Private Sub SqlDataSource1_Selected(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceStatusEventArgs) Handles SqlDataSource1.Selected
        Dim oCon As New SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("MyConnection").ConnectionString)
        oCon.Open()
        Dim oCmd As New SqlClient.SqlCommand("SELECT [Horse] FROM [Pedigrees] WHERE ([ID] = xxx)", oCon)
        Dim oRdr As SqlClient.SqlDataReader
 
        oRdr = oCmd.ExecuteReader()
        If oRdr.Read() Then
            Literal1.Text = oRdr("Horse").ToString()
        End If
    End Sub

Open in new window

0
 
LVL 1

Author Comment

by:ttist25
ID: 24311155
I ended up doing this which works:

<asp:Repeater ID="Repeater1" runat="server"
        DataSourceID="AccessDataSource2">
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Eval("Horse") %>' />
            </ItemTemplate>
        </asp:Repeater>

It seems like if the asp:Label had a "DataSourceID" property it would make things really simple.  Am I missing something or is it really that complicated?
0
 
LVL 6

Assisted Solution

by:M3mph15
M3mph15 earned 800 total points
ID: 24311400
Yeh if you want to do it automatically in the aspx page you must put the control inside another control that has a DataSourceID property. Like a FormView, or in your case a Repeater. Or you might be able to even use a <div> tag....i think it has a property something like datasrc.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

886 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