Solved

Beginner - How to populate asp literal from Access database

Posted on 2009-05-05
4
445 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:
cdebel earned 300 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 200 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

746 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

8 Experts available now in Live!

Get 1:1 Help Now