Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Download File by Selecting Item in DataGrid

Posted on 2006-07-13
4
Medium Priority
?
1,506 Views
Last Modified: 2011-09-20
I am new to ASP .NET so hopefully I can ask my question clearly.  I have a datagrid with the SelectButton enabled on my page.  I am trying to use the Datagrid_SelectedIndexChanged event to:

1. Retrieve the primary key of the selected record in the datagrid
2. Query my database for the file path associated with that primary key
3. Retrieve the file using the path and display the file dialogue box so the user can download the file to their machine.

I've been researching for hours and can't seem to find an example that pulls these three steps together.  Any help is appreciated.
0
Comment
Question by:nmwilliams
2 Comments
 
LVL 5

Accepted Solution

by:
vinodhsomasekharan earned 675 total points
ID: 17106006
Hai,

Soln 1:-
=====
in the Datagrid1_SelectedIndexChanged event u can access the required cell values using the following syntax

for eg.. if u r keyfield / primary key is in column 0  --> then u can get the selected row's keyfield value by
 myKeyField =  Datagrid1.SelectedItem.Cells(0).Text

 
Soln 2:-
=====
and by using this value u can query the database to  get the filepath.. using a command.. and a DataReader.
eg.. we can take sql server as example

 Dim myConn As SqlClient.SqlConnection
  Dim myCmd As New SqlClient.SqlCommand
 Dim myReader As SqlClient.SqlDataReader

 myConn = New SqlClient.SqlConnection("server=<--your servername-->;initial catalog=<-your db name->;uid=<-you userid->;pwd=<- Your Password->")
 myConn.Open()

myCmd.Connection = myConn
myCmd.CommandType = CommandType.Text
myCmd.CommandText = "select filePath from myactualtable where fileId = @fileId"
myCmd.Parameters.Add("@fileId", SqlDbType.VarChar).Value = myKeyField

myReader = myCmd.ExecuteReader()
' You can access the file path using the index or Field name from the sqldatareader
myFilePath = myReader.item(0)


Soln 3:-
=====
After getting the filePath value u can use a file.open() command to get the filestream of the file and convet the stream into bytes and write them into the response stream ... with suitable headers as follows..

Dim fileStream As IO.FileStream
Dim bytDocument As Byte()

 fileStream = System.IO.File.Open(myFilePath, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read)
            ReDim bytDocument(fileStream.Length)
            fileStream.Read(bytDocument, 0, fileStream.Length)

            Response.Clear()
            Response.AddHeader("content-disposition", "attachment; filename=" & myFilePath )
            Response.AddHeader("Content-Length", bytDocument.Length.ToString())
            Response.BinaryWrite(bytDocument)
            Response.Flush()
            Response.Close()
            Response.End()

regards
Vinodh
0
 

Author Comment

by:nmwilliams
ID: 17110764
Thank you so much.  One problem though is that I didn't realize a datagrid was different from a GridView.  I am using a GridView so it tells me that selecteditem is not a member of the gridview control.  Also, if I am not displaying my primary key in the GridView can I still access it by specifying a column?
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Loops Section Overview
Suggested Courses

824 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