Solved

Download File by Selecting Item in DataGrid

Posted on 2006-07-13
4
1,496 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

920 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

15 Experts available now in Live!

Get 1:1 Help Now