Download File by Selecting Item in DataGrid

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.
Who is Participating?
vinodhsomasekharanConnect With a Mentor Commented:

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->")

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 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.AddHeader("content-disposition", "attachment; filename=" & myFilePath )
            Response.AddHeader("Content-Length", bytDocument.Length.ToString())

nmwilliamsAuthor Commented:
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?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.