Solved

ASP.Net Display records from database table based on QueryString

Posted on 2014-07-23
11
406 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
 
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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
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…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

919 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now