Imports System.Data
Imports System.Data.SqlClient

Partial Class VariableList
  Inherits System.Web.UI.Page
  Public Function LoadVariables(ByVal NetworkName As String) As String
    ' construct a SQL query to get the list of ALL the variables measured for selected network
    Dim ds As Long = DateTime.Now.Ticks
    Dim stationCount As Integer = -1
    Dim varName, varCode As String
    Dim variableList As String = Nothing
    Dim SiteCodesList As String = ""
    Dim myConn As SqlConnection = Nothing
    Dim myCmd As SqlCommand = Nothing
    Dim myReader As SqlDataReader = Nothing
    Dim isSiteCodesAvailable As Boolean = False
    Dim sqlStr2 As String = ""
    'Dim networkId As String = "2" ' default 2, NWIS
    Dim oNetwork As HISNetwork
    Try
      oNetwork = Page.Session(ApSConfigAHS.KS_ACTIVENETWORK)

      listboxVariables.Items.Clear()
      myConn = New SqlConnection

      myConn.ConnectionString = oNetwork.DBConnection   'ConfigurationManager.ConnectionStrings(ApSConfigAHS.K_ODEConn).ConnectionString
      myCmd = New SqlCommand
      'networkId = ApSConfigAHS.GetNetworkID(NetworkName)

      myCmd.CommandText = ApSConfigAHS.QueryVariables_SQLQUERY_NETWORK_VARIABLES
      myCmd.CommandType = CommandType.Text
      myCmd.CommandTimeout = ApSConfigAHS.CMD_TimeOut
      'Dim networkIdParam As SqlParameter = New SqlParameter
      'networkIdParam.ParameterName = "@networkId"
      'networkIdParam.SqlDbType = SqlDbType.Int
      'networkIdParam.Size = 50
      'networkIdParam.Direction = ParameterDirection.Input
      'networkIdParam.Value = networkId
      'myCmd.Parameters.Add(networkIdParam)

      myCmd.Connection = myConn
      myCmd.Connection.Open()
      myReader = myCmd.ExecuteReader()

      Dim item As ListItem
      While myReader.Read()
        Try
          varCode = myReader(ApSConfigAHS.FD_VARIABLECODE).ToString.Trim    ' "VariableCode"
          varName = myReader(ApSConfigAHS.FD_VARIABLENAME).ToString.Trim    '  "VariableName"
          item = New ListItem(varName, varCode)
          listboxVariables.Items.Add(item)
        Catch ex As Exception
        End Try
      End While
      lblMsg.Text = "Variable list for Network: " & NetworkName & ". (" & listboxVariables.Items.Count & " variables)"
      Me.Title = "Variable list for network " & NetworkName & "."
    Catch ex As Exception
      If ((ApSConfigAHS.WebDebug And ApSConfigAHS.DebugType.LogError) = ApSConfigAHS.DebugType.LogError) Then
        ApSConfigAHS.Write2Log("queryVariables:" & ex.Message & ", " & ex.StackTrace, True, True, False)
      End If
      System.Diagnostics.Debug.WriteLine(ex.StackTrace)
      variableList = Nothing
    Finally
      If Not myConn Is Nothing Then
        If ((myConn.State = ConnectionState.Open) OrElse (myConn.State = ConnectionState.Connecting) OrElse (myConn.State = ConnectionState.Broken)) Then
          myConn.Close()
        End If
        myConn.Dispose()
      End If
      If Not myCmd Is Nothing Then
        myCmd.Dispose()
      End If
      If ((ApSConfigAHS.WebDebug And ApSConfigAHS.DebugType.LogDSTime) = ApSConfigAHS.DebugType.LogDSTime) Then
        ApSConfigAHS.Write2Log("VariableList.LoadVariables:", True, ds, False)
      End If
    End Try

    Return variableList
  End Function

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim oNetwork As HISNetwork
    oNetwork = Me.Page.Session(ApSConfigAHS.KS_ACTIVENETWORK)
    LoadVariables(oNetwork.Name)
  End Sub
End Class
