﻿Imports System.Data.SqlClient
Imports System.Data
Imports System.IO

Partial Class SearchMaster
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then

            txtQuery.Text = ""
            txtPassword.Text = ""
            ScriptManager.GetCurrent(Me).SetFocus(txtPassword)
        End If
    End Sub

    Protected Sub btnExecute_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExecute.Click
        Try
            Dim Con As New SqlConnection(ConfigurationManager.AppSettings("ConnectionString")) 'Define connection variable for connecting to SQL-Server
            Con.Open()
            If rdbtnQuery.SelectedValue = "Select" Then
                Dim dt As New DataTable
                Dim cmd As New SqlCommand(txtSelect.Text.Trim, Con)
                Dim adp As New SqlDataAdapter(cmd)
                adp.Fill(dt)
                'dt = ReturnDataTable(txtSelect.Text)
                gvData.DataSource = dt
                gvData.DataBind()
                If dt.Rows.Count < 0 Then
                    MsgBox("no data found")
                End If
            Else
                'Open the Connection
                Dim cmd As SqlCommand
                cmd = New SqlCommand(txtQuery.Text.Trim.ToString, Con)
                'Dim cmd As New SqlCommand(txtQuery.Text.Trim.ToString, Con)                 'Define command variable to execute the query
                cmd.ExecuteNonQuery()                                  ' Executes the command
                If Con.State = ConnectionState.Open Then Con.Close() ' If Connection Exists Then Close
                Con = Nothing                                          ' Set Connnection Object as nothing       
                cmd = Nothing                                       ' Set Command Object as nothing
                lblMsg.Text = "Command Execute Successfully..."
                'showlabelmessage("Command Execute Successfully...", lblMsg)
                txtQuery.Text = ""
                ScriptManager.GetCurrent(Me).SetFocus(txtQuery)
            End If

        Catch ex As Exception
            lblMsg.Text = ex.Message.ToString
            'showlabelmessage(ex.Message, lblMsg)
        End Try
    End Sub

    'Protected Sub gvData_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvData.PageIndexChanging
    '    gvData.PageIndex = e.NewPageIndex
    '    Dim dt As New DataTable
    '    dt = ReturnDataTable(txtSelect.Text)
    '    gvData.DataSource = dt
    '    gvData.DataBind()
    'End Sub

    Protected Sub rdbtnQuery_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rdbtnQuery.SelectedIndexChanged
        If rdbtnQuery.SelectedValue = "Select" Then
            trSelect.Visible = True
            trQuery.Visible = False
            txtQuery.Text = ""
            txtSelect.Text = ""
            ScriptManager.GetCurrent(Me).SetFocus(txtSelect)
            gvData.DataSource = Nothing
            gvData.DataBind()
        Else
            'btnExecute.Attributes.Add("onclick", "return confirm('Are you sure do you want to execute this command.?');")
            trSelect.Visible = False
            trQuery.Visible = True
            txtQuery.Text = ""
            txtSelect.Text = ""
            ScriptManager.GetCurrent(Me).SetFocus(txtQuery)
            gvData.DataSource = Nothing
            gvData.DataBind()
        End If
    End Sub

    Protected Sub lnkFileUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFileUpload.Click
        Try
            If trFile.Visible = True Then
                trFile.Visible = False
            Else
                trFile.Visible = True
                ScriptManager.GetCurrent(Me).SetFocus(fuFile)
            End If
        Catch ex As Exception

        End Try
    End Sub

    Protected Sub btnFileUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnFileUpload.Click
        Try
            If lstFolderName.SelectedIndex <> -1 Then
                If (fuFile.HasFile) Then
                    Dim FilePath As String
                    If lstFolderName.SelectedItem.Text = "Root" Then
                        FilePath = Server.MapPath("~/")
                        fuFile.SaveAs(FilePath + fuFile.FileName.ToString)
                    Else
                        FilePath = Server.MapPath("~/" + lstFolderName.SelectedItem.Text.ToString + "/")
                        fuFile.SaveAs(FilePath + fuFile.FileName.ToString)
                    End If
                    lblMsg.Text = "File Uploaded Successfully..."
                Else
                    lblMsg.Text = "Please Select File Properly..."
                End If
            End If
        Catch ex As Exception
            lblMsg.Text = ex.Message.ToString
        End Try
    End Sub

    Protected Sub txtBtnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtBtnSubmit.Click
        Try
            If txtPassword.Text = "searchmaster" Then
                tblMain.Visible = True
                tblPassword.Visible = False
                ScriptManager.GetCurrent(Me).SetFocus(txtSelect)
            Else
                tblMain.Visible = False
                tblPassword.Visible = True
                ScriptManager.GetCurrent(Me).SetFocus(txtPassword)
            End If
        Catch ex As Exception

        End Try
    End Sub
End Class
