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