• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 509
  • Last Modified:

Beginner - How to populate asp literal from Access database

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] = ?)">
                        <asp:parameter DefaultValue="1" Name="ID" Type="Int32" />

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.
  • 2
2 Solutions

Is there any reason behind using a <asp:Literal>. If not why not use a TextBOx or a Label.
Christian de BellefeuilleProgrammerCommented:
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)
        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

ttist25Author Commented:
I ended up doing this which works:

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

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?
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now