Monday, September 24, 2007

How to retrieve absolute physical/ virtual path inside vb class

recently my friends ask me how to write a code to retrieve absolute path from vb class for his asp.net application. the problem is server.mappath function cannot be used inside vb class so he ask for alternative.

so here's the solution that i think is good for sharing with everyone:

' import declarative
imports system.web

private strPath as string = Web.Hosting.HostingEnvironment.MapPath("~/App_Data/Dashboard.mdb")
Private sqlconn As New OleDbConnection("Provider=Microsoft.jet.oledb.4.0; data source=" & strPath & ";Jet OLEDB:Database Password=Dashboard; ")

the function HostingEnvironment.MapPath used to retrieve the physical / virtual path in the server. for more info about the HostingEnvironment classes you can go here:
Hosting Environment Class

that's it, hope it useful for everyone..

Friday, September 21, 2007

Enabling file download with asp.net

here's the code for downloading file from web server to client:

// create temporary file
dim strTemp as string = System.IO.Path.GetTempFileName

// here your process for example generating excel file

// here's the code for file download
Response.ContentType = "application/octet-stream"
Response.AddHeader("Content-Disposition", _
"attachment;filename=" & strFileName & ".xls")
Response.Flush()
Response.WriteFile(strTemp)
Response.End

* if your are using asp.net ajax update panel with this code, it will cause problem to the update panel.

the solution can be found here:
Ajax file download and iframe