Pull content from SQL based on url query string

Last post 02-07-2007 10:07 PM by timwilson. 3 replies.

Sort Posts:

  • Pull content from SQL based on url query string

    02-07-2007, 7:24 PM
    • Loading...
    • timwilson
    • Joined on 06-15-2006, 1:43 PM
    • Lawrenceville, GA
    • Posts 115

    I am trying to pull content into a content page (within a master page) by using url query strings.  I have got the code to pull the query string in and it is working fine.  I am having trouble creating my sql Select Command using a variable.  My sql Select Command works fine if I hard code it - I am just having trouble with how to reference the variable.  I am sorry I am very new to this so I am probably doing something really stupid wrong but I need to be able to move on and put this one behind me.

     Thanks for any and all help,

    Tim

     Here is the code I currently have - I know it is wrong (really wrong) but I left it this way so you could see what I am trying to do:

     

    <asp:Content ID="Content2" runat="server" ContentPlaceHolderID="Headers">

    <script runat="server">

    Sub UrlDecode()

    Dim StrToDecode As String

    Dim StrToReturn As String

    Dim SelectStatement As String

     

    StrToDecode = Request.QueryString(

    "pgid")

    StrToReturn = Server.UrlDecode(StrToDecode)

    SelectStatement =

    "SELECT * FROM [SiteContent]WHERE TextId = " + StrToReturn

    Response.Write(SelectStatement)

     

    ' Response.Write(StrToReturn)

    ' SELECT * FROM [SiteContent]WHERE TextId = 0

    End Sub

    </

    script>

     

    </

    asp:Content>

     

     

    <

    asp:Content ID="Content1" ContentPlaceHolderID="Main" Runat="Server">

     

    <

    asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Personal %>"

    SelectCommand="<% UrlDecode %>"></asp:SqlDataSource>

    <!-- SelectCommand="SELECT * FROM [SiteContent]"></asp:SqlDataSource> -->

     

     

    <

    div id="content">

    <asp:DataList ID="DataList1" runat="server"

    DataSourceID="SqlDataSource1">

    <ItemTemplate>

    <p><%#Eval("Body_Content")%></p>

    </ItemTemplate>

    </asp:DataList>

    </

    div>

    <% UrlDecode %>

     

     

     

    </

    asp:Content>
  • Re: Pull content from SQL based on url query string

    02-07-2007, 8:54 PM
    Answer
    • Loading...
    • limno
    • Joined on 06-10-2005, 3:50 PM
    • Iowa, USA
    • Posts 3,290
    • Moderator
      TrustedFriends-MVPs

    You don't need to retrieve this querystring from your code like what you did. You can do it all in declarative way.

    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Personal %>"

    SelectCommand="SELECT * FROM [SiteContent]WHERE TextId =@TexId

    <SelectParameters>

    <asp:QueryStringParameter Name="TexID" QueryStringField="pgid"  Type="String" />

    </SelectParameters>

    Limno

  • Re: Pull content from SQL based on url query string

    02-07-2007, 9:06 PM
    Answer

    Hi, timwilson:

    You can have a look at this article about querystringparameter.

    http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.querystringparameter.aspx

    Best Regards,
    __________________________________________________
    Sincerely,
    Rex Lin
    Microsoft Online Community Support

    If there is any question or the issue is not resolved, please feel free to mark the thread as not resolved
  • Re: Pull content from SQL based on url query string

    02-07-2007, 10:07 PM
    • Loading...
    • timwilson
    • Joined on 06-15-2006, 1:43 PM
    • Lawrenceville, GA
    • Posts 115

    Like a charm!

    That was exactly what I needed to get my by this - I knew it was easy - just not for me!

    Thanks both for your prompt replies - you have given me hope again after a bunch of frustration!

    Tim
     

Page 1 of 1 (4 items)
Microsoft Communities
Page view counter