Public Class Players
    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 Icards1 As System.Web.UI.WebControls.Image
    Protected WithEvents Lbtitle As System.Web.UI.WebControls.Label
    Const strConnstring As String = _
   "CONNECTION STRING GOES HERE"
    Protected WithEvents Dlplayer As System.Web.UI.WebControls.DataList
    Protected WithEvents dgplayer_games As System.Web.UI.WebControls.DataGrid
    Public DSplayer As DataSet = New DataSet
    Public DSgames_played As DataSet = New DataSet
    Public DSgame_batting_stats As DataSet = New DataSet
    Public DSgame_bowling_stats As DataSet = New DataSet
    Dim player_id As Integer
    Public DSstats As DataSet = New DataSet
    Protected WithEvents dlmenu As System.Web.UI.WebControls.DataList
    Public dsposition As DataSet = New DataSet
    Protected WithEvents Pimage As System.Web.UI.WebControls.Panel
    Public dsmenu As DataSet = New DataSet
    Protected WithEvents Dg_bowling_stats As System.Web.UI.WebControls.DataGrid
    Protected WithEvents Dg_Batting_stats As System.Web.UI.WebControls.DataGrid
    Protected WithEvents Lbbatting As System.Web.UI.WebControls.Label
    Protected WithEvents Ipic3 As System.Web.UI.WebControls.Image
    Protected WithEvents Ipic2 As System.Web.UI.WebControls.Image
    Protected WithEvents Panel1 As System.Web.UI.WebControls.Panel
    Protected WithEvents Panel2 As System.Web.UI.WebControls.Panel
    Protected WithEvents Panel As System.Web.UI.WebControls.Panel
    Protected WithEvents Panel4 As System.Web.UI.WebControls.Panel
    Protected WithEvents Dg_game_batting_stats As System.Web.UI.WebControls.DataGrid
    Protected WithEvents Panel3 As System.Web.UI.WebControls.Panel
    Protected WithEvents Dg_game_bowling_stats As System.Web.UI.WebControls.DataGrid
    Protected WithEvents ipic7 As System.Web.UI.WebControls.Image
    Protected WithEvents ipic8 As System.Web.UI.WebControls.Image
    Protected WithEvents Ipic As System.Web.UI.WebControls.Image
    Protected WithEvents Ipic4 As System.Web.UI.WebControls.Image
    Protected WithEvents Panel5 As System.Web.UI.WebControls.Panel

    '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

    

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        If Not Page.IsPostBack Then
            viewstate("player_Id") = player_id
            Binddatalist("player_id", "Select * from view_player", DSplayer, Dlplayer)
            Binddatalist("menu_id", "exec sp_menu 1", dsmenu, dlmenu)
            'dlmenu.SelectedIndex = dsmenu.Tables(0).Compute("max(selectedindex)", "menu_id = 3")
            'dsmenu.Reset()
            'Binddatalist("menu_id", "exec sp_menu 1", dsmenu, dlmenu)


        Else

        End If

    End Sub
    Sub Binddata(ByVal sortfieldname As String, ByVal select_query As String, ByVal ds As DataSet, ByVal datagrid As DataGrid)

        '1. Create a connection
        Dim objconn As New Data.SqlClient.SqlConnection(strConnstring)

        '2 Create a command object for the query
        Dim strSQL As String = select_query 'Order by " & sortfieldname

        '3 Create a new SQL command
        Dim objcmd As New Data.SqlClient.SqlCommand(strSQL, objconn)

        '4 Create the Data Adapter
        Dim objDA As New Data.SqlClient.SqlDataAdapter
        objDA.SelectCommand = objcmd

        '5 Populate the dataset 
        objDA.Fill(ds)

        '6 Assign each select query to a table within the dataset
        'DSgame.Tables(0).TableName = "view_game"

        '7 Close the connection
        objconn.Close()

        '7 Finally specifiy the datasource and call Databind()
        datagrid.DataSource = ds
        datagrid.DataKeyField = sortfieldname
        datagrid.DataBind()

    End Sub

    Sub Binddatalist(ByVal sortfieldname As String, ByVal select_query As String, ByVal ds As DataSet, ByVal datalist As DataList)

        '1. Create a connection
        Dim objconn As New Data.SqlClient.SqlConnection(strConnstring)

        '2 Create a command object for the query
        Dim strSQL As String = select_query 'Order by " & sortfieldname

        '3 Create a new SQL command
        Dim objcmd As New Data.SqlClient.SqlCommand(strSQL, objconn)

        '4 Create the Data Adapter
        Dim objDA As New Data.SqlClient.SqlDataAdapter
        objDA.SelectCommand = objcmd

        '5 Populate the dataset 
        objDA.Fill(ds)

        '6 Assign each select query to a table within the dataset
        'DSgame.Tables(0).TableName = "view_game"

        '7 Close the connection
        objconn.Close()

        '7 Finally specifiy the datasource and call Databind()
        datalist.DataSource = ds
        datalist.DataKeyField = sortfieldname
        datalist.DataBind()

    End Sub

    Sub Binddata2(ByVal sortfieldname As String, ByVal select_query As String, ByVal ds As DataSet)

        '1. Create a connection
        Dim objconn As New Data.SqlClient.SqlConnection(strConnstring)

        '2 Create a command object for the query
        Dim strSQL As String = select_query 'Order by " & sortfieldname

        '3 Create a new SQL command
        Dim objcmd As New Data.SqlClient.SqlCommand(strSQL, objconn)

        '4 Create the Data Adapter
        Dim objDA As New Data.SqlClient.SqlDataAdapter
        objDA.SelectCommand = objcmd

        '5 Populate the dataset 
        objDA.Fill(ds)

        '6 Assign each select query to a table within the dataset
        'DSgame.Tables(0).TableName = "view_game"

        '7 Close the connection
        objconn.Close()

    End Sub
    Private Function totals()
        'Dgplayer_stats.Columns(1).FooterText = "Totals"
        'Dgplayer_stats.Columns(6).FooterText = DSstats.Tables(0).Compute("Sum(Games_played)", "")
        'Dgplayer_stats.Columns(7).FooterText = Format(DSstats.Tables(0).Compute("Sum(Total_Winnings)", ""), "£0.00")
    End Function

    Private Sub Dlplayer_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles Dlplayer.ItemCommand

        player_id = viewstate("player_id")
        Dlplayer.SelectedIndex = e.Item.ItemIndex
        Dim player_IDvalue As Label = e.Item.FindControl("lbdlplayer_id")
        player_id = player_IDvalue.Text
        Viewstate("player_Id") = player_id
        Binddata("player_id", "exec sp_batting_stats " & player_id, DSgames_played, Dg_Batting_stats)
        Binddata("player_id", "exec sp_bowling_stats " & player_id, DSstats, Dg_bowling_stats)
        Binddata("player_id", "exec sp_game_batting_stats " & player_id, DSgame_batting_stats, Dg_game_batting_stats)
        Binddata("player_id", "exec sp_game_bowling_stats " & player_id, DSgame_bowling_stats, Dg_game_bowling_stats)
        totals()
        'Dg_Batting_stats.DataBind()

    End Sub
    Private Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.PreRender

        'If Not IsPostBack Then
        '    If Request.QueryString("player_ID") = 0 Then
        '        Me.Dlplayer.SelectedIndex = 0
        '        player_id = DSplayer.Tables(0).Compute("min(player_id)", "")
        '        Binddata("player_id", "exec sp_games_played " & player_id, DSgames_played, dggames_played)
        '        Binddata("player_id", "exec sp_player_stats " & player_id, DSstats, Dgplayer_stats)

        '    Else
        '        player_id = viewstate("player_id")
        '        player_id = Request.QueryString("Player_id")
        '        Binddata("player_id", "exec sp_games_played " & Request.QueryString("Player_ID"), DSgames_played, dggames_played)
        '        Binddata("player_id", "exec sp_player_stats " & Request.QueryString("Player_ID"), DSstats, Dgplayer_stats)
        '        Binddata2("player_id", "sp_selected_player_index " & Request.QueryString("Player_ID"), dsposition)
        '        totals()
        '        Dgplayer_stats.DataBind()
        '        dggames_played.DataBind()
        '        Dlplayer.SelectedIndex = dsposition.Tables(0).Compute("max(selected_index)", "")
        '    End If
        'Else

        'End If
    End Sub

    Private Sub dggames_played_ItemCommand(ByVal source As System.Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs)

        'Dim querystring As String
        'Dim itemcode As LinkButton

        'dggames_played.SelectedIndex = e.Item.ItemIndex

        'itemcode = dggames_played.Items(e.Item.ItemIndex).Cells(1).FindControl("LbtngameId")
        'querystring = "game.aspx?game_id=" + itemcode.Text
        'Response.Redirect(querystring)

    End Sub

    Private Sub Dgplayer_stats_ItemCommand(ByVal source As System.Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs)

        'Dim querystring As String
        'Dim itemcode As LinkButton

        'Dgplayer_stats.SelectedIndex = e.Item.ItemIndex

        'itemcode = Dgplayer_stats.Items(e.Item.ItemIndex).Cells(9).FindControl("Lbgame_type_id")
        'querystring = "rules.aspx?game_type_id=" + itemcode.Text
        'Response.Redirect(querystring)

    End Sub

    Private Sub Dgplayer_stats_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub
End Class
