Solved

ASP.Net Display records from database table based on QueryString

Posted on 2014-07-23
11
414 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

809 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