locked
SQL Querystrings and Metadata RRS feed

  • Question

  • User-846997834 posted

    Hello everyone, I've been working on this specific issue for a week or so and it's driving me nuts. I've looked around a lot and can't quite find exactly what I'm trying to do.

    Basically, I'm writing a CMS for our website and I've managed to get the content page to know which content to display dependant on a querystring which pulls the requested article from a SQL database and display it in a repeater - this bit works fine. What I am trying to do now is get the pages title and metadata dynamically set using the same querystring. I think I'm close but below is my code:

    Imports System.Data.SqlClient
    Imports System.Data
    Imports System.Web.UI
    
    Partial Class pages
        Inherits System.Web.UI.Page
    
        Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    
            'This reads the page name from the repeater and sets it as the current page's title.
            Dim ptitle As Control = rptPagesContent.Controls(rptPagesContent.Controls.Count - 1).Controls(1)
            Dim ptext As Label = TryCast(ptitle.FindControl("lblTitle"), Label)
            Page.Header.Title = ptext.Text.ToString & " :: The Dorcan Academy"
    
            'This section of code reads the loaded page's metadata and displays it in the header
            Dim qsval As String = Page.Request.QueryString("pagename")
            Dim qspar As QueryStringParameter(qsval)
            Dim query As String = "SELECT PageMeta FROM dw2_Pages WHERE PageName = @PageName"
            Dim myconnection As New SqlConnection(ConfigurationManager.ConnectionStrings("dorcanwebConnectionString").ConnectionString)
            Dim SqlCmd As SqlCommand = Nothing
            SqlCmd = New SqlCommand(query, myconnection)
            ' ADDING THE PARAMETER
            SqlCmd.Parameters.Add(qspar)
            ' Opening the SQL Connection 
            SqlCmd.Connection.Open()
            SqlCmd.ExecuteNonQuery()
    
            Dim ad As New SqlDataAdapter(SqlCmd)
            Dim dt As New DataTable()
            ad.Fill(dt)
    
            keywords.Attributes.Add("content", dt.Rows(0)("PageMeta").ToString())
    
            ' Closing the SQL connection 
            SqlCmd.Connection.Close()
    
        End Sub
    
    End Class

    With the page title section I get an "ArgumentOutOfRangeException" error on the line starting with "Dim ptitle As Control...".

    On the metadata section the build fails and I get an error relating to the "qsval" string - "Array bounds cannot appear in type specifiers", which doesn't make much sense to me.

    I understand that this is kinda two issues in one but they are both the same type of end result I'm after - work out what page's data is loaded, display the related page title to title section of the header and add in the metadata to the header too.

    Any help would be greatly appreciated. Thanks, Adam

    Wednesday, February 26, 2014 8:49 AM

Answers

  • User-933407369 posted

    hi,

    According to your description, i think that you need to debug your codes. without reproduced your issues, i didn't provide solution for you.

    >>1.With the page title section I get an "ArgumentOutOfRangeException" error on the line starting with "Dim ptitle As Control...":

    'This reads the page name from the repeater and sets it as the current page's title.
     Dim ptitle As Control = rptPagesContent.Controls(rptPagesContent.Controls.Count - 1).Controls(1)

    The error message means that you find control which not exists. so it causes 'ArgumentOutOfRangeException' error. you need to debug your codes  and make sure the index correct, you can try to use the ways below:

      Dim ptitle As Control = rptPagesContent.Controls(rptPagesContent.Controls.Count - 1).Controls(0) As Control 

    >>2.On the metadata section the build fails and I get an error relating to the "qsval" string - "Array bounds cannot appear in type specifiers", which doesn't make much sense to me:

     'This section of code reads the loaded page's metadata and displays it in the header
            Dim qsval As String = Page.Request.QueryString("pagename")
            Dim qspar As QueryStringParameter(qsval)

    what is your 'QueryStringParameter(qsval)' and qsval? there cannot appear in type specifiers.

    you can also refer to the link for details:

    http://forums.asp.net/p/1330624/2669949.aspx

    Hope it helps you.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 27, 2014 1:35 AM