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. Page_Load calling Sub [modified]

Page_Load calling Sub [modified]

Scheduled Pinned Locked Moved ASP.NET
database
7 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

    I do initialize some controls on Page_Load. Sub Page_Load(ByVal Sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load SQLconn.Open() Dim SelectCmd1 As New SqlCommand("SP1", SQLconn) SelectCmd1.CommandType = CommandType.StoredProcedure Dim result1 As Integer Dim myDataReader1 As SqlDataReader myDataReader1 = SelectCmd1.ExecuteReader() myDataReader1.Read() result1 = myDataReader1.GetInt32(0) SQLconn.Close() ChangeButton(result1) End Sub Sub ChangeButton(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 I moved the code from Page_Load to MyRefresh bc I want to call this code fom other events too: Sub Page_Load() If Not Page.IsPostBack Then MyRefresh() End If End Sub Sub MyRefresh() SQLconn.Open() Dim SelectCmd1 As New SqlCommand("SP1", SQLconn) SelectCmd1.CommandType = CommandType.StoredProcedure Dim result1 As Integer Dim myDataReader1 As SqlDataReader myDataReader1 = SelectCmd1.ExecuteReader() myDataReader1.Read() result1 = myDataReader1.GetInt32(0) SQLconn.Close() ChangeButton(result1) End Sub I don't get any errors but the controls get no values from the database. Something is missing but I don't know what.:doh: Thanks -- modified at 10:05 Friday 15th June, 2007

    K 1 Reply Last reply
    0
    • K kallileo

      I do initialize some controls on Page_Load. Sub Page_Load(ByVal Sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load SQLconn.Open() Dim SelectCmd1 As New SqlCommand("SP1", SQLconn) SelectCmd1.CommandType = CommandType.StoredProcedure Dim result1 As Integer Dim myDataReader1 As SqlDataReader myDataReader1 = SelectCmd1.ExecuteReader() myDataReader1.Read() result1 = myDataReader1.GetInt32(0) SQLconn.Close() ChangeButton(result1) End Sub Sub ChangeButton(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 I moved the code from Page_Load to MyRefresh bc I want to call this code fom other events too: Sub Page_Load() If Not Page.IsPostBack Then MyRefresh() End If End Sub Sub MyRefresh() SQLconn.Open() Dim SelectCmd1 As New SqlCommand("SP1", SQLconn) SelectCmd1.CommandType = CommandType.StoredProcedure Dim result1 As Integer Dim myDataReader1 As SqlDataReader myDataReader1 = SelectCmd1.ExecuteReader() myDataReader1.Read() result1 = myDataReader1.GetInt32(0) SQLconn.Close() ChangeButton(result1) End Sub I don't get any errors but the controls get no values from the database. Something is missing but I don't know what.:doh: Thanks -- modified at 10:05 Friday 15th June, 2007

      K Offline
      K Offline
      kubben
      wrote on last edited by
      #2

      Have you tried debuging the code? What is the value that you are passing into the changebutton method? when you call the stored procedure manually do you get what you are expecting? I would guess that your call to the stored procedure is not returning what you are expecting. Try debugging the code and see if that helps you figure it out. Ben

      K 1 Reply Last reply
      0
      • K kubben

        Have you tried debuging the code? What is the value that you are passing into the changebutton method? when you call the stored procedure manually do you get what you are expecting? I would guess that your call to the stored procedure is not returning what you are expecting. Try debugging the code and see if that helps you figure it out. Ben

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

        I'm absolutely sure that the SP is correct bc when I have the content of the content of MyRefresh() in the Page_Load it works without any problems. But when I moved the code to MyRefresh() and call it from Page_Load it doesn't work Sub Page_Load() If Not Page.IsPostBack Then Refresh() End If End Sub

        K 1 Reply Last reply
        0
        • K kallileo

          I'm absolutely sure that the SP is correct bc when I have the content of the content of MyRefresh() in the Page_Load it works without any problems. But when I moved the code to MyRefresh() and call it from Page_Load it doesn't work Sub Page_Load() If Not Page.IsPostBack Then Refresh() End If End Sub

          K Offline
          K Offline
          kubben
          wrote on last edited by
          #4

          Well, based on the code you posted the only difference I can see is in the new code you are checking If Not Page.IsPostback This seems like the correct thing to do, but it doesn't seem you did that when you weren't using the MyRefresh() method. So I guess you could try removing that and see if it works. I still think you should be able to step through the program and see if the code is getting called correctly. Ben

          K 1 Reply Last reply
          0
          • K kubben

            Well, based on the code you posted the only difference I can see is in the new code you are checking If Not Page.IsPostback This seems like the correct thing to do, but it doesn't seem you did that when you weren't using the MyRefresh() method. So I guess you could try removing that and see if it works. I still think you should be able to step through the program and see if the code is getting called correctly. Ben

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

            If Not Page.IsPostback has no effect...but I think I found something. Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load SQLconn.Open() Dim SelectCmd1 As New SqlCommand("SP1", SQLconn) SelectCmd1.CommandType = CommandType.StoredProcedure Dim result1 As Integer Dim myDataReader1 As SqlDataReader myDataReader1 = SelectCmd1.ExecuteReader() myDataReader1.Read() result1 = myDataReader1.GetInt32(0) SQLconn.Close() ChangeButton1(result1) End Sub "Handles MyBase.Load" is the key....when I remove this I have the same as in the MyRefresh(). I had to modified it like this: Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load If Not Page.IsPostBack Then Refresh() End If End Sub Sub Refresh() SQLconn.Open() Dim SelectCmd1 As New SqlCommand("SP1", SQLconn) SelectCmd1.CommandType = CommandType.StoredProcedure Dim result1 As Integer Dim myDataReader1 As SqlDataReader myDataReader1 = SelectCmd1.ExecuteReader() myDataReader1.Read() result1 = myDataReader1.GetInt32(0) SQLconn.Close() ChangeButton1(result1) End Sub Why is Handles MyBase.Load so important???

            K 1 Reply Last reply
            0
            • K kallileo

              If Not Page.IsPostback has no effect...but I think I found something. Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load SQLconn.Open() Dim SelectCmd1 As New SqlCommand("SP1", SQLconn) SelectCmd1.CommandType = CommandType.StoredProcedure Dim result1 As Integer Dim myDataReader1 As SqlDataReader myDataReader1 = SelectCmd1.ExecuteReader() myDataReader1.Read() result1 = myDataReader1.GetInt32(0) SQLconn.Close() ChangeButton1(result1) End Sub "Handles MyBase.Load" is the key....when I remove this I have the same as in the MyRefresh(). I had to modified it like this: Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load If Not Page.IsPostBack Then Refresh() End If End Sub Sub Refresh() SQLconn.Open() Dim SelectCmd1 As New SqlCommand("SP1", SQLconn) SelectCmd1.CommandType = CommandType.StoredProcedure Dim result1 As Integer Dim myDataReader1 As SqlDataReader myDataReader1 = SelectCmd1.ExecuteReader() myDataReader1.Read() result1 = myDataReader1.GetInt32(0) SQLconn.Close() ChangeButton1(result1) End Sub Why is Handles MyBase.Load so important???

              K Offline
              K Offline
              kubben
              wrote on last edited by
              #6

              Without it your load event doesn't fire. That is how your code behind methods get linked into events. Glad you figured it out. Ben

              K 1 Reply Last reply
              0
              • K kubben

                Without it your load event doesn't fire. That is how your code behind methods get linked into events. Glad you figured it out. Ben

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

                Thank you.. but I haven't finished yet..

                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