Imports System.IO Imports System.Data.OleDb Public Class download Inherits System.Web.UI.Page #Region " Web Form Designer Generated Code " 'This call is required by the Web Form Designer. Private Sub InitializeComponent() End Sub Protected WithEvents Label1 As System.Web.UI.WebControls.Label Protected WithEvents pcode As System.Web.UI.WebControls.DropDownList Protected WithEvents lnklogout As System.Web.UI.WebControls.LinkButton Protected WithEvents lnkView As System.Web.UI.WebControls.LinkButton Protected WithEvents lblrpt As System.Web.UI.WebControls.Label Protected WithEvents dgfile As System.Web.UI.WebControls.DataGrid Protected WithEvents Label7 As System.Web.UI.WebControls.Label Protected WithEvents Label6 As System.Web.UI.WebControls.Label Protected WithEvents dt As System.Web.UI.WebControls.TextBox Protected WithEvents Label5 As System.Web.UI.WebControls.Label Protected WithEvents txtpname As System.Web.UI.WebControls.TextBox Protected WithEvents Label4 As System.Web.UI.WebControls.Label Protected WithEvents lnkDownload As System.Web.UI.WebControls.LinkButton Protected WithEvents Label2 As System.Web.UI.WebControls.Label Protected WithEvents cmdupload As System.Web.UI.WebControls.Button Protected WithEvents file2 As System.Web.UI.HtmlControls.HtmlInputFile Protected WithEvents lbldname As System.Web.UI.WebControls.Label Protected WithEvents lnkhome As System.Web.UI.WebControls.LinkButton 'NOTE: The following placeholder declaration is required by the Web Form Designer. 'Do not delete or move it. Private designerPlaceholderDeclaration As System.Object Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init 'CODEGEN: This method call is required by the Web Form Designer 'Do not modify it using the code editor. InitializeComponent() End Sub #End Region Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim i Dim p() As String If Not IsPostBack() Then lbldname.Text = Session("doctorcode") & "|" & Session("Doctorname") fillPatientList() If Request.QueryString("pcode") <> "" Then Dim pcd = Request.QueryString("pcode") For i = 0 To pcode.Items.Count - 1 p = Split(pcode.Items.Item(i).Value, "|") If p(0) = pcd Then pcode.SelectedIndex = i display() Exit For End If Next End If End If End Sub Public Sub fillPatientList() Dim con As New OleDbConnection, cmd As New OleDbCommand, Rs As OleDbDataReader, pms As OleDbParameterCollection con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;User ID=Admin;Data Source=" & MapPath("db") & "\db.mdb;" If con.State <> 0 Then con.Close() con.Open() pcode.Items.Add("Select") cmd.CommandText = "Select PatientCode,PatientName from Patient Where DoctorCode='" & Session("DoctorCode") & "'" cmd.Connection = con Rs = cmd.ExecuteReader While Rs.Read pcode.Items.Add(Rs.GetValue(0) & "|" & Rs.GetValue(1)) End While cmd.Dispose() con.Close() End Sub Private Sub pcode_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pcode.SelectedIndexChanged display() End Sub Private Sub lnkView_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lnkView.Click Dim p() As String p = Split(pcode.SelectedItem.Value, "|") Server.Transfer("View.aspx?PCode=" & p(0)) End Sub Private Sub dgfile_ItemCommand(ByVal source As System.Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgfile.ItemCommand Dim con As New OleDbConnection, cmd As New OleDbCommand, Rs As OleDbDataReader con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;User ID=Admin;Data Source=" & MapPath("db") & "\db.mdb;" con.Open() Dim lid As Integer = Convert.ToInt32(e.Item.Cells(0).Text), i Dim strfilename As String = e.Item.Cells(1).Text Dim p() As String p = Split(pcode.SelectedItem.Value, "|") Dim dir As System.IO.DirectoryInfo = New System.IO.DirectoryInfo(Server.MapPath("images") & "\" & Session("DoctorCode") & "_" & p(0)) Try dir.Create() Catch ex As Exception Response.Write(ex.ToString()) End Try If dir.Exists Then cmd.CommandText = "Select FileData,ContentType from PatientReport where ID=" & lid cmd.Connection = con Rs = cmd.ExecuteReader If Rs.Read Then Dim objFileStream As FileStream Dim objBinaryWriter As BinaryWriter objFileStream = New FileStream(dir.FullName & "\" & strfilename, FileMode.Create) objBinaryWriter = New BinaryWriter(objFileStream) objBinaryWriter.Write(Rs.GetValue(0)) objBinaryWriter.Close() Dim file As System.IO.FileInfo = New System.IO.FileInfo(dir.FullName & "\" & strfilename) If file.Exists Then Response.Clear() Response.AddHeader("Content-Disposition", "attachment; filename=" & file.Name) Response.AddHeader("Content-Length", file.Length.ToString()) Response.ContentType = Rs.GetValue(1) Response.WriteFile(file.FullName, True) con.Close() Response.End() End If End If End If End Sub Private Sub lnklogout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lnklogout.Click Dim i, j Dim dir As System.IO.DirectoryInfo = New System.IO.DirectoryInfo(Server.MapPath("images")) If dir.Exists Then Dim subdir() As System.IO.DirectoryInfo = dir.GetDirectories(Session("DoctorCode") & "_*") For i = 0 To subdir.Length - 1 If subdir(i).Exists Then Dim fl() As System.IO.FileInfo = subdir(i).GetFiles For j = 0 To fl.Length - 1 If fl(j).Exists Then fl(j).Delete() End If Next End If Next For i = 0 To subdir.Length - 1 If subdir(i).Exists Then subdir(i).Delete() End If Next End If Session.Abandon() Server.Transfer("default.aspx") End Sub Public Sub display() Dim con As New OleDbConnection, cmd As New OleDbCommand, Rs As OleDbDataReader Dim adp As OleDbDataAdapter, ds As New DataSet con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;User ID=Admin;Data Source=" & MapPath("db") & "\db.mdb;" con.Open() Dim p() As String p = Split(pcode.SelectedItem.Value, "|") cmd.CommandText = "Select * from patient where PatientCode='" & p(0) & "'" cmd.Connection = con Rs = cmd.ExecuteReader(CommandBehavior.KeyInfo) If Rs.Read Then txtpname.Text = Rs.GetValue(1) Dim dtr() As String dtr = Split(Rs.GetValue(3), "/") dt.Text = dtr(1) & "/" & dtr(0) & "/" & dtr(2) lblrpt.Text = Rs.GetValue(5) End If Rs.Close() cmd.Dispose() con.Close() If pcode.SelectedIndex = 0 Then txtpname.Text = "" dt.Text = "" lblrpt.Text = "" End If con.Open() p = Split(pcode.SelectedItem.Value, "|") adp = New OleDbDataAdapter("Select ID,FileName from PatientReport where PatientCode='" & p(0) & "'", con) adp.Fill(ds, "PatientReport") dgfile.DataSource = ds.Tables(0) dgfile.DataBind() con.Close() End Sub Private Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Unload If Not IsPostBack() Then Dim i, j Dim dir As System.IO.DirectoryInfo = New System.IO.DirectoryInfo(Server.MapPath("images")) Dim subdir() As System.IO.DirectoryInfo = dir.GetDirectories For i = 0 To subdir.Length - 1 If subdir(i).Exists Then Dim fl() As System.IO.FileInfo = subdir(i).GetFiles For j = 0 To fl.Length - 1 If fl(j).Exists Then fl(j).Delete() End If Next End If Next For i = 0 To subdir.Length - 1 If subdir(i).Exists Then subdir(i).Delete() End If Next End If End Sub Private Sub lnkDownload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lnkDownload.Click Dim con As New OleDbConnection, cmd As New OleDbCommand, Rs As OleDbDataReader con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;User ID=Admin;Data Source=" & MapPath("db") & "\db.mdb;" con.Open() Dim strfilename As String Dim p() As String p = Split(pcode.SelectedItem.Value, "|") Dim dir As System.IO.DirectoryInfo = New System.IO.DirectoryInfo(Server.MapPath("images") & "\" & Session("DoctorCode") & "_" & p(0)) Try dir.Create() Catch ex As Exception Response.Write(ex.ToString()) End Try If dir.Exists Then p = Split(pcode.SelectedItem.Value, "|") cmd.CommandText = "Select rpt,FileName from Patient where PatientCode='" & p(0) & "'" cmd.Connection = con Rs = cmd.ExecuteReader If Rs.Read Then Dim objFileStream As FileStream Dim objBinaryWriter As BinaryWriter strfilename = Rs.GetValue(1) objFileStream = New FileStream(dir.FullName & "\" & strfilename, FileMode.Create) objBinaryWriter = New BinaryWriter(objFileStream) objBinaryWriter.Write(Rs.GetValue(0)) objBinaryWriter.Close() con.Close() Dim file As System.IO.FileInfo = New System.IO.FileInfo(dir.FullName & "\" & strfilename) If file.Exists Then Response.Clear() Response.AddHeader("Content-Disposition", "attachment; filename=" & file.Name) Response.AddHeader("Content-Length", file.Length.ToString()) Response.ContentType = "Word Document" Response.WriteFile(file.FullName, True) Response.End() End If End If End If End Sub Private Sub cmdupload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdupload.Click Dim con As New OleDbConnection, cmd As New OleDbCommand, pms As OleDbParameterCollection con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;User ID=Admin;Data Source=" & MapPath("db") & "\db.mdb;" con.Open() If file2.Value = "" Then Response.Write("") Exit Sub End If Dim p() As String p = Split(pcode.SelectedItem.Value, "|") Dim dir As System.IO.DirectoryInfo = New System.IO.DirectoryInfo(Server.MapPath("images") & "\" & Session("DoctorCode") & "_" & p(0)) Try dir.Create() Catch ex As Exception Response.Write(ex.ToString()) End Try If dir.Exists Then 'Dim file() As System.IO.FileInfo = dir.GetFiles("*.doc") 'If file.Length > 0 Then ' Response.Write("") 'Else Dim strFilename As String strFilename = file2.PostedFile.FileName Dim fl As System.IO.FileInfo = New System.IO.FileInfo(strFilename) If fl.Extension = ".doc" Then strFilename = System.IO.Path.GetFileName(strFilename) file2.PostedFile.SaveAs(dir.FullName & "\" & strFilename) Dim file1 As System.IO.FileInfo = New System.IO.FileInfo(dir.FullName & "\" & strFilename) If file1.Exists Then Dim FSize = file1.Length lblrpt.Text = strFilename Dim binReader As BinaryReader = New BinaryReader(file1.Open(FileMode.Open, FileAccess.Read)) binReader.BaseStream.Position = 0 Dim binFile As Byte() = binReader.ReadBytes(Convert.ToInt32(binReader.BaseStream.Length)) binReader.Close() pms = cmd.Parameters pms.Add("@Rpt", OleDbType.VarBinary) pms.Add("@FileName", OleDbType.VarChar, 100) pms.Add("@FileSize", OleDbType.UnsignedBigInt) pms("@Rpt").Value = binFile pms("@FileName").Value = strFilename '"application/octet-stream" pms("@FileSize").Value = FSize p = Split(pcode.SelectedItem.Value, "|") cmd.CommandText = "Update Patient set Rpt=@Rpt,FileName=@FileName,FileSize=@FileSize where PatientCode='" & p(0) & "'" cmd.Connection = con cmd.ExecuteNonQuery() cmd.Dispose() con.Close() file1.Delete() End If Else Response.Write("") End If End If 'End If End Sub Private Sub lnkhome_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lnkhome.Click Response.Redirect("dochome.aspx") End Sub End Class