Imports System.Data.OleDb
Imports AcggDoc.Acgg.dbClass

Namespace Acgg
    Public Class Properties
        Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

        'This call is required by the Web Form Designer.
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

        End Sub
        Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
        Protected WithEvents Label1 As System.Web.UI.WebControls.Label
        Protected WithEvents txtDesc As System.Web.UI.WebControls.TextBox
        Protected WithEvents Label3 As System.Web.UI.WebControls.Label
        Protected WithEvents txtAdd As System.Web.UI.WebControls.TextBox
        Protected WithEvents Label2 As System.Web.UI.WebControls.Label
        Protected WithEvents txtTaxID As System.Web.UI.WebControls.TextBox
        Protected WithEvents Label5 As System.Web.UI.WebControls.Label
        Protected WithEvents txtLegal As System.Web.UI.WebControls.TextBox
        Protected WithEvents cmdOk As System.Web.UI.WebControls.Button
        Protected WithEvents Label6 As System.Web.UI.WebControls.Label
        Protected WithEvents cmbDev As System.Web.UI.WebControls.DropDownList
        Protected WithEvents cmdCancel As System.Web.UI.WebControls.Button
        Protected WithEvents lnkRefresh As System.Web.UI.WebControls.HyperLink
        Protected WithEvents Label9 As System.Web.UI.WebControls.Label
        Protected WithEvents lblError As System.Web.UI.WebControls.Label
        Protected WithEvents lblCap 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
        'Dim sConnString As String

        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'Put user code to initialize the page here
            'sConnString = "Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Registry Path=;Jet OLEDB:Database Locking Mode=1;Jet OLEDB:Database Password=;Data Source=" & Server.MapPath("AcggDoc.mdb") & ";Password=;Jet OLEDB:Engine Type=5;Jet OLEDB:Global Bulk Transactions=1;Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:System database=;Jet OLEDB:SFP=False;Extended Properties=;Mode=Share Deny None;Jet OLEDB:New Database Password=;Jet OLEDB:Create System Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;User ID=Admin;Jet OLEDB:Encrypt Database=False"

            If Session("UserId") = "" Then Response.End()

            If Session("UserCat") = 1 Or Session("UserCat") = 2 Then
                cmdOk.Enabled = True
            Else
                cmdOk.Enabled = False
            End If

            If Not Page.IsPostBack Then
                BindGrid()
                BindDevCombo()
                If Request.QueryString("PropId") <> "" Then
                    lblCap.Text = "Edit unit details..."
                    lblCap.ForeColor = System.Drawing.Color.Red
                    cmdOk.Text = "Edit"
                    PopulateFields()
                Else
                    lblCap.Text = "Add unit details..."
                    lblCap.ForeColor = System.Drawing.Color.SteelBlue
                    cmdOk.Text = "Add"
                End If
            End If

        End Sub

        Private Sub PopulateFields()
            Dim objDBClass As New Acgg.dbClass
            Dim objRead As OleDbDataReader
            Dim strQuery As String = "SELECT PropId,PropDesc,PropAdd,PropTaxID,PropFullLegal,DevId FROM Property WHERE PropId=" & Request.QueryString("propId")
            Try
                objDBClass.OpenDB()
                objRead = objDBClass.retReader(strQuery)
                cmbDev.ClearSelection()
                Do While objRead.Read()
                    txtDesc.Text = objRead("PropDesc")
                    txtAdd.Text = objRead("PropAdd")
                    txtTaxID.Text = objRead("PropTaxID")
                    txtLegal.Text = objRead("PropFullLegal")
                    cmbDev.Items.FindByValue(objRead("DevId")).Selected = True
                Loop
                objRead.Close()
                objDBClass.CloseDB()

            Catch Exc As Exception
                lblError.Text = "Error: " & Exc.Message
            Finally
                objRead = Nothing
                objDBClass = Nothing
            End Try
        End Sub

        Private Sub BindGrid()
            Dim objDBClass As New Acgg.dbClass
            Dim strQuery As String = "SELECT A.PropId,A.PropDesc,A.PropAdd,B.DevName FROM Property A , Developments B WHERE A.DevID=B.DevId"
            Try
                objDBClass.OpenDB()
                DataGrid1.DataSource = objDBClass.retDataSet(strQuery)
                DataGrid1.DataBind()
                objDBClass.CloseDB()
                txtDesc.Text = ""
                txtAdd.Text = ""
                txtTaxID.Text = ""
                txtLegal.Text = ""


            Catch Exc As Exception
                lblError.Text = "ERROR : " & Exc.Message
            Finally
                objDBClass = Nothing
            End Try
        End Sub

        Private Sub BindDevCombo()
            Dim objDBClass As New Acgg.dbClass
            Dim strQuery As String = "SELECT DevID,DevName FROM Developments"
            Try
                objDBClass.OpenDB()
                cmbDev.DataSource = objDBClass.retReader(strQuery)
                cmbDev.DataMember = "DevId"
                cmbDev.DataTextField = "DevName"
                cmbDev.DataValueField = "DevId"
                cmbDev.DataBind()
                objDBClass.CloseDB()
            Catch Exc As Exception
                lblError.Text = "ERROR : " & Exc.Message
            Finally
                objDBClass = Nothing
            End Try

        End Sub

        Private Sub cmdOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOk.Click
            Dim objDBClass As New Acgg.dbClass
            Dim objRead As OleDb.OleDbDataReader
            Dim strQuery As String

            Try
                If boolValidateFields() = False Then
                    lblError.Text = "You have missed out on one or more compulsory fields."
                    Exit Sub
                End If
                If Request("PropId") = "" Then
                    strQuery = "INSERT INTO Property(PropDesc,PropAdd,PropTaxID,PropFullLegal,DevId) VALUES('" & Replace(Request("txtDesc"), "'", "") & "','" & Replace(Request("txtAdd"), "'", "") & "','" & Replace(Request("txtTaxID"), "'", "") & "','" & Replace(Request("txtLegal"), "'", "") & "'," & Request("cmbDev") & ")"
                Else
                    strQuery = "UPDATE Property SET PropDesc='" & Replace(Request("txtDesc"), "'", "") & "',PropAdd='" & Replace(Request("txtAdd"), "'", "") & "',PropTaxID='" & Replace(Request("txtTaxID"), "'", "") & "',PropFullLegal='" & Replace(Request("txtLegal"), "'", "") & "',DevId=" & Request("cmbDev") & " WHERE PropId=" & Request("PropId")
                End If

                objDBClass.OpenDB()
                If objDBClass.boolManipulateData(strQuery) Then
                    lblError.Text = "Updation successful..."
                End If
                objDBClass.CloseDB()
                'Response.Redirect("Properties.aspx")
                BindGrid()

            Catch Exc As Exception
                lblError.Text = "Error: " & Exc.Message
            Finally
                objRead = Nothing
                objDBClass = Nothing
            End Try
        End Sub

        Function boolValidateFields() As Boolean
            If Trim(txtDesc.Text) = "" Or Trim(txtAdd.Text) = "" Or Trim(txtTaxID.Text) = "" Or Trim(txtLegal.Text) = "" Then
                boolValidateFields = False
                Exit Function
            Else
                boolValidateFields = True
            End If
        End Function

        Private Sub cmdCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCancel.Click
            Response.Redirect("Properties.aspx")
        End Sub

        Sub Paginate(ByVal Source As Object, ByVal E As DataGridPageChangedEventArgs)
            DataGrid1.CurrentPageIndex = E.NewPageIndex
            BindGrid()
        End Sub

        Private Sub DataGrid1_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles DataGrid1.PageIndexChanged

        End Sub

        Private Sub DataGrid1_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemCreated
            If e.Item.ItemType = ListItemType.Pager Then
                Dim oControl As Control
                Dim x As Integer
                x = 0

                For Each oControl In e.Item.Cells(0).Controls
                    If x > 0 Then Exit For
                    If oControl.GetType.ToString = "System.Web.UI.WebControls.DataGridLinkButton" Then
                        CType(oControl, LinkButton).Text = "Page " & CType(oControl, LinkButton).Text
                        x = x + 1
                    ElseIf TypeOf oControl Is Label Then
                        CType(oControl, Label).Text = "Page " & CType(oControl, Label).Text
                        x = x + 1
                    End If
                Next
            End If
        End Sub
    End Class
End Namespace