Solved

ASP.Net Display records from database table based on QueryString

Posted on 2014-07-23
11
419 Views
Last Modified: 2014-07-30
In the following code in this Handler I am trying to modify or alter it so that the records or results are based on a QueryString value. Currently all of the records

in the table display on the page but I want records to display based on the passing a querystring value based on the field, id. I have tried to alter the code without much success.

Public Class FileVB : Implements IHttpHandler

Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
Dim id As Integer = Integer.Parse(context.Request.QueryString("id"))
Dim bytes As Byte()
Dim contentType As String
Dim strConnString As String = ConfigurationManager.ConnectionStrings("fitnesConnectionString").ConnectionString
Dim name As String
Using con As New SqlConnection(strConnString)
Using cmd As New SqlCommand()
cmd.CommandText = "select Name, Data, ContentType from tblFiles where Id=@Id"
cmd.Parameters.AddWithValue("@Id", id)
cmd.Connection = con
con.Open()
Dim sdr As SqlDataReader = cmd.ExecuteReader()
sdr.Read()
bytes = DirectCast(sdr("Data"), Byte())
contentType = sdr("ContentType").ToString()
name = sdr("Name").ToString()
con.Close()
End Using
End Using
context.Response.Clear()
context.Response.Buffer = True
context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + name)
context.Response.ContentType = contentType
context.Response.BinaryWrite(bytes)
context.Response.[End]()
End Sub
0
Comment
Question by:derrekdeveloper
  • 6
  • 4
11 Comments
 
LVL 12

Expert Comment

by:jitendra patil
ID: 40216554
how are you calling your handler?

can you post the markup/ code for calling the handler?

my guess is you can use querystring in your handler and pass the id value in querystring.

hope this helps
0
 
LVL 28

Expert Comment

by:sammySeltzer
ID: 40216852
Just this alone should have worked:

Dim id As Integer = Request.QueryString("id")

Open in new window


When you said you tried without success, are you getting an error?

It could be a case where the Id has no value.

You could also try this:

Dim id As Integer = CLng("0" & Request.QueryString("id"))

Open in new window


Then try Response.Write (id) just to see what is returned.
0
 

Author Comment

by:derrekdeveloper
ID: 40217567
When I use Dim id As Integer = Request.QueryString("id") I receive "Request is not declared" error
0
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
LVL 28

Expert Comment

by:sammySeltzer
ID: 40217592
hmm, that's odd.

Can you try this:

Dim id As Integer = Request("id") 

Open in new window

0
 

Author Comment

by:derrekdeveloper
ID: 40217735
I tried Dim id As Integer = Request("id"). I immediately get a Request is not declared and Visual Studio offer to change Request to HttpRequest which does not work either. I am sure this is due to the fact of the way the Handler is built with Public Class FileVB : Implements IHttpHandler. I am including the code that displays and allows playback of the .mp4 video. As I said, the solution works very well as far as uploading the video and playing the video is flawless. I just would not want to display every record in the database.

Code that displays and plays video. Note the reference to the Handler in the href tag:

<asp:DataList ID="DataList1" Visible="true" runat="server" AutoGenerateColumns="false"
        RepeatColumns="2" CellSpacing="5">
        <ItemTemplate>
            <u>
                <%# Eval("Name") %></u>
            <hr />
            <a class="player" style="height: 200px; width: 200px; display: block" href='<%# Eval("id", "FileVB.ashx?id={0}")%>'>
            </a>
        </ItemTemplate>
    </asp:DataList>
    <script src="FlowPlayer/flowplayer-3.2.12.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        flowplayer("a.player", "FlowPlayer/flowplayer-3.2.16.swf", {
            plugins: {
                pseudo: { url: "FlowPlayer/flowplayer.pseudostreaming-3.2.12.swf" }
            },
            clip: { provider: 'pseudo', autoPlay: false},
        });
    </script>
0
 

Author Comment

by:derrekdeveloper
ID: 40217746
Would changing any of this code make a difference?

<a class="player" style="height: 200px; width: 200px; display: block" href='<%# Eval("id", "FileVB.ashx?id={0}")%>'>
            </a>
0
 
LVL 28

Expert Comment

by:sammySeltzer
ID: 40217831
Sorry. I should have paid closer attention that you are using http handler to retrieve the value of ID.

If you tried with your original code as you did and did not get any values, could you then try this:

	Dim idAs String = context.Request.QueryString("id")

Open in new window

0
 
LVL 28

Expert Comment

by:sammySeltzer
ID: 40217833
That is Dim id AS (with space between id and AS).
0
 

Author Comment

by:derrekdeveloper
ID: 40218104
Time to give up on trying to get another developers solution to work. I am storing the mp4 video in a varbinary(max) column without any problems. What I am looking for is a link that provides a solution that allows me to retrieve the video from the sql database table and stream to a browser. People do this daily but I just don't know how to do it. If anyone can provide a link I will be happy to try another solution.
0
 

Accepted Solution

by:
derrekdeveloper earned 0 total points
ID: 40219838
I was able to solve this by talking to the .Net Developer that originally wrote the solution. Getting the query string value only had to be handled in the code behind binding event. Here is the code that finally worked.

Private Sub BindGrid()

        Dim id As Integer = Integer.Parse(Context.Request.QueryString("id"))
        Dim strConnString As String = ConfigurationManager.ConnectionStrings("SQL2008R2_504887_golivefitnesConnectionString").ConnectionString
        Using con As New SqlConnection(strConnString)
            Using cmd As New SqlCommand()
                cmd.CommandText = "select Id, Name from tblFiles where Id=@Id"
                cmd.Parameters.AddWithValue("@Id", id)
                cmd.Connection = con
                con.Open()
                DataList1.DataSource = cmd.ExecuteReader()
                DataList1.DataBind()
                con.Close()
            End Using
        End Using


    End Sub
0
 

Author Closing Comment

by:derrekdeveloper
ID: 40228672
I did not come up with the solution but none of the answers provided on the site would have worked. The original developer provided the solution. I found him on another site...ASPFOURMS.NET
0

Featured Post

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

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