Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Collections
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.IO
Imports System.IO.FileStream
Imports ModCommon
Imports Ajax


Partial Class BrandMaster
    Inherits System.Web.UI.Page

    Shared Search As Boolean
    Shared g_User As String
    Shared g_Company As String
    Shared g_FinYear As String
    Shared str As String
    Public strPath As String
    Shared img As Integer
    Dim strImageName As String = ""
    Dim flag As String
    Dim FlagSearch As Boolean = False

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            Ajax.Utility.RegisterTypeForAjax(GetType(BrandMaster))

            If Session("AdminID") Is Nothing Then
                Response.Redirect("AdminLogin.aspx?sesn=expire")
                Exit Sub
            End If
            If (Not IsPostBack) Then        'If IsPostBack is false then
                ClearFields()               'Clear the TextFields
                UnLockTextBox(False)        'Unlock (Enable) TextFields
                'g_User = "U1"
                'g_Company = "COM1"
                g_User = Session("g_User").ToString
                g_Company = Session("g_Company").ToString
                g_FinYear = Session("g_FinYear").ToString()
                FillGrid("")                  'Fills the DatagGrid with All Records
                Call CancelClickVisible(Me, btnADD, BtnSave, BtnEdit, BtnDelete, BtnCancel, BtnSearch)
                Search = False              'Default Value for Search Mode
                Call CheckRights(Me, g_User, "Brand Master")
                img = 0
                'txtBrandCode.Visible = False
                'txtBrandId.Visible = False

                Fill_ComboWithSelect("SELECT  Supplier_Kid, Supplier_Name  FROM Supplier_Master WHERE     (Supplier_IsDeleted = '0') ORDER BY Supplier_Name", ddlSupplier, "Select Supplier")


            End If
            txtBrandCode.MaxLength = 20 : txtBrandName.MaxLength = 50
            txtBrandName.Attributes.Add("onblur", "return CheckNull('txtBrandName');")
            txtBrandName.Attributes.Add("onkeypress", "return BlockNumericValue(event);")
            BrandFileUpload.Attributes.Add("onblur", "return  CheckNull('File1');")
            BtnDelete.Attributes.Add("onclick", " return confirm('Are you Sure ! Want to Delete ?');")
            BtnEdit.Attributes.Add("onclick", "return CheckUpdateBrand();")
            ' CheckOthers()
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")
        End Try
    End Sub
    Protected Sub _Default_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init

        If Not IsValidConnectionString() Then CreateMessageAlert(Me, "Check the Connection String !", "StrKeyVal") 'If not a valid ConnectionString then Display the message 

    End Sub
    '----------------------------------------------------------------------------------------------------------------------------
    '<UserDefinedFunction Name="ClearFields">
    '   <Desc> For Clearing the contents of TextFields </Desc>
    '   <Input> Nothing </Input>
    '   <Output> Nothing </Output>
    '   <DateOfCreation> 21/06/06 </DateOfCreation>
    '   <Developer> Neeta </Developer>
    '</UserDefinedFunction>
    '----------------------------------------------------------------------------------------------------------------------------
    Protected Sub ClearFields()
        Try
            txtBrandId.Text = ""
            txtBrandCode.Text = ""                           'Assign Null to the TextField
            txtBrandName.Text = ""                           'Assign Null to the TextField
            BrandImage.ImageUrl = ""
            ddlSupplier.ClearSelection()
            ddlSupplier.SelectedIndex = -1
            '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
            txtBrandName.Enabled = Action
            BrandFileUpload.Enabled = Action
            ddlSupplier.Enabled = Action
            'Enable / Disable the TextField


        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")     'Display the Error Message
        End Try
    End Sub
    '----------------------------------------------------------------------------------------------------------------------------
    '<UserDefinedFunction Name="FillGrid">
    '   <Desc> To Fill the Grid with All Records </Desc>
    '   <Input> 
    '  <Param Name="PTN">
    '   PartType_Name
    '   </Param>
    '   </Input>
    '   <Output> Nothing </Output>
    '   <DateOfCreation> 21/06/06 </DateOfCreation>
    '   <Developer> Neeta </Developer>
    '</UserDefinedFunction>
    '----------------------------------------------------------------------------------------------------------------------------
    Protected Sub FillGrid(ByVal PTN As String)
        Try

            If (txtBrandName.Text <> "") Then
                PTN = txtBrandName.Text.ToString.Trim
            End If

            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     Brand_Master.Brand_Kid, Brand_Master.Brand_Code AS Code, Brand_Master.Brand_Name AS Brand, Brand_Master.Brand_Image AS Image, Brand_Master.Brand_CompanyId FROM         Brand_Master LEFT OUTER JOIN     Supplier_Master ON Brand_Master.Brand_CompanyId = Supplier_Master.Supplier_Kid WHERE     (Brand_Master.Brand_IsDeleted = '0') ORDER BY Supplier_Master.Supplier_Name DESC"
                qry = "SELECT     Brand_Kid, Brand_Code AS Code, Brand_Name AS Brand, Brand_Image AS Image, Brand_CompanyId   FROM Brand_Master WHERE   Brand_IsDeleted = '0' Order by Brand_Name"
            Else
                '  qry =  "SELECT Brand_Master.Brand_Kid, Brand_Master.Brand_Code AS Code, Brand_Master.Brand_Name AS Brand, Brand_Master.Brand_Image AS Image,Brand_CompanyId FROM   Brand_Master LEFT OUTER JOIN Supplier_Master ON Brand_Master.Brand_CompanyId = Supplier_Master.Supplier_Kid WHERE   Brand_Master.Brand_IsDeleted = '0' AND Brand_name like '" & Trim(PTN) & "%' ORDER BY Supplier_Master.Supplier_Name desc"
                qry = "SELECT     Brand_Kid, Brand_Code AS Code, Brand_Name AS Brand, Brand_Image AS Image, Brand_CompanyId   FROM Brand_Master WHERE    Brand_IsDeleted = '0'  AND Brand_Name LIKE '" & Trim(PTN) & "%' Order by Brand_Name"
            End If

            gvBrand.Columns(1).Visible = True
            gvBrand.Columns(2).Visible = True
            gvBrand.Columns(5).Visible = True
            gvBrand.Columns(6).Visible = True
            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
            Dim dt As New DataTable
            dt = ReturnDataTable(qry)
            gvBrand.DataSource = dt                          'Set the DataReader Variable as DataSource for the Grid
            gvBrand.DataBind()                               'Bind the Data to the Grid
            gvBrand.Columns(1).Visible = False
            gvBrand.Columns(2).Visible = False
            gvBrand.Columns(5).Visible = False
            gvBrand.Columns(6).Visible = False
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")     'Display the Error Message
        End Try
    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 AddClickVisible(Me, btnADD, BtnSave, BtnEdit, BtnDelete, BtnCancel, BtnSearch)
            Call GenerateBrandCode()
            txtBrandName.Focus()                                 'Put the Focus to BankCode TextField
            ''   BtnSave.Attributes.Add("onclick", "return ConfrmMessage();")
            BtnSave.Attributes.Add("onclick", "return CheckProductBrand();")


        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")     'Display the Error Message
        End Try
    End Sub
    Protected Sub gvBrand_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvBrand.RowDataBound
        Try
            If e.Row.RowType = DataControlRowType.DataRow Then
                'for Image
                Dim Pimg As Image
                Pimg = CType(e.Row.Cells(4).FindControl("Image1"), Image)
                Pimg.ImageUrl = CType(e.Row.Cells(5).FindControl("lblImageUrl"), Label).Text.ToString
                Dim imgpath As String
                imgpath = Server.MapPath(Pimg.ImageUrl)
                KeepAspectRatioNew(Pimg, imgpath, 40, 30)

                'for SupplierName
                Dim compid As String
                compid = e.Row.Cells(6).Text
                Dim array = Split(compid, "-")
                Dim i As Integer
                Dim supname As String = ""
                Dim sname As String
                Dim len As Integer = array.Length - 1
                Dim arr_supname(len) As String
                For i = 0 To len
                    sname = ReturnValue("Select Supplier_Name from Supplier_Master where Supplier_Kid='" & RemoveLiterals(array(i).ToString) & "' And Supplier_IsDeleted='0'")
                    arr_supname(i) = sname
                Next

                System.Array.Sort(arr_supname)

                For i = 0 To len
                    If arr_supname(i) <> "" Then
                        supname += "<img alt='' src='../Images/red_dot.gif'/>&nbsp;&nbsp;" & arr_supname(i).ToString & "<br/>"
                    End If
                Next

                e.Row.Cells(7).Text = supname.ToString

            End If
            If e.Row.RowType = DataControlRowType.Footer Then
                e.Row.Cells(3).Text = "Page " & (gvBrand.PageIndex + 1) & " of " & gvBrand.PageCount
            End If
        Catch ex As Exception

        End Try
    End Sub
    Protected Sub gvBrand_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvBrand.SelectedIndexChanged
        Try

            'Dim dk As String
            'dk = ReturnValue("select Brand_CompanyId from Brand_Master where Brand_Kid='" & RemoveLiterals(txtBrandId.Text) & "' and Brand_IsDeleted='0'")

            'Dim dk1 As String
            'dk1 = ReturnValue("select Supplier_Name from Supplier_Master where Supplier_Kid ='" & RemoveLiterals(dk) & "' and Supplier_IsDeleted='0'")
            'If dk1 <> "" Then
            '    ddlSupplier.ClearSelection()
            '    ddlSupplier.Items.FindByValue(RemoveLiterals(dk)).Selected = True
            'Else
            '    ddlSupplier.ClearSelection()
            '    ddlSupplier.SelectedIndex = -1
            'End If


            'If gvBrand.SelectedRow.Cells(6).Text <> "" And gvBrand.SelectedRow.Cells(6).Text <> "&nbsp;" Then
            '    ddlSupplier.ClearSelection()
            '    ddlSupplier.Items.FindByText(gvBrand.SelectedRow.Cells(6).Text).Selected = True
            'Else
            '    ddlSupplier.ClearSelection()
            '    ddlSupplier.SelectedIndex = -1
            'End If

            ddlSupplier.SelectedItem.Text = "Select Supplier"
            txtBrandId.Text = RemoveLiterals(gvBrand.SelectedRow.Cells(1).Text)     'Assign the value of cell to the textField
            hfkid.Value = RemoveLiterals(gvBrand.SelectedRow.Cells(1).Text)
            txtBrandCode.Text = gvBrand.SelectedRow.Cells(2).Text   'Assign the value of cell to the textField
            txtBrandName.Text = gvBrand.SelectedRow.Cells(3).Text   'Assign the value of cell to the textField

            BrandImage.ImageUrl = CType(gvBrand.SelectedRow.FindControl("lblImageUrl"), Label).Text 'Assign the value of cell to the textField 
            Dim imgpath As String
            imgpath = Server.MapPath(BrandImage.ImageUrl)
            KeepAspectRatioNew(BrandImage, imgpath, 50, 40)
            UnLockTextBox(True)                                         'Enable the TextFields
            Call GridClickVisible(Me, btnADD, BtnSave, BtnEdit, BtnDelete, BtnCancel, BtnSearch)
            Search = False
            txtBrandName.Focus()
            txtBrandName.AutoPostBack = False
            ''   BtnEdit.Attributes.Add("onclick", "return ConfrmMessage();")
        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
            '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

            If ddlSupplier.SelectedItem.Text = "Select Supplier" Then
                CreateMessageAlert(Me, "Select Proper Supplier!", "StrKeyVal")      'Display the Message
                Exit Sub
            End If
            If Trim(txtBrandName.Text) = "" Then                                         'If txtPartTypeName TextField is Null then
                CreateMessageAlert(Me, "Brand Name Cannot be Null !", "StrKeyVal")      'Display the Message
                Exit Sub
            End If
            'If Trim(txtBrandCode.Text) = "" Then                                         'If txtPartTypeCode TextField is Null then
            '    CreateMessageAlert(Me, "Brand Code Cannot be Null !", "StrKeyVal")      'Display the Message 
            '    Exit Sub
            'End If

            If IsAlreadyPresent("select count(*) from Brand_Master where brand_code!='" & Trim(txtBrandCode.Text) & "' and  Brand_Name='" & txtBrandName.Text.Trim & "'  and Brand_isdeleted='0' and Brand_companyId like '%" & RemoveLiterals(ddlSupplier.SelectedItem.Value) & "%'") Then  ' To Check Existance of Engineer Code
                CreateMessageAlert(Me, " Brand Name Already Present !", "StrKeyVal")      'Display the Message
                txtBrandName.Text = "" : txtBrandName.Focus()
                Exit Sub
            End If
            'If Trim(BrandFileUpload.HasFile) Then
            'Else
            '    CreateMessageAlert(Me, "Brand Image Cannot be Null !", "StrKeyVal")      'Display the Message
            '    Exit Sub
            'End If
            '----------------------------------
            'str = UploadBrandImage()                                                'Upload Image
            'Session("Brand_Image") = str
            '---------------------------------------


            ''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 IsAlreadyPresent("select count(*) from Brand_Master where Brand_Code='" & txtBrandCode.Text.Trim & "'and Brand_isdeleted='0' and Brand_companyId='" & ddlSupplier.SelectedItem.Value & "'") Then  ' To Check Existance of Engineer Code
            ''    CreateMessageAlert(Me, " Brand Code Already Present !", "StrKeyVal")      'Display the Message
            ''    txtBrandCode.Text = "" : txtBrandCode.Focus()
            ''    Exit Sub
            ''End If
            ''If IsAlreadyPresent("select count(*) from Brand_Master where Brand_Name='" & txtBrandName.Text.Trim & "'and Brand_isdeleted='0' and Brand_companyId='" & ddlSupplier.SelectedItem.Value & "'") Then  ' To Check Existance of Engineer Code
            ''    CreateMessageAlert(Me, " Brand Name Already Present !", "StrKeyVal")      'Display the Message
            ''    txtBrandName.Text = "" : txtBrandName.Focus()
            ''    Exit Sub
            ''End If

            ''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 = "Brand_Master_Proc"                                        'Assign the Name of Stored Procedure, to Execute
            ''cmd.Connection = con                                                            'Assign the connection
            ''cmd.Parameters.Add(New SqlParameter("@Brand_Kid", SqlDbType.NVarChar, 20)).Value = Trim(RemoveLiterals(txtBrandId.Text))     'Add Parameter Banke_Kid and assign the value  to it
            ''cmd.Parameters.Add(New SqlParameter("@Brand_Code", SqlDbType.NVarChar, 20)).Value = Trim(txtBrandCode.Text)       'Add Parameter Banke_Code and assign the value of txtBrandCode TextField to it
            ''cmd.Parameters.Add(New SqlParameter("@Brand_Name", SqlDbType.NVarChar, 25)).Value = Trim(txtBrandName.Text)       'Add Parameter Bank_Name and assign the value of txtBrandName TextField to it
            ''cmd.Parameters.Add(New SqlParameter("@Brand_Image", SqlDbType.NVarChar, 100)).Value = Trim(strImageName)       'Add Parameter Bank_Image and assign the value of txtBrandImage TextField to it
            ''cmd.Parameters.Add(New SqlParameter("@Brand_FinancialYearID", SqlDbType.NVarChar, 20)).Value = Trim(g_FinYear)       'Add Parameter Company_Code and assign the value of Session("g_Company") to it
            ''cmd.Parameters.Add(New SqlParameter("@Brand_CompanyId", SqlDbType.NVarChar, 20)).Value = Trim(ddlSupplier.SelectedItem.Value)       'Add Parameter Company_Code and assign the value of Session("g_Company") to it
            ''cmd.Parameters.Add(New SqlParameter("@Brand_UserId", SqlDbType.NVarChar, 20)).Value = Trim(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 = "Brand 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


            Dim file_name As String = ""
            If (BrandFileUpload.HasFile) Then
                Dim fileType As String = Path.GetExtension(BrandFileUpload.FileName)
                If fileType = ".gif" Or fileType = ".png" Or fileType = ".jpg" Or fileType = ".jpeg" Or fileType = ".bmp" Then
                    strImageName = Final_FileUpload(BrandFileUpload, img, Server.MapPath("~\Upload Image\Brand_Image\"))
                    lblMsg.Visible = False
                    'strImageName = Server.MapPath("~/Upload Image/Product Image/UploadImages/")
                    'file_name = Fileupload(ProRevwFileUpload, img, strImageName) 'Replace Final_FileUpload() function to Fileuload() by Pankaj mishra
                Else
                    lblMsg.Visible = True
                    lblMsg.Text = "Please Select image File Only!"
                    BrandFileUpload.Focus()
                    Exit Sub
                End If
            End If

            flag = "S"
            InsertBrand()    'To Check & Update Data
            FillGrid("")                                                                      'Fill the Grid
            ClearFields()                                                                   'Clear the TextFields
            UnLockTextBox(False)                                                            'Disable the TextFields
            Call SaveClickVisible(Me, btnADD, BtnSave, BtnEdit, BtnDelete, BtnCancel, BtnSearch)
            flag = ""
            gvBrand.SelectedIndex = -1
        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
            'If CBool(Session("Modify_Flag").ToString) = False Then
            '    CreateMessageAlert(Me, "You Do Not Have Access Right To Modify An Existing Record !", "StrKeyVal")  'Display the Message 
            '    Exit Sub
            'End If
            'If ddlSupplier.SelectedItem.Text = "Select Supplier" Then
            '    CreateMessageAlert(Me, "Select Proper Supplier!", "StrKeyVal")      'Display the Message
            '    Exit Sub
            'End If
            'If Trim(txtBrandCode.Text) = "" Then                                         'If txtPartTypeCode TextField is Null then
            '    CreateMessageAlert(Me, "Brand Code Cannot be Null !", "StrKeyVal")      'Display the Message 
            '    Exit Sub
            'End If
            If Trim(txtBrandName.Text) = "" Then                                         'If txtPartTypeName TextField is Null then
                CreateMessageAlert(Me, "Brand Name Cannot be Null !", "StrKeyVal")      'Display the Message 
                Exit Sub
            End If


            'If IsAlreadyPresent("select count(*) from Brand_Master where brand_code!='" & Trim(txtBrandCode.Text) & "' and  Brand_Name='" & txtBrandName.Text.Trim & "'  and Brand_isdeleted='0' and Brand_companyId like '%" & RemoveLiterals(ddlSupplier.SelectedItem.Value) & "%'") Then  ' To Check Existance of Engineer Code
            '    CreateMessageAlert(Me, " Brand Name Already Present !", "StrKeyVal")      'Display the Message
            '    txtBrandName.Text = "" : txtBrandName.Focus()
            '    Exit Sub
            'End If

            'If BrandImage.ImageUrl = "~\Upload Image\Brand_Image\" Then
            '    CreateMessageAlert(Me, "Brand Image Cannot Be Null..!!", "StrKeyVal")
            '    Exit Sub
            'End If

            'If Trim(BrandFileUpload.HasFile) Then
            'Else

            '    CreateMessageAlert(Me, "Brand Image Cannot be Null !", "StrKeyVal")      'Display the Message
            '    Exit Sub
            'End If
            '----------------------------------
            'str = UploadBrandImage()                                                'Upload Image
            'Session("Brand_Image") = str
            '---------------------------------------

            ''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 = "Brand_Master_Proc"                                        'Assign the Name of Stored Procedure, to Execute
            ''cmd.Connection = con                                                            'Assign the connection
            ''cmd.Parameters.Add(New SqlParameter("@Brand_Kid", SqlDbType.NVarChar, 10)).Value = Trim(RemoveLiterals(txtBrandId.Text))       'Add Parameter Brand_Kid and assign the value of txtBrandId TextField  to it
            ''cmd.Parameters.Add(New SqlParameter("@Brand_Code", SqlDbType.NVarChar, 25)).Value = Trim(txtBrandCode.Text)       'Add Parameter Brand_Code and assign the value of txtBrandCode TextField to it
            ''cmd.Parameters.Add(New SqlParameter("@Brand_Name", SqlDbType.NVarChar, 100)).Value = Trim(txtBrandName.Text)       'Add Parameter Brand_Name and assign the value oftxtBrandName TextField to it
            ''cmd.Parameters.Add(New SqlParameter("@Brand_Image", SqlDbType.NVarChar, 100)).Value = Trim(strImageName)       'Add Parameter Bank_Image and assign the value of txtBrandImage TextField to it
            ''cmd.Parameters.Add(New SqlParameter("@Brand_FinancialYearID", SqlDbType.NVarChar, 20)).Value = Trim(g_FinYear)       'Add Parameter Company_Code and assign the value of Session("g_Company") to it
            ''cmd.Parameters.Add(New SqlParameter("@Brand_CompanyId", SqlDbType.NVarChar, 10)).Value = Trim(ddlSupplier.SelectedItem.Value)       'Add Parameter Company_Code and assign the value of Session("g_Company") to it
            ''cmd.Parameters.Add(New SqlParameter("@Brand_UserId", SqlDbType.NVarChar, 10)).Value = Trim(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 = "Brand 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


            Dim file_name As String = ""
            If (BrandFileUpload.HasFile) Then
                Dim fileType As String = Path.GetExtension(BrandFileUpload.FileName)
                If fileType = ".gif" Or fileType = ".png" Or fileType = ".jpg" Or fileType = ".jpeg" Or fileType = ".bmp" Then
                    strImageName = Final_FileUpload(BrandFileUpload, img, Server.MapPath("~\Upload Image\Brand_Image\"))
                    lblMsg.Visible = False
                    'strImageName = Server.MapPath("~/Upload Image/Product Image/UploadImages/")
                    'file_name = Fileupload(ProRevwFileUpload, img, strImageName) 'Replace Final_FileUpload() function to Fileuload() by Pankaj mishra
                Else
                    lblMsg.Visible = True
                    lblMsg.Text = "Please Select image File Only!"
                    BrandFileUpload.Focus()
                    Exit Sub
                End If
            Else
                strImageName = ReturnValue("Select Brand_Image from Brand_Master where Brand_Kid = '" & RemoveLiterals(txtBrandId.Text.Trim) & "' and Brand_IsDeleted = '0'")
            End If
            flag = "U"
            UpdateBrand()  'To Check & Update Data
            ClearFields()
            FillGrid("")                                                                      'Fill the Grid
            BrandImage.ImageUrl = ""
            'Clear the TextFields
            UnLockTextBox(False)                                                            'Disable the TextFields
            Call EditClickVisible(Me, btnADD, BtnSave, BtnEdit, BtnDelete, BtnCancel, BtnSearch)
            flag = ""
            gvBrand.SelectedIndex = -1
        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 Trim(txtBrandCode.Text) = "" Then                                         'If txtPartTypeCode TextField is Null then
                CreateMessageAlert(Me, "Brand Code Cannot be Null !", "StrKeyVal")      'Display the Message 
                txtBrandCode.Focus()
                Exit Sub
            End If
            If Trim(txtBrandName.Text) = "" Then                                         'If txtPartTypeName TextField is Null then
                CreateMessageAlert(Me, "Brand Name Cannot be Null !", "StrKeyVal")      'Display the Message 
                txtBrandName.Focus()
                Exit Sub
            End If

            'If Trim(BrandImage.ImageUrl) = "~/Upload Image/Brand_Image/" Then                                         'If Inputfile Null then
            '    CreateMessageAlert(Me, "Brand Image Cannot be Null !", "StrKeyVal")      'Display the Message
            '    Exit Sub
            'End If
            'If IsAlreadyPresent("select count(*) from Brand_Master where Brand_Code='" & txtBrandCode.Text.Trim & "'and Brand_isdeleted='0' and Brand_companyId='" & g_Company & "'") Then  ' To Check Existance of Engineer Code
            '    CreateMessageAlert(Me, " Unable to Delete, Referenced to Other Records... !", "StrKeyVal")      'Display the Message
            '    btnCancel_Click(sender, e)
            '    Exit Sub
            'End If
            strImageName = ReturnValue("Select Brand_Image from Brand_Master where Brand_Kid = '" & RemoveLiterals(txtBrandId.Text.Trim) & "' and Brand_IsDeleted = '0'")
            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 = "Brand_Master_Proc"                                        'Assign the Name of Stored Procedure, to Execute
            cmd.Connection = con                                                            'Assign the connection

            cmd.Parameters.Add(New SqlParameter("@Brand_Kid", SqlDbType.NVarChar, 20)).Value = Trim(RemoveLiterals(txtBrandId.Text))       'Add Parameter Brand_Kid and assign the value of  txtBrandId TextField  to it
            cmd.Parameters.Add(New SqlParameter("@Brand_Code", SqlDbType.NVarChar, 20)).Value = Trim(txtBrandCode.Text)       'Add Parameter Brand_Code and assign the value of txtBrandCode TextField to it
            cmd.Parameters.Add(New SqlParameter("@Brand_Name", SqlDbType.NVarChar, 100)).Value = Trim(txtBrandName.Text)       'Add Parameter Brand_Name and assign the value of txtBrandName TextField to it
            cmd.Parameters.Add(New SqlParameter("@Brand_Image", SqlDbType.NVarChar, 100)).Value = Trim(strImageName)       'Add Parameter Brand_Image and assign the value of txtBrandImage TextField to it
            cmd.Parameters.Add(New SqlParameter("@Brand_FinancialYearID", SqlDbType.NVarChar, 20)).Value = Trim(g_FinYear)       'Add Parameter Company_Code and assign the value of Session("g_Company") to it
            cmd.Parameters.Add(New SqlParameter("@Brand_CompanyId", SqlDbType.NVarChar, 500)).Value = Trim(ddlSupplier.SelectedItem.Value)       'Add Parameter Company_Code and assign the value of Session("g_Company") to it
            cmd.Parameters.Add(New SqlParameter("@Brand_UserId", SqlDbType.NVarChar, 20)).Value = Trim(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 = "Brand 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
            BrandImage.ImageUrl = ""
            CreateMessageAlert(Me, "Record Deleted Successfully...", "StrKeyVal")             'Display the Message
            ClearFields()                                                                   'Clear the TextFields
            UnLockTextBox(False)                                                            'Disable the TextFields
            Call DeleteClickVisible(Me, btnADD, BtnSave, BtnEdit, BtnDelete, BtnCancel, BtnSearch)
            gvBrand.SelectedIndex = -1
        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
            Search = False
            txtBrandName.AutoPostBack = False
            Call FillGrid("")
            Call CancelClickVisible(Me, btnADD, BtnSave, BtnEdit, BtnDelete, BtnCancel, BtnSearch)
            gvBrand.SelectedIndex = -1
        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 SearchClickVisible(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()                             'Set Focus to txtPartTypeName TextField
    End Sub
    Protected Sub txtBrandName_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtBrandName.TextChanged
        'If Search = True Then  
        'flag = True
        'If Search Mode is true then
        Call FillGrid(Trim(txtBrandName.Text))  'Fill the Grid filtering on txtStockLocationName 
        'Call FillGrid(Trim(txtBrandImage.Text)) 'Fill the Grid filtering on txtStockLocationName  
        ' End If
        If BtnSave.Enabled Then BtnSave.Focus()
    End Sub
    Protected Sub GenerateBrandCode()
        Try
            Dim i As String
            Dim qry As String
            Dim code As String
            txtBrandId.Text = NewPrimaryKey(Me)
            i = "1"
            i = Format(Val(i), "000")
            While True
                code = "BND" & "/" & Year(Date.Today) & "/" & Format(Month(Date.Today), "00") & "/" & Day(Date.Today) & "/" & i
                qry = "select count(*) from Brand_Master where Brand_Code='" & code & "'"
                If IsAlreadyPresent(qry) Then
                    i = Val(i) + 1
                    i = Format(Val(i), "000")
                Else
                    txtBrandCode.Text = code
                    Exit Sub
                End If
            End While
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message, "StrKeyVal")     'Display the Error Message
        End Try
    End Sub
    'Private Function UploadBrandImage() As String
    '    Dim strFileName2 As String
    '    Dim strFileName As String
    '    Dim strr As String
    '    strFileName = Mid(File1.PostedFile.FileName.ToString, File1.PostedFile.FileName.ToString.LastIndexOf("\") + 2, Len(File1.PostedFile.FileName.ToString))
    '    strr = strFileName.Substring(strFileName.LastIndexOf(".") + 1)
    '    If String.Compare(strr, "jpg", False) = 0 Then
    '        strPath = Server.MapPath("~\Upload Image\Brand_Image\")
    '        '-----Calling a function from the class file-------------
    '        strFileName2 = IsUploadImageAlreadyPersent.FileExists(strFileName, strPath)
    '        If strFileName2 <> "" Then
    '            File1.PostedFile.SaveAs(Server.MapPath("~\Upload Image\Brand_Image\") & strFileName2)
    '            UploadBrandImage = strFileName2
    '        End If
    '    End If
    'End Function
    Protected Sub gvBrand_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs)
        gvBrand.PageIndex = e.NewPageIndex
        FillGrid("")
        gvBrand.SelectedIndex = -1
        'ClearFields()
        Call CancelClickVisible(Me, btnADD, BtnSave, BtnEdit, BtnDelete, BtnCancel, BtnSearch)

    End Sub
    Protected Sub gvBrand_PageIndexChanging1(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs)
        gvBrand.PageIndex = e.NewPageIndex
        FillGrid("")
        gvBrand.SelectedIndex = -1
        'ClearFields()
        Call CancelClickVisible(Me, btnADD, BtnSave, BtnEdit, BtnDelete, BtnCancel, BtnSearch)
    End Sub
    'Protected Sub ddlSupplier_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlSupplier.SelectedIndexChanged

    '    Try
    '        Dim qry As String
    '        qry = "SELECT     Brand_Kid, Brand_Code AS Code, Brand_Name AS Brand, Brand_Image AS Image, Brand_CompanyId   FROM Brand_Master WHERE   Brand_IsDeleted = '0' and Brand_CompanyId like '%" & RemoveLiterals(ddlSupplier.SelectedItem.Value) & "%' Order by Brand_Name"
    '        Dim dt As New DataTable
    '        dt = ReturnDataTable(qry)
    '        gvBrand.Columns(1).Visible = True
    '        gvBrand.Columns(2).Visible = True
    '        gvBrand.Columns(5).Visible = True
    '        gvBrand.Columns(6).Visible = True
    '        gvBrand.DataSource = dt                          'Set the DataReader Variable as DataSource for the Grid
    '        gvBrand.DataBind()                               'Bind the Data to the Grid
    '        gvBrand.Columns(1).Visible = False
    '        gvBrand.Columns(2).Visible = False
    '        gvBrand.Columns(5).Visible = False
    '        gvBrand.Columns(6).Visible = False
    '    Catch ex As Exception
    '        CreateMessageAlert(Me, ex.Message, "StrKeyVal")     'Display the Error Message
    '    End Try
    'End Sub


    'Protected Sub CheckBrandName()
    '    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 present As DataTable
    '        Dim dt_brand As DataTable
    '        present = ReturnDataTable("select Brand_Kid,Brand_Code,Brand_Image,Brand_CompanyId from Brand_Master where Brand_companyId like '%" & RemoveLiterals(ddlSupplier.SelectedItem.Value) & "%' and Brand_Name='" & txtBrandName.Text.Trim & "' and Brand_IsDeleted='0'")
    '        If present.Rows.Count <> 0 Then

    '            If present.Rows(0).Item("Brand_CompanyId").ToString.Contains(RemoveLiterals(ddlSupplier.SelectedItem.Value)) Then
    '                CreateMessageAlert(Me, "Brand is Already Added", "strkeyVal")
    '                txtBrandName.Text = ""
    '                Exit Sub
    '            End If

    '            ''Dim res As Integer = MsgBox("Brand Name Already Added by Other Supplier. Still You want to Add ?", MsgBoxStyle.YesNo, "Brand Name")
    '            ''If res = MsgBoxResult.Yes Then
    '            ''    InsertUpdateBrandName("Update", present.Rows(0).Item("Brand_Kid").ToString, present.Rows(0).Item("Brand_CompanyID").ToString & "-" & RemoveLiterals(ddlSupplier.SelectedItem.Value), txtBrandCode.Text.Trim, strImageName)
    '            ''End If
    '        Else
    '            dt_brand = ReturnDataTable("select Brand_Kid,Brand_Code,Brand_Image,Brand_CompanyId from Brand_Master where Brand_Name='" & txtBrandName.Text.Trim & "' and Brand_IsDeleted='0'")
    '            If dt_brand.Rows.Count = 0 Then
    '                '' Call GenerateBrandCode()
    '                Dim brand_image As String = ""

    '                If flag = "S" Then
    '                    InsertUpdateBrandName("Insert", txtBrandId.Text.Trim, RemoveLiterals(ddlSupplier.SelectedItem.Value), txtBrandCode.Text.Trim, strImageName)
    '                ElseIf flag = "U" Then

    '                    Dim s_nm As String = ReturnValue("Select Brand_CompanyId from Brand_Master where Brand_CompanyId like '%" & RemoveLiterals(ddlSupplier.SelectedItem.Value) & "%' AND Brand_IsDeleted='0' And Brand_Kid='" & RemoveLiterals(txtBrandId.Text.Trim) & "'")
    '                    If s_nm <> "" Then
    '                        CreateMessageAlert(Me, "This Supplier Already Added", "strkeyVal")
    '                        InsertUpdateBrandName("Update", txtBrandId.Text, s_nm.ToString, txtBrandCode.Text.Trim, strImageName)
    '                    Else
    '                        Dim sid As String = ReturnValue("Select Brand_CompanyId from Brand_Master where Brand_Kid='" & RemoveLiterals(txtBrandId.Text) & "' and Brand_IsDeleted='0'")

    '                        If ddlSupplier.SelectedItem.Value = "Select" Then
    '                            InsertUpdateBrandName("Update", txtBrandId.Text, sid, txtBrandCode.Text.Trim, strImageName)
    '                            Exit Sub
    '                        End If

    '                        ' Dim r As Integer = MsgBox("Are You Sure Want to Add this Supplier ?", MsgBoxStyle.YesNo, "Supplier Name")
    '                        If ConfirmCheck() = True Then
    '                            InsertUpdateBrandName("Update", txtBrandId.Text, sid & "-" & RemoveLiterals(ddlSupplier.SelectedItem.Value), txtBrandCode.Text.Trim, strImageName)
    '                        Else
    '                            InsertUpdateBrandName("Update", txtBrandId.Text, sid, txtBrandCode.Text.Trim, strImageName)
    '                        End If
    '                        'If r = MsgBoxResult.Yes Then
    '                        '    InsertUpdateBrandName("Update", txtBrandId.Text, sid & "-" & RemoveLiterals(ddlSupplier.SelectedItem.Value), txtBrandCode.Text.Trim, strImageName)
    '                        'Else
    '                        '    InsertUpdateBrandName("Update", txtBrandId.Text, sid, txtBrandCode.Text.Trim, strImageName)
    '                        'End If


    '                    End If

    '                End If

    '            Else
    '                If dt_brand.Rows(0).Item("Brand_CompanyId").ToString.Contains(ddlSupplier.SelectedItem.Value) Then
    '                    CreateMessageAlert(Me, "Brand is Already Added", "strkeyVal")
    '                    txtBrandName.Text = ""
    '                    Exit Sub
    '                End If

    '                If dt_brand.Rows(0).Item(0).ToString = txtBrandId.Text Then

    '                    If ddlSupplier.SelectedItem.Value <> "Select" Then
    '                        Dim res As Integer = MsgBox("Brand Name Already Added by Other Supplier. Still You want to Add ?", MsgBoxStyle.YesNo, "Brand Name")
    '                        If res = MsgBoxResult.Yes Then
    '                            InsertUpdateBrandName("Update", dt_brand.Rows(0).Item("Brand_Kid").ToString, dt_brand.Rows(0).Item("Brand_CompanyID").ToString & "-" & RemoveLiterals(ddlSupplier.SelectedItem.Value), txtBrandCode.Text.Trim, strImageName)
    '                        End If
    '                    Else
    '                        InsertUpdateBrandName("Update", dt_brand.Rows(0).Item("Brand_Kid").ToString, dt_brand.Rows(0).Item("Brand_CompanyID").ToString, txtBrandCode.Text.Trim, strImageName)
    '                    End If


    '                Else
    '                    Dim br_id As String = ReturnValue("Select Brand_Kid from Brand_Master where Brand_Name = '" & txtBrandName.Text.Trim & "' and Brand_IsDeleted='0'")
    '                    If RemoveLiterals(br_id) <> "" Then

    '                        If flag = "S" Then
    '                            Dim res As Integer = MsgBox("Brand Name Already Added by Other Supplier. Still You want to Add ?", MsgBoxStyle.YesNo, "Brand Name")
    '                            If res = MsgBoxResult.Yes Then
    '                                InsertUpdateBrandName("Update", dt_brand.Rows(0).Item("Brand_Kid").ToString, dt_brand.Rows(0).Item("Brand_CompanyID").ToString & "-" & RemoveLiterals(ddlSupplier.SelectedItem.Value), txtBrandCode.Text.Trim, strImageName)
    '                            End If
    '                        ElseIf flag = "U" Then
    '                            CreateMessageAlert(Me, "Brand Name Can Not be Same.", "strkeyVal")
    '                            txtBrandName.Text = ""
    '                            Exit Sub
    '                        End If
    '                    Else
    '                        CreateMessageAlert(Me, "Brand Name Can Not be Same.", "strkeyVal")
    '                        txtBrandName.Text = ""
    '                        Exit Sub

    '                    End If
    '                End If
    '            End If
    '        End If
    '    Catch ex As Exception
    '        CreateMessageAlert(Me, ex.Message.ToString, "StrKeyVal")
    '    End Try
    'End Sub
    'Private Function ConfirmCheck() As Boolean


    '    Dim ProdApp As String
    '    ProdApp = ReturnValue("Select Brand_CompanyId from Brand_Master where Brand_Kid='" & RemoveLiterals(txtBrandId.Text) & "' and Brand_IsDeleted='0'")
    '    If ProdApp.ToString = "" Then
    '        Return False
    '    Else
    '        Return True
    '    End If
    'End Function
    'Private Function CheckOthers() As Boolean


    '    Dim ProdApp As String
    '    ProdApp = ReturnValue("Select Brand_CompanyId from Brand_Master where Brand_Kid='" & RemoveLiterals(txtBrandId.Text) & "' and Brand_IsDeleted='0'")
    '    If ProdApp.ToString = "" Then
    '        txtOther.Text = "False"
    '        'lblOtherError.Text = "<img alt='' src='" & Page.ResolveUrl("~/Images/javascripts.gif") & "'  />" + "&nbsp" + "Product Application" + "&nbsp" + "<br />"
    '    End If
    '    If txtOther.Text = "False" Then
    '        Return False
    '    Else
    '        Return True
    '    End If
    'End Function
    Protected Sub InsertUpdateBrandName(ByVal mode As String, ByVal kid As String, ByVal supid As String, ByVal bcode As String, ByVal b_img As String)

        Try

            Dim cmd As New Data.SqlClient.SqlCommand()
            Dim sqlcoll As Data.SqlClient.SqlParameterCollection = cmd.Parameters
            sqlcoll.Clear()

            If mode = "Insert" Then

                sqlcoll.Add("@Brand_Kid", Data.SqlDbType.NVarChar, 20).Value = RemoveLiterals(kid)
                sqlcoll.Add("@Brand_Code", Data.SqlDbType.NVarChar, 25).Value = bcode
                sqlcoll.Add("@Brand_Name", Data.SqlDbType.NVarChar, 100).Value = txtBrandName.Text.Trim
                sqlcoll.Add("@Brand_Image", Data.SqlDbType.NVarChar, 100).Value = b_img
                sqlcoll.Add("@Brand_FinancialYearID", Data.SqlDbType.NVarChar, 20).Value = Session("g_FinYear")
                sqlcoll.Add("@Brand_CompanyId", Data.SqlDbType.NVarChar).Value = RemoveLiterals(ddlSupplier.SelectedItem.Value)
                sqlcoll.Add("@Brand_UserId", Data.SqlDbType.NVarChar, 20).Value = Session("g_User")
                sqlcoll.Add("@FormName", Data.SqlDbType.NVarChar, 50).Value = "BrandMaster"
                sqlcoll.Add("@Mode", Data.SqlDbType.Char, 50).Value = "Insert"
                ExecQuery("Brand_Master_Proc", sqlcoll)
                CreateMessageAlert(Me, "Record Saved Successfully...", "StrKeyVal")             'Display the Message
            Else
                sqlcoll.Add("@Brand_Kid", Data.SqlDbType.NVarChar, 20).Value = RemoveLiterals(kid)
                sqlcoll.Add("@Brand_Code", Data.SqlDbType.NVarChar, 25).Value = bcode
                sqlcoll.Add("@Brand_Name", Data.SqlDbType.NVarChar, 100).Value = txtBrandName.Text.Trim
                sqlcoll.Add("@Brand_Image", Data.SqlDbType.NVarChar, 100).Value = b_img
                sqlcoll.Add("@Brand_FinancialYearID", Data.SqlDbType.NVarChar, 20).Value = Session("g_FinYear")
                sqlcoll.Add("@Brand_CompanyId", Data.SqlDbType.NVarChar, 500).Value = supid
                sqlcoll.Add("@Brand_UserId", Data.SqlDbType.NVarChar, 20).Value = Session("g_User")
                sqlcoll.Add("@FormName", Data.SqlDbType.NVarChar, 50).Value = "BrandMaster"
                sqlcoll.Add("@Mode", Data.SqlDbType.Char, 50).Value = "Update"
                ExecQuery("Brand_Master_Proc", sqlcoll)
                CreateMessageAlert(Me, "Record Modified Successfully...", "StrKeyVal")
            End If
        Catch ex As Exception
            CreateMessageAlert(Me, ex.Message.ToString, "StrKeyVal")
        End Try
    End Sub
    <Ajax.AjaxMethod()> _
    Public Function CheckExistingSupplier(ByVal btext As String, ByVal supid As String) As Boolean
        If IsAlreadyPresent("Select count(*) from Brand_Master where Brand_Name='" & btext & "'and Brand_CompanyId like '%" & supid & "%' and Brand_IsDeleted='0'") Then
            Return False
        End If
        Return True
    End Function
    <Ajax.AjaxMethod()> _
    Public Function CheckExistingBrand(ByVal btext As String, ByVal supid As String) As Boolean
        If IsAlreadyPresent("Select count(*) from Brand_Master where Brand_Name='" & btext & "' and Brand_IsDeleted='0'") Then
            Return False
        End If
        Return True
    End Function
    <Ajax.AjaxMethod()> _
   Public Function CheckExistingBrandUpdate(ByVal bkid As String) As Boolean
        If IsAlreadyPresent("Select count(*) from Brand_Master where Brand_Kid='" & RemoveLiterals(bkid.Trim) & "' and Brand_IsDeleted='0'") Then
            Return False
        End If
        Return True
    End Function
    <Ajax.AjaxMethod()> _
       Public Function CheckExistingName(ByVal txtname As String, ByVal txtkid As String) As Boolean
        Dim nm As String = ReturnValue("Select Brand_Kid from Brand_Master where Brand_Name='" & txtname.Trim & "' and Brand_IsDeleted='0' and Brand_Kid !='" & RemoveLiterals(txtkid.Trim) & "'")
        If nm = "" Then
            Return True
        Else
            Return False
        End If
    End Function
    <Ajax.AjaxMethod()> _
        Public Function CheckNotExistingSupplier(ByVal ddl As String, ByVal txtkid As String) As Boolean
        Dim nm As String = ReturnValue("Select Brand_CompanyId from Brand_Master where Brand_Kid='" & RemoveLiterals(txtkid) & "' and Brand_IsDeleted='0'")
        If nm.Contains(RemoveLiterals(ddl.ToString.Trim)) Then
            Return False
        Else
            Return True
        End If
        Return False
    End Function
    Protected Sub InsertBrand()
        Dim s_nm As DataTable = ReturnDataTable("Select Brand_Kid,Brand_CompanyId,Brand_Code,Brand_Image from Brand_Master where Brand_Name='" & txtBrandName.Text.Trim & "' and Brand_IsDeleted='0'")

        If s_nm.Rows.Count <> 0 Then
            If strImageName = "" Then
                InsertUpdateBrandName("Update", s_nm.Rows(0).Item("Brand_Kid").ToString, s_nm.Rows(0).Item("Brand_CompanyId").ToString & "-" & RemoveLiterals(ddlSupplier.SelectedItem.Value), s_nm.Rows(0).Item("Brand_Code").ToString, s_nm.Rows(0).Item("Brand_Image").ToString)
            Else
                InsertUpdateBrandName("Update", s_nm.Rows(0).Item("Brand_Kid").ToString, s_nm.Rows(0).Item("Brand_CompanyId").ToString & "-" & RemoveLiterals(ddlSupplier.SelectedItem.Value), s_nm.Rows(0).Item("Brand_Code").ToString, strImageName)
            End If
        Else
            InsertUpdateBrandName("Insert", txtBrandId.Text.Trim, RemoveLiterals(ddlSupplier.SelectedItem.Value), txtBrandCode.Text.Trim, strImageName)
        End If
    End Sub
    Protected Sub UpdateBrand()
        Dim s_nm As DataTable = ReturnDataTable("Select Brand_Kid,Brand_CompanyId,Brand_Code,Brand_Image from Brand_Master where Brand_Kid='" & RemoveLiterals(txtBrandId.Text.Trim) & "' and Brand_IsDeleted='0'")
        If s_nm.Rows.Count <> 0 Then
            If ddlSupplier.SelectedItem.Value = "Select" Then
                InsertUpdateBrandName("Update", s_nm.Rows(0).Item("Brand_Kid").ToString, s_nm.Rows(0).Item("Brand_CompanyId").ToString, s_nm.Rows(0).Item("Brand_Code").ToString, strImageName)
                Exit Sub
            Else

                If s_nm.Rows(0).Item("Brand_CompanyId").ToString.Contains(RemoveLiterals(ddlSupplier.SelectedItem.Value)) Then
                    InsertUpdateBrandName("Update", s_nm.Rows(0).Item("Brand_Kid").ToString, s_nm.Rows(0).Item("Brand_CompanyId").ToString, s_nm.Rows(0).Item("Brand_Code").ToString, strImageName)
                Else
                    InsertUpdateBrandName("Update", s_nm.Rows(0).Item("Brand_Kid").ToString, s_nm.Rows(0).Item("Brand_CompanyId").ToString & "-" & RemoveLiterals(ddlSupplier.SelectedItem.Value), s_nm.Rows(0).Item("Brand_Code").ToString, strImageName)
                End If

                Exit Sub
            End If

        End If
    End Sub
End Class