Rich
Rich

Reputation: 375

Populate Literal elements with the results of a SqlDataSource query

I have what is possibly a very simple question yet the answer is escaping me completely. I'm new to ASP.NET (vb) but I have a solid Classic ASP/VB background.

I have a relatively simple database which contains just four columns (Language, SiteFooterPrivacyPolicy, SiteFooterTermsAndConditions, SiteFooterCopyright). I wish to perform a query which will return just 1 row.

Here is the main page so far (along with the SQL SELECT statement):

    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="dbtest1.aspx.vb" Inherits="dbtest1" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:SqlDataSource ID="MyLanguageDataSource" runat="server" 
                ConnectionString="<%$ ConnectionStrings:customer_support_devConnectionString %>" 
                ProviderName="<%$ ConnectionStrings:customer_support_devConnectionString.ProviderName %>" 
                SelectCommand="SELECT [SiteFooterPrivacyPolicy], [SiteFooterTermsAndConditions], [SiteFooterCopyright] FROM [language_file_2] WHERE ([Language] = 'French')">
            </asp:SqlDataSource>
            <br />
            <br /><b>Literal1PrivPolicy:</b> <asp:Literal ID="Literal1PrivPolicy" runat="server"></asp:Literal>
            <br /><b>Literal2TermsConds:</b> <asp:Literal ID="Literal2TermsConds" runat="server"></asp:Literal>
            <br /><b>Literal3Copyright:</b> <asp:Literal ID="Literal3Copyright" runat="server"></asp:Literal>
            <br />
            </div>
        </form>
    </body>
    </html>

And so far I have this code behind (which does not work) but hopefully it'll explain far better than I can, what I am trying to achieve:

    Partial Class dbtest1
        Inherits System.Web.UI.Page

        Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load

            Dim dv As New Data.DataView

            dv = MyLanguageDataSource.Select(DataSourceSelectArguments.Empty)

            Literal1PrivPolicy.Text = dv.Table.Rows("SiteFooterPrivacyPolicy").ToString()
            Literal2TermsConds.Text = dv.Table.Rows("SiteFooterTermsAndConditions").ToString()
            Literal3Copyright.Text = dv.Table.Rows("SiteFooterCopyright").ToString()

        End Sub
    End Class

Hopefully from that you can see what I'm trying to do. I basically need my Literal elements populated with the correct column returned from my database query.

Upvotes: 1

Views: 730

Answers (1)

Netricity
Netricity

Reputation: 2738

I think you're trying to access the rows by name, when you should be accessing the first row's columns by name, e.g:

Literal1PrivPolicy.Text = dv.Table.Rows(0)("SiteFooterPrivacyPolicy").ToString()

Upvotes: 1

Related Questions