ASP.NET, AJAX and SQL Server question
-
This the code to initialize a button control that I have on my page: Sub Page_Load(ByVal Sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load If Not Page.IsPostBack Then SQLconn.Open() Dim SelectCmd1 As New SqlCommand("SELECT [rawvalue] FROM [Data] WHERE (timestamp=(SELECT MAX(timestamp) FROM [Data]WHERE(name = 's1'))) AND (name='s1')", SQLconn) SelectCmd1.CommandType = CommandType.Text Dim result1 As Integer Dim myDataReader1 As SqlDataReader myDataReader1 = SelectCmd1.ExecuteReader() myDataReader1.Read() result1 = myDataReader1.GetInt32(0) SQLconn.Close() ChangeButton1(result1) End If End Sub Sub ChangeButton1(ByVal Status As Integer) If Status = -1 Then Label1.Text = "ON" OnOff1.Style.Value = "background-image: url(../ImgContr/1.png);" Else Label1.Text = "OFF" OnOff1.Style.Value = "background-image: url(../ImgContr/2.png);" End If End Sub Also I use to write to the database: Sub OnOff_Click1(ByVal Sender As Object, ByVal e As System.EventArgs) Dim updateCMD As SqlCommand Dim query As String If Label1.Text = "ON" Then query = "UPDATE [Triggers] SET [rawvalue] = 'False' WHERE [name] = 's1'" ChangeButton1(0) Else query = "UPDATE [Triggers] SET [rawvalue] = 'True' WHERE [name] = 's1'" ChangeButton1(-1) End If updateCMD = New SqlCommand(query, SQLconn) updateCMD.CommandType = CommandType.Text SQLconn.Open() updateCMD.ExecuteNonQuery() SQLconn.Close() End Sub I used Ajax Update Panel with my button(OnOff1) inside to write the to the database without reloading the page. What I want now is to poll the database every few seconds to read the values without pressing refresh(F5). I believe that I have to use the Ontick event of the Update Panel Trigger and a asp Timer control. How should I modify the Sub for Page_Load Event?
-
This the code to initialize a button control that I have on my page: Sub Page_Load(ByVal Sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load If Not Page.IsPostBack Then SQLconn.Open() Dim SelectCmd1 As New SqlCommand("SELECT [rawvalue] FROM [Data] WHERE (timestamp=(SELECT MAX(timestamp) FROM [Data]WHERE(name = 's1'))) AND (name='s1')", SQLconn) SelectCmd1.CommandType = CommandType.Text Dim result1 As Integer Dim myDataReader1 As SqlDataReader myDataReader1 = SelectCmd1.ExecuteReader() myDataReader1.Read() result1 = myDataReader1.GetInt32(0) SQLconn.Close() ChangeButton1(result1) End If End Sub Sub ChangeButton1(ByVal Status As Integer) If Status = -1 Then Label1.Text = "ON" OnOff1.Style.Value = "background-image: url(../ImgContr/1.png);" Else Label1.Text = "OFF" OnOff1.Style.Value = "background-image: url(../ImgContr/2.png);" End If End Sub Also I use to write to the database: Sub OnOff_Click1(ByVal Sender As Object, ByVal e As System.EventArgs) Dim updateCMD As SqlCommand Dim query As String If Label1.Text = "ON" Then query = "UPDATE [Triggers] SET [rawvalue] = 'False' WHERE [name] = 's1'" ChangeButton1(0) Else query = "UPDATE [Triggers] SET [rawvalue] = 'True' WHERE [name] = 's1'" ChangeButton1(-1) End If updateCMD = New SqlCommand(query, SQLconn) updateCMD.CommandType = CommandType.Text SQLconn.Open() updateCMD.ExecuteNonQuery() SQLconn.Close() End Sub I used Ajax Update Panel with my button(OnOff1) inside to write the to the database without reloading the page. What I want now is to poll the database every few seconds to read the values without pressing refresh(F5). I believe that I have to use the Ontick event of the Update Panel Trigger and a asp Timer control. How should I modify the Sub for Page_Load Event?
You need to write some javascript. Use the setInterval function to call your server-side method and get the refreshed data. Then you can refresh the control used to display your data. Another tip: Stop using dynamic SQL statements! Use stored procs ro at least validate the values used to build your statements.
only two letters away from being an asset
-
You need to write some javascript. Use the setInterval function to call your server-side method and get the refreshed data. Then you can refresh the control used to display your data. Another tip: Stop using dynamic SQL statements! Use stored procs ro at least validate the values used to build your statements.
only two letters away from being an asset
-
Do you have any examples for the javascript? I will use dynamic SQL statements only for testing. Thank you for your tips.
kallileo wrote:
I will use dynamic SQL statements only for testing.
Yeah, I've heard that many times. So you plan on writing two different versions of your code, one for testing and one for production? Can we guess at how reliable and maintainable that solution will be. As for the javascript, do a search for setInterval
only two letters away from being an asset
-
kallileo wrote:
I will use dynamic SQL statements only for testing.
Yeah, I've heard that many times. So you plan on writing two different versions of your code, one for testing and one for production? Can we guess at how reliable and maintainable that solution will be. As for the javascript, do a search for setInterval
only two letters away from being an asset
It's for presentation only. I'm a student and I neen it as a Interface to control a hardware device in my project. Is javascript realy necessary? I was thinking of using asp timer control with an interval and the OnTick event of the triggers in the update control to call the Sub to refresh the page. Thank you.
-
It's for presentation only. I'm a student and I neen it as a Interface to control a hardware device in my project. Is javascript realy necessary? I was thinking of using asp timer control with an interval and the OnTick event of the triggers in the update control to call the Sub to refresh the page. Thank you.
kallileo wrote:
I'm a student
Then you should be learning the proper way to code.
kallileo wrote:
Is javascript realy necessary?
AJAX = Asynchronous JAVASCRIPT and xml At some point if you are using AJAX you will need to learn it. No it isn't strictly necessary in your case, although I think it is easy to use then what you trying. -- modified at 11:52 Tuesday 12th June, 2007
only two letters away from being an asset