Imports System.Web.Configuration
Imports Superbowl.sqlDB

Partial Class SnellsupergroupDetails
  Inherits System.Web.UI.Page

  Public Shared SnellsupergroupID As String = ""
  Public Shared dwSnellsupergroupName As String = ""
  Private db As New sqlDB
  Private ds As DataSet
  Private dr, drIni As DataRow

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    If Not Page.IsPostBack Then
      WebConfigurationManager.AppSettings("strPage") = "SnellsupergroupDetails"
      If WebConfigurationManager.AppSettings("ULEVEL") <> 5 Then
        btnSave.Enabled = False
      End If
      Try
        SnellsupergroupID = Request.QueryString("SsgID")
      Catch ex As Exception
        SnellsupergroupID = ""
      End Try

      If SnellsupergroupID <> "" Then
        Try
          drIni = sqlDB.GetRow(db.sql_get_Snellsupergroup(SnellsupergroupID))
          txtSnellsupergroup.Text = drIni!strSnellsupergroup
          txtSnellsupergroup.ReadOnly = True

          dwSnellsupergroupName = drIni!strSnellsupergroupName                                'get ori SnellsupergroupName for dw
          strSnellsupergroupName.Text = drIni!strSnellsupergroupName
          lblLastEdit.Text = Nz(drIni!strLastUser, "unknown") & " - " & SqlDate(Nz(drIni!dtStamp, ""), "d MMMM yyyy HH:mm")

        Catch ex As IndexOutOfRangeException                'dr = ds.tables(0).rows(0) doesn't exist
          lblMessage.Text = "Snellsupergroup ID, <font color=black>" & SnellsupergroupID & "</font>, was not found!"
          drIni = Nothing
        End Try
      Else
        dwSnellsupergroupName = ""
        txtSnellsupergroup.ReadOnly = False
      End If
    End If
  End Sub

  Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
    lblMessage.Text = ""

    If txtSnellsupergroup.Text.Trim() = "" Then lblMessage.Text &= "Snell Supergroup Code required.<BR>"
    If strSnellsupergroupName.Text.Trim() = "" Then lblMessage.Text &= "Snell Supergroup Description required.<BR>"
   
    If sqlDB.CountRows(db.sql_get_Snellsupergroup(txtSnellsupergroup.Text.Trim, SnellsupergroupID)) > 0 Then lblMessage.Text &= "Snell Supergroup """ & txtSnellsupergroup.Text.Trim & """ already exists!"

    If Not lblMessage.Text.Length > 0 Then

      If SnellsupergroupID <> "" Then
            sqlDB.doQueryDS(db.sql_update_Snellsupergroup(SnellsupergroupID, strSnellsupergroupName.Text.Trim, Session.Item("User")))
        '2-Update into DW	 x4	
        If dwSnellsupergroupName <> strSnellsupergroupName.Text Then
          sqlDB.doQueryDS(db.sql_update_Snellsupergroup_dw(SnellsupergroupID, strSnellsupergroupName.Text.Trim))
        End If
      Else
            sqlDB.doQueryDS(db.sql_insert_Snellsupergroup(txtSnellsupergroup.Text.Trim(), strSnellsupergroupName.Text.Trim, Session.Item("User")))
      End If

      Response.Redirect("SnellsupergroupList.aspx")
    End If

  End Sub

  Protected Sub btnClose_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClose.Click
    Response.Redirect("SnellsupergroupList.aspx")
  End Sub

  Protected Sub txtSnellsupergroup_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles txtSnellsupergroup.TextChanged
    'load rep
    If db.CountRows("SELECT * FROM mstSnellsupergroup WHERE strSnellsupergroup = '" & sqlDB.CleanString(txtSnellsupergroup.Text.Trim) & "' ") > 0 Then
      Response.Redirect("SnellsupergroupDetails.aspx?SsgID=" & sqlDB.CleanString(txtSnellsupergroup.Text.Trim))
    Else
      strSnellsupergroupName.Focus()
    End If
  End Sub
End Class