Solved

Beginner - How to populate asp literal from Access database

Posted on 2009-05-05
4
483 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

730 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