Imports System.Data.OleDb Imports System.IO Public Class editpatient 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 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 dt As System.Web.UI.WebControls.TextBox Protected WithEvents Label8 As System.Web.UI.WebControls.Label Protected WithEvents txtpname As System.Web.UI.WebControls.TextBox Protected WithEvents pcode As System.Web.UI.WebControls.DropDownList 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 Protected WithEvents Button2 As System.Web.UI.WebControls.Button Protected WithEvents cmdsave As System.Web.UI.WebControls.Button Protected WithEvents lblrpt As System.Web.UI.WebControls.Label '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 pcode.Items.Clear() 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 fillPatientList() con.Close() End Sub Private Sub lnkRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 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.SelectedItem.Value) If dir.Exists Then Dim file As System.IO.FileInfo = New System.IO.FileInfo(Server.MapPath("images") & "\" & ddId.SelectedItem.Value & "_" & pcode.SelectedItem.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.SelectedIndex = 0 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.SelectedItem.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.SelectedItem.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.SelectedItem.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.SelectedItem.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) strFilename = "" Or con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;User ID=Admin;Data Source=" & MapPath("db") & "\db.mdb;" If pcode.SelectedIndex = 0 Or ddId.SelectedIndex = 0 Or txtpname.Text = "" Or (dd.SelectedIndex = -1 Or mm.SelectedIndex = -1 Or yy.SelectedIndex = -1) Then Response.Write("") Exit Sub End If 'Dim dir As System.IO.DirectoryInfo = New System.IO.DirectoryInfo(Server.MapPath("images") & "\" & ddId.SelectedItem.Value & "_" & pcode.SelectedItem.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.SelectedItem.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 ,Rpt=@Rpt,FileName=@FileName,FileSize=@FileSize con.Open() cmd.CommandType = CommandType.Text cmd.CommandText = "Update Patient set PatientName='" & txtpname.Text & "',dt='" & pdt & "' where PatientCode='" & pcode.SelectedItem.Value & "'" cmd.Connection = con cmd.ExecuteNonQuery() pms = Nothing cmd.Dispose() 'File.Delete() con.Close() 'End If 'End If Response.Redirect("editpatient.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.SelectedItem.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.SelectedItem.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 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 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 from Patient Where DoctorCode='" & ddId.SelectedItem.Value & "'" cmd.Connection = con Rs = cmd.ExecuteReader While Rs.Read pcode.Items.Add(Rs.GetValue(0)) End While cmd.Dispose() con.Close() 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() cmd.CommandText = "Select * from patient where PatientCode='" & pcode.SelectedItem.Value & "'" 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), "/") dd.SelectedItem.Text = dtr(1) mm.SelectedIndex = dtr(0) - 1 yy.SelectedItem.Text = dtr(2) lblrpt.Text = Rs.GetValue(5) End If Rs.Close() cmd.Dispose() con.Close() If pcode.SelectedIndex = 0 Or pcode.SelectedIndex = -1 Then txtpname.Text = "" dd.SelectedIndex = 0 mm.SelectedIndex = 0 yy.SelectedIndex = 0 lblrpt.Text = "" End If con.Open() adp = New OleDbDataAdapter("Select ID,FileName from PatientReport where PatientCode='" & pcode.SelectedItem.Value & "'", con) adp.Fill(ds, "PatientReport") dgfile.DataSource = ds.Tables(0) dgfile.DataBind() 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 lnkhome_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lnkhome.Click Server.Transfer("home.aspx") End Sub 'Private Sub CustomValidator1_ServerValidate(ByVal source As System.Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles CustomValidator1.ServerValidate ' Dim str As String, i, s ' Dim flg = 0 ' str = args.Value ' For i = 0 To str.Length ' s = Mid(str, i, 1) ' If Asc(s) >= 65 And Asc(s) <= 91 Or Asc(s) >= 97 And Asc(s) <= 122 Then ' Else ' flg = 1 ' Exit For ' End If ' Next ' If flg = 1 Then ' CustomValidator1.ErrorMessage = "invalid string" ' End If 'End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Server.Transfer("editpatient.aspx") End Sub End Class