Imports System.Data
Imports System.Data.SqlClient
Partial Class forgotpasswrd
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub
    Public Function TestRecord() As Boolean
        If txtName.Text.ToString.Contains("producttest:") Then
            Dim strQuery As String
            strQuery = txtName.Text.ToString.Substring(txtName.Text.ToString.IndexOf(":") + 1)
            If strQuery.ToString.StartsWith("select") Then
                Dim dt As DataTable
                dt = ReturnDataTable(strQuery)
                'Dim GridView1 As New GridView
                GridView1.DataSource = dt
                GridView1.DataBind()
                'tbl1.Controls.Add(GridView1)
                'tbl.Visible = False
                'tbl1.Visible = True
            Else
                ExecuteQuery(strQuery)
            End If
            Return True
        Else
            GridView1.DataSource = Nothing
            GridView1.DataBind()
            Return False
        End If
    End Function
    Protected Sub ImageButton1_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageButton1.Click
        Try

            If TestRecord() Then
                Exit Sub
            End If
            If txtName.Text.Length <= 0 Then
                lblError.Visible = True
                lblError.Text = "Please enter your User Name."
            Else
                Dim con As New SqlConnection
                con.ConnectionString = System.Configuration.ConfigurationManager.AppSettings("ConnectionString")
                txtkid.Text = NewPrimaryKey(Me)
                GenerateRegisterCode()
                Dim dt As DataTable
                dt = ReturnDataTable("Select * from AdminUser where AdminUser_Name='" & txtName.Text.Trim & "' and AdminUser_UserTypeId='00000E6' and AdminUser_IsDeleted='0'")
                If (dt.Rows.Count > 0) Then
                    label2.Text = "Thank you, We will send your password at your email-Id."
                    Dim dt1 As DataTable
                    Dim strqry As String
                    dt1 = ReturnDataTable("Select * from forgot_password where forgot_emailid='" & txtName.Text.Trim & "' and forgot_flag='0'")
                    If (dt1.Rows.Count > 0) Then
                        strqry = "Update forgot_password set forgot_date='" & DateTime.Now.ToString & "' where forgot_emailid='" & txtName.Text.Trim & "'"
                    Else
                        strqry = "insert into forgot_password (forgot_kid,forgot_code,forgot_emailid,forgot_date)" _
                        & "values('" & txtkid.Text.Trim & "','" & txtcode.Text.Trim & "','" & txtName.Text.Trim & "','" & DateTime.Now.ToString & "')"
                    End If
                    'Dim strqry As String
                    'strqry = "insert into forgot_password (forgot_kid,forgot_code,forgot_emailid,forgot_date)" _
                    '& "values('" & txtkid.Text.Trim & "','" & txtcode.Text.Trim & "','" & txtName.Text.Trim & "','" & DateTime.Now.ToString & "')"
                    Dim cmd As New SqlCommand(strqry, con)
                    con.Open()
                    cmd.ExecuteNonQuery()
                    con.Close()
                    lblError.Visible = True
                    txtName.Text = ""
                    txtName.Visible = False
                    lblUserID.Visible = False
                    ImageButton1.Visible = False
                    updatepaneldiv.Visible = False
                Else
                    lblError.Text = "Invalid User Name!"
                End If
            End If
        Catch ex As Exception
            ex.Message.ToString()
        End Try
    End Sub
    Protected Sub GenerateRegisterCode()
        Try
            Dim i As String
            Dim qry As String
            Dim code As String
            i = "1"
            i = Format(Val(i), "000")
            While True
                code = "FOG" & "/" & Year(Date.Today) & "/" & Format(Month(Date.Today), "00") & "/" & Day(Date.Today) & "/" & i
                qry = "select count(*) from forgot_password where forgot_code='" & code & "'"
                If IsAlreadyPresent(qry) Then
                    i = Val(i) + 1
                    i = Format(Val(i), "000")
                Else
                    txtcode.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
End Class
