Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. ASP.NET, AJAX and SQL Server question

ASP.NET, AJAX and SQL Server question

Scheduled Pinned Locked Moved ASP.NET
databasequestioncsharpasp-netsql-server
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kallileo
    wrote on last edited by
    #1

    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?

    N 1 Reply Last reply
    0
    • K kallileo

      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?

      N Offline
      N Offline
      Not Active
      wrote on last edited by
      #2

      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

      K 1 Reply Last reply
      0
      • N Not Active

        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

        K Offline
        K Offline
        kallileo
        wrote on last edited by
        #3

        Do you have any examples for the javascript? I will use dynamic SQL statements only for testing. Thank you for your tips.

        N 1 Reply Last reply
        0
        • K kallileo

          Do you have any examples for the javascript? I will use dynamic SQL statements only for testing. Thank you for your tips.

          N Offline
          N Offline
          Not Active
          wrote on last edited by
          #4

          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

          K 1 Reply Last reply
          0
          • N Not Active

            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

            K Offline
            K Offline
            kallileo
            wrote on last edited by
            #5

            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.

            N 1 Reply Last reply
            0
            • K kallileo

              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.

              N Offline
              N Offline
              Not Active
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups