Imports System
Imports System.Data
Imports System.Data.SqlClient

Partial Class Admin_CategoryEmail_Master
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Session("AdminID") Is Nothing Then
            Response.Redirect("AdminLogin.aspx?sesn=expire")
            Exit Sub
        End If
        If Not IsPostBack Then
            fillcategory()
            FillGrid("")                  'Fills the DatagGrid with All Records
            Call CancelClick(Me, btnADD, BtnSave, BtnEdit, BtnDelete, BtnCancel, BtnSearch)
            BtnDelete.Attributes.Add("onclick", " return confirm('Are you sure you want to delete.?');")
            ClearFields()               'Clear the TextFields
            UnLockTextBox(False)
        End If

    End Sub

    Protected Sub ClearFields()
        Try
            txtEmailID.Text = ""
            ddlCategory.ClearSelection()
            'txtBrandImage.Text = ""
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")     'Display the Error Message
        End Try
    End Sub

    '----------------------------------------------------------------------------------------------------------------------------
    '<UserDefinedFunction Name="UnLockTextBox">
    '   <Desc> For Locking & UnLocking the TextFields </Desc>
    '   <Input> 
    '   <Param Name="Action">
    '   True or False
    '   </Param>
    '   </Input>
    '   <Output> Nothing </Output>
    '   <DateOfCreation> 21/06/06 </DateOfCreation>
    '   <Developer> Neeta </Developer>
    '</UserDefinedFunction>
    '----------------------------------------------------------------------------------------------------------------------------
    Protected Sub UnLockTextBox(ByVal Action As Boolean)
        Try
            'txtBrandCode.Enabled = Action                    'Enable / Disable the TextField
            txtEmailID.Enabled = Action
            ddlCategory.Enabled = Action
            'Enable / Disable the TextField


        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")     'Display the Error Message
        End Try
    End Sub

    Protected Sub FillGrid(ByVal PTN As String)
        Try

            Dim qry As String                                   'Define a String Variable for Query
            Dim con As New SqlConnection                        'Define connection variable for connecting to SQL-Server
            con.ConnectionString = System.Configuration.ConfigurationManager.AppSettings("ConnectionString")    'Read the ConnectionString from web.config File
            con.Open()                                          'Open the Connection
            If Trim(PTN) = "" Then
                qry = "SELECT Category_Email.CategoryEmail_EmailID, Category_Master.Category_Name, Category_Email.CategoryEmail_KID, Category_Email.CategoryEmail_IsDeleted FROM Category_Email INNER JOIN Category_Master ON Category_Email.CategoryEMail_CategoryID = Category_Master.Category_Kid WHERE (Category_Email.CategoryEmail_IsDeleted = N'0') order by Category_Master.Category_Name"
            Else
                qry = "SELECT Category_Email.CategoryEmail_EmailID, Category_Master.Category_Name, Category_Email.CategoryEmail_KID, Category_Email.CategoryEmail_IsDeleted FROM Category_Email INNER JOIN Category_Master ON Category_Email.CategoryEMail_CategoryID = Category_Master.Category_Kid WHERE (Category_Email.CategoryEmail_IsDeleted = N'0') and Category_Email.CategoryEmail_EmailID like '" & Trim(PTN) & "%' order by Category_Master.Category_Name"
            End If
            Dim cmd As New SqlCommand(qry, con)                 'Define command variable to execute the query
            Dim dr As SqlDataReader                             'Define DataReader variable for storing the output of the query
            dr = cmd.ExecuteReader                              'Assign the output of query to DataReader variable
            gvCategoryEmail.DataSource = dr                          'Set the DataReader Variable as DataSource for the Grid
            gvCategoryEmail.DataBind()                               'Bind the Data to the Grid
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")     'Display the Error Message
        End Try
    End Sub

    Public Sub fillcategory()
        Fill_Combo("Select Category_KId, Category_Name from Category_Master where Category_ParentId ='Root Category' and Category_IsDeleted ='0' order by Category_Name", ddlCategory)
    End Sub

    Protected Sub BtnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnADD.Click
        Try
            'If CBool(Session("Add_Flag").ToString) = False Then
            '    CreateMessageAlert(Me, "You Do Not Have Access Right To Add New Record !", "StrKeyVal")  'Display the Message 
            '    Exit Sub
            'End If
            ClearFields()                                       'Clears the TextFields
            UnLockTextBox(True)                                 'Enable the TextFields
            Call AddClick(Me, btnADD, BtnSave, BtnEdit, BtnDelete, BtnCancel, BtnSearch)
            ddlCategory.Focus()                                 'Put the Focus to BankCode TextField
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")     'Display the Error Message
        End Try
    End Sub

    Protected Sub BtnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnSave.Click
        Try
            Dim con As New SqlConnection                                                    'Define connection variable for connecting to SQL-Server
            con.ConnectionString = System.Configuration.ConfigurationManager.AppSettings("ConnectionString")    'Read the ConnectionString from web.config File
            con.Open()                                                                       'Open the Connection
            Dim cmd As New SqlCommand                                                       'Define command variable to execute the query
            cmd.CommandType = CommandType.StoredProcedure                                   'Set the Command Type to Stored procedure
            cmd.CommandText = "Category_Email_Proc"                                        'Assign the Name of Stored Procedure, to Execute
            cmd.Connection = con                                                            'Assign the connection
            cmd.Parameters.Add(New SqlParameter("@CategoryEmail_Kid", SqlDbType.NVarChar, 10)).Value = Trim(NewPrimaryKey(Me))       'Add Parameter Banke_Kid and assign the value  to it
            cmd.Parameters.Add(New SqlParameter("@CategoryEmail_Code", SqlDbType.NVarChar, 25)).Value = Trim(Generate_Code("select count(*) from Category_Email where CategoryEmail_Code='", "CEM"))       'Add Parameter Banke_Code and assign the value of txtBrandCode TextField to it
            cmd.Parameters.Add(New SqlParameter("@CategoryEmail_EmailID", SqlDbType.NVarChar, 200)).Value = Trim(txtEmailID.Text)       'Add Parameter Bank_Name and assign the value of txtBrandName TextField to it
            cmd.Parameters.Add(New SqlParameter("@CategoryEmail_CategoryID", SqlDbType.NVarChar, 10)).Value = Trim(ddlCategory.SelectedItem.Value)       'Add Parameter Bank_Image and assign the value of txtBrandImage TextField to it
            cmd.Parameters.Add(New SqlParameter("@CategoryEmail_FinancialYearID", SqlDbType.NVarChar, 20)).Value = Session("g_FinYear").ToString()            'Add Parameter Company_Code and assign the value of Session("g_Company") to it
            cmd.Parameters.Add(New SqlParameter("@CategoryEmail_CompanyId", SqlDbType.NVarChar, 20)).Value = Session("g_Company").ToString       'Add Parameter Company_Code and assign the value of Session("g_Company") to it
            cmd.Parameters.Add(New SqlParameter("@CategoryEmail_UserId", SqlDbType.NVarChar, 20)).Value = Session("g_User").ToString             'Add Parameter UserCode and assign the value of Session("g_User").toString to it
            cmd.Parameters.Add(New SqlParameter("@FormName", SqlDbType.NVarChar, 100)).Value = "Category Email Master"                           'Add Parameter FormName and assign the name of form to it
            cmd.Parameters.Add(New SqlParameter("@Mode", SqlDbType.VarChar, 50)).Value = "Insert"                                   'Add Parameter Mode and assign the DML Operation to Execute (Insert)
            cmd.ExecuteNonQuery()                                                           'Execute the Query
            FillGrid("")                                                                      'Fill the Grid
            CreateMessageAlert(Me, "Record Saved Successfully.", "StrKeyVal")             'Display the Message
            ClearFields()                                                                   'Clear the TextFields
            UnLockTextBox(False)                                                            'Disable the TextFields
            Call SaveClick(Me, btnADD, BtnSave, BtnEdit, BtnDelete, BtnCancel, BtnSearch)
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")                                 'Display the Error Message
        End Try
    End Sub

    Protected Sub BtnEdit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnEdit.Click
        Try
            
            Dim con As New SqlConnection                                                    'Define connection variable for connecting to SQL-Server
            con.ConnectionString = System.Configuration.ConfigurationManager.AppSettings("ConnectionString")    'Read the ConnectionString from web.config File
            con.Open()                                                                  'Open the Connection
            Dim cmd As New SqlCommand                                                       'Define command variable to execute the query
            cmd.CommandType = CommandType.StoredProcedure                                   'Set the Command Type to Stored procedure
            cmd.CommandText = "Category_Email_Proc"                                        'Assign the Name of Stored Procedure, to Execute
            cmd.Connection = con                                                            'Assign the connection
            cmd.Parameters.Add(New SqlParameter("@CategoryEmail_Kid", SqlDbType.NVarChar, 10)).Value = Trim(gvCategoryEmail.SelectedDataKey.Value)       'Add Parameter Brand_Kid and assign the value of txtBrandId TextField  to it
            cmd.Parameters.Add(New SqlParameter("@CategoryEmail_Code", SqlDbType.NVarChar, 25)).Value = ReturnValue("Select CategoryEmail_Code from Category_Email where CategoryEmail_KID ='" & gvCategoryEmail.SelectedDataKey.Value & "'")       'Add Parameter Brand_Code and assign the value of txtBrandCode TextField to it
            cmd.Parameters.Add(New SqlParameter("@CategoryEmail_EmailID", SqlDbType.NVarChar, 100)).Value = Trim(txtEmailID.Text)       'Add Parameter Brand_Name and assign the value oftxtBrandName TextField to it
            cmd.Parameters.Add(New SqlParameter("@CategoryEmail_CategoryID", SqlDbType.NVarChar, 100)).Value = Trim(ddlCategory.SelectedItem.Value)       'Add Parameter Bank_Image and assign the value of txtBrandImage TextField to it
            cmd.Parameters.Add(New SqlParameter("@CategoryEmail_FinancialYearID", SqlDbType.NVarChar, 20)).Value = Session("g_FinYear")       'Add Parameter Company_Code and assign the value of Session("g_Company") to it
            cmd.Parameters.Add(New SqlParameter("@CategoryEmail_CompanyId", SqlDbType.NVarChar, 10)).Value = Session("g_Company")       'Add Parameter Company_Code and assign the value of Session("g_Company") to it
            cmd.Parameters.Add(New SqlParameter("@CategoryEmail_UserId", SqlDbType.NVarChar, 10)).Value = Session("g_User")             'Add Parameter UserCode and assign the value of Session("g_User").toString to it
            cmd.Parameters.Add(New SqlParameter("@FormName", SqlDbType.NVarChar, 100)).Value = "Category Email Master"
            cmd.Parameters.Add(New SqlParameter("@Mode", SqlDbType.VarChar, 50)).Value = "Update"                                   'Add Parameter Mode and assign the DML Operation to Execute (Update)
            cmd.ExecuteNonQuery()                                                           'Execute the Query
            FillGrid("")                                                                      'Fill the Grid
            CreateMessageAlert(Me, "Record Modified Successfully.", "StrKeyVal")             'Display the Message
            ClearFields()                                                                   'Clear the TextFields
            UnLockTextBox(False)                                                            'Disable the TextFields
            Call EditClick(Me, btnADD, BtnSave, BtnEdit, BtnDelete, BtnCancel, BtnSearch)

        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")                                 'Display the Error Message
        End Try

    End Sub

    Protected Sub BtnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnDelete.Click
        Try
            'If CBool(Session("Delete_Flag").ToString) = False Then
            '    CreateMessageAlert(Me, "You Do Not Have Access Right To Delete An Existing Record !", "StrKeyVal")  'Display the Message 
            '    Exit Sub
            'End If
            Dim con As New SqlConnection                                                    'Define connection variable for connecting to SQL-Server
            con.ConnectionString = System.Configuration.ConfigurationManager.AppSettings("ConnectionString")    'Read the ConnectionString from web.config File
            con.Open()                                                                      'Open the Connection
            Dim cmd As New SqlCommand                                                       'Define command variable to execute the query
            cmd.CommandType = CommandType.StoredProcedure                                   'Set the Command Type to Stored procedure
            cmd.CommandText = "Category_Email_Proc"                                        'Assign the Name of Stored Procedure, to Execute
            cmd.Connection = con                                                            'Assign the connection

            cmd.Parameters.Add(New SqlParameter("@CategoryEmail_Kid", SqlDbType.NVarChar, 20)).Value = Trim(gvCategoryEmail.SelectedDataKey.Value)       'Add Parameter Brand_Kid and assign the value of  txtBrandId TextField  to it
            cmd.Parameters.Add(New SqlParameter("@CategoryEmail_Code", SqlDbType.NVarChar, 20)).Value = Trim(ReturnValue("Select CategoryEmail_Code from Category_Email where CategoryEmail_KID ='" & gvCategoryEmail.SelectedDataKey.Value & "'"))       'Add Parameter Brand_Code and assign the value of txtBrandCode TextField to it
            cmd.Parameters.Add(New SqlParameter("@CategoryEmail_EmailID", SqlDbType.NVarChar, 25)).Value = Trim(txtEmailID.Text)       'Add Parameter Brand_Name and assign the value of txtBrandName TextField to it
            cmd.Parameters.Add(New SqlParameter("@CategoryEmail_CategoryID", SqlDbType.NVarChar, 100)).Value = Trim(ddlCategory.SelectedItem.Value)       'Add Parameter Brand_Image and assign the value of txtBrandImage TextField to it
            cmd.Parameters.Add(New SqlParameter("@CategoryEmail_FinancialYearID", SqlDbType.NVarChar, 20)).Value = Session("g_FinYear")       'Add Parameter Company_Code and assign the value of Session("g_Company") to it
            cmd.Parameters.Add(New SqlParameter("@CategoryEmail_CompanyId", SqlDbType.NVarChar, 20)).Value = Session("g_Company")       'Add Parameter Company_Code and assign the value of Session("g_Company") to it
            cmd.Parameters.Add(New SqlParameter("@CategoryEmail_UserId", SqlDbType.NVarChar, 20)).Value = Session("g_User")             'Add Parameter UserCode and assign the value of Session("g_User").toString to it
            cmd.Parameters.Add(New SqlParameter("@FormName", SqlDbType.NVarChar, 100)).Value = "Category Email Master"
            cmd.Parameters.Add(New SqlParameter("@Mode", SqlDbType.VarChar, 50)).Value = "Delete"                                   'Add Parameter Mode and assign the DML Operation to Execute (Delete)
            cmd.ExecuteNonQuery()                                                           'Execute the Query
            FillGrid("")                                                                      'Fill the Grid
            CreateMessageAlert(Me, "Record Deleted Successfully...", "StrKeyVal")             'Display the Message
            ClearFields()                                                                   'Clear the TextFields
            UnLockTextBox(False)                                                            'Disable the TextFields
            Call DeleteClick(Me, btnADD, BtnSave, BtnEdit, BtnDelete, BtnCancel, BtnSearch)
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")                                 'Display the Error Message
        End Try

    End Sub

    Protected Sub BtnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnCancel.Click
        Try
            ClearFields()                                       'Clears the TextFields
            UnLockTextBox(False)                                'Disable the TextFields
            Call CancelClick(Me, btnADD, BtnSave, BtnEdit, BtnDelete, BtnCancel, BtnSearch)
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")     'Display the Error Message
        End Try
    End Sub

    Protected Sub BtnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnSearch.Click
        'Search = True                                       'Set Search Mode True
        'Call SearchClick(Me, btnADD, BtnSave, BtnEdit, BtnDelete, BtnCancel, BtnSearch)
        'txtBrandName.Enabled = True
        'txtBrandName.AutoPostBack = True
        ''txtBrandImage.Enabled = True
        ''txtBrandImage.AutoPostBack = True
        ''Enable txtPartTypeName TextField
        ''CalltxtBrandName_TextChanged(sender, e)
        'txtBrandName.Focus()
    End Sub

    Protected Sub gvCategoryEmail_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvCategoryEmail.SelectedIndexChanged
        Try

            txtEmailID.Text = gvCategoryEmail.SelectedRow.Cells(1).Text     'Assign the value of cell to the textField
            ddlCategory.ClearSelection()
            ddlCategory.Items.FindByValue(ReturnValue("Select CategoryEmail_CategoryId from Category_Email where CategoryEmail_KID ='" & gvCategoryEmail.SelectedDataKey.Value & "'")).Selected = True
            UnLockTextBox(True)                                         'Enable the TextFields
            Call GridClick(Me, btnADD, BtnSave, BtnEdit, BtnDelete, BtnCancel, BtnSearch)
            txtEmailID.Focus()
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")                 'Display the Error Message
        End Try
    End Sub
End Class
