Solved

Download File by Selecting Item in DataGrid

Posted on 2006-07-13
4
1,501 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 5

Accepted Solution

by:
vinodhsomasekharan earned 225 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

751 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