G
Guest
I have a dynamically created command button on a .net page that adds 1 row to a sql server table when clicked. The page_load event load rows from that table for the user to view, but for some reason it is not loading the most recently added record when the page is reloaded from the command button click. When the page is initially loaded it does display all records. It is like it is executing the page_load method before the method to add the row to the table. Is this what is happening in my code below and how do i get around it?
(edited for simplicity)
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim db As New SqlClient.SqlConnection
Dim sql As String, id As String, rdr
sql = "<select rows from table>"
Dim cmd As New SqlClient.SqlCommand(sql, db)
rdr = cmd.ExecuteReader
If rdr.read Then
'populate static field on form
End If
'Call to method to read rows from table
LoadSteps(db)
End Sub
Private Sub AddStep(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Dim db As New SqlClient.SqlConnection, sql As String, nextId
sql = "<insert rows into jobstep table>"
Dim cmd As New SqlClient.SqlCommand(sql, db)
cmd.ExecuteNonQuery()
End Sub
Private Sub LoadSteps(ByVal db As SqlClient.SqlConnection)
Dim sql As String
sql = "<select rows from jobstep table>"
Dim cmd As New SqlClient.SqlCommand(sql, db)
Dim rdr = cmd.ExecuteReader
Do While rdr.read
'dynamically build table and write out rows from select
Loop
'dynamically create textarea fields for user to enter next step
'Create save button
btnSave.ID = "btnSave"
btnSave.Style.Item("width") = "88px"
btnSave.Text = "Save Step"
btnSave.CssClass = "mybutton"
'Add buttons to table
End Sub
(edited for simplicity)
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim db As New SqlClient.SqlConnection
Dim sql As String, id As String, rdr
sql = "<select rows from table>"
Dim cmd As New SqlClient.SqlCommand(sql, db)
rdr = cmd.ExecuteReader
If rdr.read Then
'populate static field on form
End If
'Call to method to read rows from table
LoadSteps(db)
End Sub
Private Sub AddStep(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Dim db As New SqlClient.SqlConnection, sql As String, nextId
sql = "<insert rows into jobstep table>"
Dim cmd As New SqlClient.SqlCommand(sql, db)
cmd.ExecuteNonQuery()
End Sub
Private Sub LoadSteps(ByVal db As SqlClient.SqlConnection)
Dim sql As String
sql = "<select rows from jobstep table>"
Dim cmd As New SqlClient.SqlCommand(sql, db)
Dim rdr = cmd.ExecuteReader
Do While rdr.read
'dynamically build table and write out rows from select
Loop
'dynamically create textarea fields for user to enter next step
'Create save button
btnSave.ID = "btnSave"
btnSave.Style.Item("width") = "88px"
btnSave.Text = "Save Step"
btnSave.CssClass = "mybutton"
'Add buttons to table
End Sub