Solved

ASP.Net Display records from database table based on QueryString

Posted on 2014-07-23
11
403 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
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…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

743 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

12 Experts available now in Live!

Get 1:1 Help Now