Solved

ASP.Net Display records from database table based on QueryString

Posted on 2014-07-23
11
410 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

832 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