Solved

Download File by Selecting Item in DataGrid

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

760 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

20 Experts available now in Live!

Get 1:1 Help Now