<%@ Page language="vb"%>
<script language="vb" runat="server">
   '! Support file only, run AjaxTable.html instead !
   ' Uses new ASP.NET style scripting with event Page_Load
   ' -------------------------------------------------------------------------------------------------------------------------------
   Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)

      ' --- Database initialization ---
      Dim Path As String : Path = System.IO.Path.GetDirectoryName(Context.Request.PhysicalPath)
      Dim Conn As System.Data.OleDb.OleDbConnection : Conn = New System.Data.OleDb.OleDbConnection("Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Registry Path=;Jet OLEDB:Database Locking Mode=1;Data Source=""" + Path + "\\Database.mdb"";Mode=Share Deny None;Jet OLEDB:Engine Type=5;Provider=""Microsoft.Jet.OLEDB.4.0"";Jet OLEDB:System database=;Jet OLEDB:SFP=False;persist security info=False;Extended Properties=;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Create System Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;User ID=Admin;Jet OLEDB:Global Bulk Transactions=1")
      Conn.Open()
      Dim Cmd As System.Data.OleDb.OleDbCommand : Cmd = Conn.CreateCommand()
   
      ' --- Response initialization ---
      Response.ContentType = "text/xml"
      Response.Charset = "utf-8"
      Response.AppendHeader("Cache-Control", "max-age=1, must-revalidate")
      System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture("en-US")

      ' --- Save data to database ---
      Dim XML As String : XML = Request("TGData")
      If XML <> "" And XML <> Nothing Then
   
         Dim X As System.Xml.XmlDocument : X = New System.Xml.XmlDocument()
         X.LoadXml(HttpUtility.HtmlDecode(XML))
         Dim Ch As System.Xml.XmlNodeList : Ch = X.GetElementsByTagName("Changes")
         If (Ch.Count > 0) Then
            For Each I As System.Xml.XmlElement In Ch(0)
               Dim SQL As String : SQL = ""
               Dim id As String : id = I.GetAttribute("id")
         
               If I.GetAttribute("Deleted") = "1" Then
                  SQL = "DELETE FROM TableData WHERE ID=" + id

               ElseIf I.GetAttribute("Added") = "1" Then
                  SQL = "INSERT INTO TableData(ID,Project,Resource,Week,Hours) VALUES(" _
                     + id + "," _
                     + "'" + I.GetAttribute("Project").Replace("'", "''") + "'," _
                     + "'" + I.GetAttribute("Resource").Replace("'", "''") + "'," _
                     + I.GetAttribute("Week") + "," _
                     + I.GetAttribute("Hours") + ")"
               
               ElseIf I.GetAttribute("Changed") = "1" Then
                  SQL = "UPDATE TableData SET "
                  For Each A As System.Xml.XmlAttribute In I.Attributes
                     If A.Name = "Project" Or A.Name = "Resource" Then
                        SQL += A.Name + " = '" + A.Value.Replace("'", "''") + "',"
                     ElseIf A.Name = "Week" Or A.Name = "Hours" Then
                        SQL += A.Name + " = " + A.Value + ","
                     End If
                  Next A
                  SQL = SQL.TrimEnd(",".ToCharArray()) 'Last comma away
                  SQL = SQL + " WHERE ID=" + id
               End If
               
               Cmd.CommandText = SQL
               Cmd.ExecuteNonQuery()
            Next I
         End If
         Response.Write("<Grid><IO Result='0'/></Grid>")
      
      Else
         ' --- Load data from database ---

         Cmd.CommandText = "SELECT * FROM TableData"
         Dim R As System.Data.OleDb.OleDbDataReader : R = Cmd.ExecuteReader()
         Response.Write("<Grid><Body><B>")
         Do While R.Read()
            Response.Write("<I id='" + R(0).ToString() + "'" _
               + " Project='" + R(1).ToString().Replace("&", "&amp;").Replace("'", "&apos;").Replace("<", "&lt;") + "'" _
               + " Resource='" + R(2).ToString().Replace("&", "&amp;").Replace("'", "&apos;").Replace("<", "&lt;") + "'" _
               + " Week='" + R(3).ToString() + "'" _
               + " Hours='" + R(4).ToString() + "'" _
               + "/>")
         Loop
         Response.Write("</B></Body></Grid>")
      End If
   
      Conn.Close()
   End Sub
   ' -------------------------------------------------------------------------------------------------------------------------------
</script>
