Imports System.Data.OleDb Imports System.IO Public Class WebForm1 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 ddId As System.Web.UI.WebControls.DropDownList Protected WithEvents Label1 As System.Web.UI.WebControls.Label Protected WithEvents Label2 As System.Web.UI.WebControls.Label Protected WithEvents Label3 As System.Web.UI.WebControls.Label Protected WithEvents txtdname As System.Web.UI.WebControls.TextBox Protected WithEvents Label4 As System.Web.UI.WebControls.Label Protected WithEvents lnklogout As System.Web.UI.WebControls.LinkButton Protected WithEvents Label5 As System.Web.UI.WebControls.Label Protected WithEvents Label6 As System.Web.UI.WebControls.Label Protected WithEvents file2 As System.Web.UI.HtmlControls.HtmlInputFile Protected WithEvents dgfile As System.Web.UI.WebControls.DataGrid Protected WithEvents cmdOK As System.Web.UI.WebControls.Button Protected WithEvents Label7 As System.Web.UI.WebControls.Label Protected WithEvents file1 As System.Web.UI.HtmlControls.HtmlInputFile Protected WithEvents cmdSave As System.Web.UI.WebControls.Button Protected WithEvents cmdCancel As System.Web.UI.WebControls.Button Protected WithEvents dt As System.Web.UI.WebControls.TextBox Protected WithEvents Label8 As System.Web.UI.WebControls.Label Protected WithEvents cmdrpt As System.Web.UI.WebControls.Button Protected WithEvents lblrpt As System.Web.UI.WebControls.Label Protected WithEvents lnkRemove As System.Web.UI.WebControls.LinkButton Protected WithEvents txtpname As System.Web.UI.WebControls.TextBox Protected WithEvents pcode As System.Web.UI.HtmlControls.HtmlInputText Protected WithEvents dd As System.Web.UI.WebControls.DropDownList Protected WithEvents mm As System.Web.UI.WebControls.DropDownList Protected WithEvents yy As System.Web.UI.WebControls.DropDownList 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 If Not IsPostBack() Then Session("flg") = 0 FillDoctorList() End If End Sub Private Sub ddId_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ddId.SelectedIndexChanged Dim con As New OleDbConnection, cmd As New OleDbCommand, Rs As OleDbDataReader If ddId.SelectedIndex = 0 Then txtdname.Text = "" Exit Sub End If con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;User ID=Admin;Data Source=" & MapPath("db") & "\db.mdb;" con.Open() cmd.CommandText = "select * from Doctor where DoctorCode='" & ddId.SelectedItem.Value & "'" cmd.Connection = con Rs = cmd.ExecuteReader If Rs.Read Then txtdname.Text = Rs.GetValue(1) End If con.Close() End Sub Private Sub cmdrpt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdrpt.Click If Session("flg") = 1 Then Session("flg") = 0 Exit Sub End If If pcode.Value = "" Or ddId.SelectedIndex = 0 Then Response.Write("") Exit Sub End If If file2.PostedFile.FileName = "" Then Response.Write("") Exit Sub End If Dim dir As System.IO.DirectoryInfo = New System.IO.DirectoryInfo(Server.MapPath("images") & "\" & ddId.SelectedItem.Value & "_" & pcode.Value) 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) lblrpt.Text = strFilename Else Response.Write("") End If End If End If End Sub Private Sub lnkRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lnkRemove.Click If lblrpt.Text = "" Then Exit Sub End If Dim dir As System.IO.DirectoryInfo = New System.IO.DirectoryInfo(Server.MapPath("images") & "\" & ddId.SelectedItem.Value & "_" & pcode.Value) If dir.Exists Then Dim file As System.IO.FileInfo = New System.IO.FileInfo(Server.MapPath("images") & "\" & ddId.SelectedItem.Value & "_" & pcode.Value & "\" & lblrpt.Text) If file.Exists Then file.Delete() lblrpt.Text = "" End If End If End Sub Private Sub cmdOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOK.Click If file1.Value = "" Then Response.Write("") Exit Sub End If If ddId.SelectedIndex = 0 Or pcode.Value = "" Then Response.Write("") Exit Sub End If Dim adp As OleDbDataAdapter, ds As New DataSet Dim con As New OleDbConnection, cmd As New OleDbCommand, Rs As OleDbDataReader, pms As OleDbParameterCollection Dim strFilename As String, f As Integer = 0 strFilename = file1.PostedFile.FileName strFilename = System.IO.Path.GetFileName(strFilename) Dim fl As System.IO.FileInfo = New System.IO.FileInfo(strFilename) If fl.Extension = ".jpg" Then 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() cmd.CommandText = "Select FileName from PatientReport where DoctorCode='" & ddId.SelectedItem.Value & "' and PatientCode='" & pcode.Value & "'" cmd.Connection = con Rs = cmd.ExecuteReader While Rs.Read If Rs.GetValue(0) = strFilename Then f = 1 Exit While End If End While cmd.Dispose() con.Close() If f = 0 Then Dim dir As System.IO.DirectoryInfo = New System.IO.DirectoryInfo(Server.MapPath("images") & "\" & ddId.SelectedItem.Value & "_" & pcode.Value) Try dir.Create() Catch ex As Exception Response.Write(ex.ToString()) End Try If dir.Exists Then file1.PostedFile.SaveAs(dir.FullName & "\" & strFilename) Dim path As String = dir.FullName & "\" & strFilename Dim file As System.IO.FileInfo = New System.IO.FileInfo(path) Dim FSize = file.Length If file.Exists Then Dim binReader As BinaryReader = New BinaryReader(file.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("@DoctorCode", OleDbType.VarChar, 10) pms.Add("@PatientCode", OleDbType.VarChar, 10) pms.Add("@FileData", OleDbType.VarBinary) pms.Add("@ContentType", OleDbType.VarChar, 50) pms.Add("@FileName", OleDbType.VarChar, 50) pms.Add("@FileSize", OleDbType.UnsignedBigInt) pms("@DoctorCode").Value = ddId.SelectedItem.Value pms("@PatientCode").Value = pcode.Value pms("@FileData").Value = binFile pms("@ContentType").Value = file1.PostedFile.ContentType '"application/octet-stream" pms("@FileName").Value = strFilename pms("@FileSize").Value = FSize con.Open() cmd.CommandType = CommandType.Text cmd.CommandText = "insert into PatientReport (DoctorCode,PatientCode,FileData,ContentType,FileName,FileSize) values(@DoctorCode,@PatientCode,@FileData,@ContenType,@FileName,@FileSize)" cmd.Connection = con cmd.ExecuteNonQuery() pms = Nothing cmd.Dispose() file.Delete() con.Close() End If End If If con.State <> 0 Then con.Close() con.Open() adp = New OleDbDataAdapter("Select ID,FileName from PatientReport where DoctorCode='" & ddId.SelectedItem.Value & "' and PatientCode='" & pcode.Value & "'", con) adp.Fill(ds, "PatientReport") dgfile.DataSource = ds.Tables(0) dgfile.DataBind() con.Close() Else Response.Write("") End If Else Response.Write("") End If End Sub Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click Dim adp As OleDbDataAdapter, ds As New DataSet Dim con As New OleDbConnection, cmd As New OleDbCommand, Rs As OleDbDataReader, pms As OleDbParameterCollection Dim strFilename As String, f As Integer = 0 strFilename = lblrpt.Text 'strFilename = System.IO.Path.GetFileName(strFilename) con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;User ID=Admin;Data Source=" & MapPath("db") & "\db.mdb;" If strFilename = "" Or pcode.Value = "" Or ddId.SelectedIndex = 0 Then Response.Write("") Exit Sub End If Dim dir As System.IO.DirectoryInfo = New System.IO.DirectoryInfo(Server.MapPath("images") & "\" & ddId.SelectedItem.Value & "_" & pcode.Value) Try dir.Create() Catch ex As Exception Response.Write(ex.ToString()) End Try If dir.Exists Then Dim path As String = dir.FullName & "\" & strFilename Dim file As System.IO.FileInfo = New System.IO.FileInfo(path) Dim FSize = file.Length If file.Exists Then Dim binReader As BinaryReader = New BinaryReader(file.Open(FileMode.Open, FileAccess.Read)) binReader.BaseStream.Position = 0 Dim binFile As Byte() = binReader.ReadBytes(Convert.ToInt32(binReader.BaseStream.Length)) binReader.Close() Dim pdt As String pdt = (mm.SelectedIndex + 1) & "/" & dd.SelectedItem.Value & "/" & yy.SelectedItem.Value pms = cmd.Parameters pms.Add("@PatientCode", OleDbType.VarChar, 10) pms.Add("@PatientName", OleDbType.VarChar, 50) pms.Add("@DoctorCode", OleDbType.VarChar, 10) pms.Add("@Dt", OleDbType.Date) pms.Add("@Rpt", OleDbType.VarBinary) pms.Add("@FileName", OleDbType.VarChar, 100) pms.Add("@FileSize", OleDbType.UnsignedBigInt) pms("@PatientCode").Value = pcode.Value pms("@PatientName").Value = txtpname.Text pms("@DoctorCode").Value = ddId.SelectedItem.Value pms("@Dt").Value = pdt pms("@Rpt").Value = binFile pms("@FileName").Value = strFilename pms("@FileSize").Value = FSize con.Open() cmd.CommandType = CommandType.Text cmd.CommandText = "insert into Patient (PatientCode,PatientName,DoctorCode,dt,Rpt,FileName,FileSize) values(@PatientCode,@PatientName,@DoctorCode,@Dt,@Rpt,@FileName,@FileSize)" cmd.Connection = con cmd.ExecuteNonQuery() pms = Nothing cmd.Dispose() file.Delete() con.Close() End If End If Response.Redirect("admin.aspx") 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 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 iid As Integer = Convert.ToInt32(e.Item.Cells(0).Text) 'Grab the ID from the hidden column Dim strfilename As String = e.Item.Cells(1).Text Dim dir As System.IO.DirectoryInfo = New System.IO.DirectoryInfo(Server.MapPath("images" & "\" & ddId.SelectedItem.Value & "_" & pcode.Value)) If dir.Exists Then Dim file As System.IO.FileInfo = New System.IO.FileInfo(dir.FullName & "\" & strfilename) If (file.Exists) Then file.Delete() End If End If cmd.CommandText = "DELETE FROM PatientReport WHERE ID =" & iid & "" cmd.Connection = con cmd.ExecuteNonQuery() cmd.Dispose() con.Close() If con.State <> 0 Then con.Close() con.Open() adp = New OleDbDataAdapter("Select ID,FileName from PatientReport where DoctorCode='" & ddId.SelectedItem.Value & "' and PatientCode='" & pcode.Value & "'", con) adp.Fill(ds, "PatientReport") dgfile.DataSource = ds.Tables(0) dgfile.DataBind() con.Close() End Sub Private Sub dgfile_ItemDataBound(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgfile.ItemDataBound Dim l As LinkButton If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then l = CType(e.Item.Cells(2).FindControl("cmdDel"), LinkButton) l.Attributes.Add("onclick", "return confirm('Are you sure you want to delete this item?');") End If End Sub Public Sub FillDoctorList() 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() cmd.CommandText = "Select DoctorCode from Doctor" cmd.Connection = con Rs = cmd.ExecuteReader ddId.Items.Add("Select") While Rs.Read ddId.Items.Add(Rs.GetValue(0)) End While Rs.Close() con.Close() End Sub Private Sub cmdcancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCancel.Click Dim i, j 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 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 cmd.CommandText = "DELETE FROM PatientReport WHERE PatientCode='" & pcode.Value & "' and DoctorCode='" & ddId.SelectedItem.Value & "'" cmd.Connection = con cmd.ExecuteNonQuery() cmd.Dispose() con.Close() Response.Redirect("admin.aspx") End Sub Private Sub lnklogout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lnklogout.Click Server.Transfer("default.aspx") 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 pcode_ServerChange(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pcode.ServerChange 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() cmd.CommandText = "Select PatientCode from Patient where PatientCode='" & pcode.Value & "'" cmd.Connection = con Rs = cmd.ExecuteReader If Rs.Read Then Response.Write("") pcode.Value = "" Session("flg") = 1 End If cmd.Dispose() con.Close() End Sub Private Sub lnkhome_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lnkhome.Click Server.Transfer("home.aspx") End Sub End Class