Link to home
Start Free TrialLog in
Avatar of GlobaLevel
GlobaLevelFlag for United States of America

asked on

Convert HTA into ASP.net app...if last key press = .03 than send data to MS SQL DB...


The goal is to have an app that would begin timing at last keypress..once it reached 3 seconds..it would send whatever data is in the textbox/label control to a DB.... then it would delete whatever is in the control...fresh for a new session...

Here is the thread that got it to the HTA...

https://www.experts-exchange.com/questions/26531086/vbscript-user-starts-typing-in-a-txt-file-if-three-seconds-go-by-no-activity-send-data-to-db.html?anchorAnswerId=33929706#a33929706
<html>
<head>
<title>My HTML application</title>
<HTA:APPLICATION
  APPLICATIONNAME="Text Collector"
  ID="TextCollector"
  VERSION="1.0"/>
</head>

<script language="VBScript">
Option Explicit
Dim intTimer,strStatus
Sub Window_OnLoad

  'This method will be called when the application loads
  'Add your code here
intTimer = window.setTimeout("TimedOut", 3000) 'milliseconds
RestartTimer()
End Sub
  
Sub RestartTimer()
window.cleartimeout(intTimer)  
intTimer = window.setTimeout("TimedOut", 3000) 'milliseconds
strStatus="active"
spstatus.innerhtml=strStatus
End Sub


Sub KeyPressedinInput()
RestartTimer()
spdebug.innerhtml=taInput.value  
End Sub


Sub TimedOut()
	If tainput.value <> "" Then
  		StoreData(tainput.value)
  		taInput.value=""
  	End If
  	
  	strStatus="Hibernating"
  	spstatus.innerhtml=strStatus
  	
  	
End Sub

Sub StoreData(strText)
	taoutput.value=taoutput.value & strText
	
'	Const adOpenStatic = 3
'	Const adLockOptimistic = 3

'	Set objConnection = CreateObject("ADODB.Connection")
'	Set objRecordSet = CreateObject("ADODB.Recordset")

'	objConnection.Open _
'    "Provider=SQLOLEDB;Data Source=atl-sql-01;" & _
'        "Trusted_Connection=Yes;Initial Catalog=Northwind;" & _
'             "User ID=fabrikam\kenmyer;Password=34DE6t4G!;"

'	objRecordSet.Open "SELECT * FROM GeneralProperties" , _
'    objConnection, adOpenStatic, adLockOptimistic

'	objRecordSet.AddNew
'	objRecordSet("Recordedtext") = strText

'	objRecordSet.Update

'	objRecordSet.Close
'	objConnection.Close
	
	
End Sub

</script>

<body bgcolor="white">

<!--Add your controls here-->

<textarea name="taInput" id="taInput" rows="25" cols="80" onKeyPress="KeyPressedinInput"></textarea>
<br>
<br>
<textarea name="taOutput" id="taOutput" rows="5" cols="80"></textarea>
<br>
<span name="spStatus" id="spStatus" ></span>
<br>
<span name="spDebug" id="spDebug" ></span>
<!--{{InsertControlsHere}}-Do not remove this line-->
</body>
</html>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of YZlat
YZlat
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
and here is your code page Default.aspx.vb
mports System.Data
Imports System.Data.SqlClient
Partial Class _Default
    Inherits System.Web.UI.Page
    Public intTimer As Timer
    Public strStatus As String
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            

        intTimer = New Timer()

        intTimer.Interval = 3000

        RestartTimer()
    End Sub

  

    Sub RestartTimer()

        intTimer.Interval = 3000
        strStatus = "active"
        Me.spStatus.InnerHtml = strStatus
    End Sub


    Sub KeyPressedinInput()
        RestartTimer()
        Me.spDebug.InnerHtml = taInput.Value
    End Sub


    Sub TimedOut()
        If taInput.Value <> "" Then
            StoreData(taInput.Value)
            taInput.Value = ""
        End If

        strStatus = "Hibernating"
        Me.spStatus.InnerHtml = strStatus


    End Sub

    Sub StoreData(ByVal strText As String)
        taOutput.Value = taOutput.Value & strText
        Dim result As Integer
        Dim sql As String
        Dim strConn = "Provider=SQLOLEDB;Data Source=atl-sql-01;" & _
              "Trusted_Connection=Yes;Initial Catalog=Northwind;" & _
                     "User ID=fabrikam\kenmyer;Password=34DE6t4G!;"
        Dim conn As SqlConnection
        sql = "UPDATE GeneralProperties SET Recordedtext='" & strText & "'"
        Try
            conn = New SqlConnection(strConn)
            conn.Open()

            Dim cmd As SqlCommand = New SqlCommand(sql, conn)
            ' Configure the SqlCommand object
            If Not cmd Is Nothing Then

                With cmd
                    .Connection = conn
                    .CommandType = CommandType.Text      ''Set type to Text
                    .CommandText = sql                    ''Specify sql to run
                End With
            End If

            result = cmd.ExecuteNonQuery()
        Catch ex As SqlException
            Console.Write("SQL ERROR: " & ex.Message)
        Catch ex As Exception
            Console.Write("ERROR: " & ex.Message)
        Finally
            conn.Close()
        End Try



    End Sub



End Class

Open in new window

Avatar of GlobaLevel

ASKER

from design I can drop a sqldatasource from toolbox that will begin the sql wizard..right..even if the control is embedded in the HTA?
not sure I understand what are you talking about?
YZlat...

I tried the code...but it doesnt seems to register the keypress...wehnI run as a stand alone .html app it runs fine...but when I try it through the IIS...it doesnt function....is this an IIS security issue?
when I run as a stand alone .html it runs fine...when I run as .html or .aspx going thru the IIS..all the client events stop working...
ASP.net tries to attach these events on the server side, instead of putting them in the markup, try using the following code in your Page_Load

 taInput.Attributes.Add("onKeyPress", "KeyPressedinInput")