Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 966
  • Last Modified:

asp.net VB refresh page after button click

On my page I am updating a record on button click. But the page is not reloading itself, so after updating it will still display the old contents. What can I add to the code below to make the page reload after updating the db?

Thanks

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim fileName As String = "countdown_bg.png"
        If FileUpload1.HasFile Then
            ' = FileUpload1.FileName
            FileUpload1.SaveAs(MapPath("images/" & fileName))
            Message.Text = "Upload complete."
        Else
            Message.Text = "No files uploaded."
        End If

        Dim LinkID As Integer = 1
        Dim conn_update_link As SqlConnection
        Dim comm_update_link As SqlCommand
        Dim connectionString_update_link As String = _
        ConfigurationManager.ConnectionStrings( _
        "play3on3").ConnectionString
        conn_update_link = New SqlConnection(connectionString_update_link)
        comm_update_link = New SqlCommand( _
        "UPDATE countdown SET year= @year WHERE id = @LinkID", conn_update_link)
        comm_update_link.Parameters.Add("@LinkID", Data.SqlDbType.Int)
        comm_update_link.Parameters.Item("@LinkID").Value = LinkID
        comm_update_link.Parameters.Add("@year", Data.SqlDbType.Int)
        comm_update_link.Parameters.Item("@year").Value = ddlYear.SelectedItem.Value
        Try
            conn_update_link.Open()
            comm_update_link.ExecuteNonQuery()
        Catch
            Message.Text = _
            "Error updating the database.<br />"
        Finally
            conn_update_link.Close()
        End Try

    End Sub
0
elliottbenzle
Asked:
elliottbenzle
1 Solution
 
Reza RadCommented:
what is your page_load method content?
0
 
elliottbenzleAuthor Commented:
Protected Sub Page_Load_Slides(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim conn_slides As SqlConnection
        Dim comm_slides As SqlCommand
        Dim reader_slides As SqlDataReader
        Dim connectionString_slides As String = _
        ConfigurationManager.ConnectionStrings( _
        "play3on3").ConnectionString
        conn_slides = New SqlConnection(connectionString_slides)
        comm_slides = New SqlCommand( _
        "SELECT slide " & _
        "FROM slides_side ", conn_slides)
        Try
            conn_slides.Open()
            reader_slides = comm_slides.ExecuteReader()
            If reader_slides.Read() Then
                slide1.ImageUrl = "slides/" & reader_slides.Item("slide").ToString()
                reader_slides.Read()
                slide2.ImageUrl = "slides/" & reader_slides.Item("slide").ToString()
                reader_slides.Read()
                slide3.ImageUrl = "slides/" & reader_slides.Item("slide").ToString()
                reader_slides.Read()
                slide4.ImageUrl = "slides/" & reader_slides.Item("slide").ToString()
                reader_slides.Read()
                slide5.ImageUrl = "slides/" & reader_slides.Item("slide").ToString()

            End If
            reader_slides.Close()
        Catch
        Finally
            conn_slides.Close()
        End Try
    End Sub
0
 
Mohit VijayCommented:
My Suggestion will be:

Create a function, that fetch data from data base (I mean whatever you are doing on page load), so that function will also be called from page load and also be called after you have updated record in databse on button click.

Let me try to explain you how (I am providing just structure, please modify it according to you best needs)

=================Page Load Event=====================

Protected Sub Page_Load_Slides(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Page.ispostback = false then
FetchDataFromDatabase()
end if
End Sub
==================================================

================Function FetchDatafromDatabase() =====================

private sub FetchDataFromDatabase()
Dim conn_slides As SqlConnection
        Dim comm_slides As SqlCommand
        Dim reader_slides As SqlDataReader
        Dim connectionString_slides As String = _
        ConfigurationManager.ConnectionStrings( _
        "play3on3").ConnectionString
        conn_slides = New SqlConnection(connectionString_slides)
        comm_slides = New SqlCommand( _
        "SELECT slide " & _
        "FROM slides_side ", conn_slides)
        Try
            conn_slides.Open()
            reader_slides = comm_slides.ExecuteReader()
            If reader_slides.Read() Then
                slide1.ImageUrl = "slides/" & reader_slides.Item("slide").ToString()
                reader_slides.Read()
                slide2.ImageUrl = "slides/" & reader_slides.Item("slide").ToString()
                reader_slides.Read()
                slide3.ImageUrl = "slides/" & reader_slides.Item("slide").ToString()
                reader_slides.Read()
                slide4.ImageUrl = "slides/" & reader_slides.Item("slide").ToString()
                reader_slides.Read()
                slide5.ImageUrl = "slides/" & reader_slides.Item("slide").ToString()

            End If
            reader_slides.Close()
        Catch
        Finally
            conn_slides.Close()
        End Try
End Sub

=============Button Click Event================

 Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim fileName As String = "countdown_bg.png"
        If FileUpload1.HasFile Then
            ' = FileUpload1.FileName
            FileUpload1.SaveAs(MapPath("images/" & fileName))
            Message.Text = "Upload complete."
        Else
            Message.Text = "No files uploaded."
        End If

        Dim LinkID As Integer = 1
        Dim conn_update_link As SqlConnection
        Dim comm_update_link As SqlCommand
        Dim connectionString_update_link As String = _
        ConfigurationManager.ConnectionStrings( _
        "play3on3").ConnectionString
        conn_update_link = New SqlConnection(connectionString_update_link)
        comm_update_link = New SqlCommand( _
        "UPDATE countdown SET year= @year WHERE id = @LinkID", conn_update_link)
        comm_update_link.Parameters.Add("@LinkID", Data.SqlDbType.Int)
        comm_update_link.Parameters.Item("@LinkID").Value = LinkID
        comm_update_link.Parameters.Add("@year", Data.SqlDbType.Int)
        comm_update_link.Parameters.Item("@year").Value = ddlYear.SelectedItem.Value
        Try
            conn_update_link.Open()
            comm_update_link.ExecuteNonQuery()
        Catch
            Message.Text = _
            "Error updating the database.<br />"
        Finally
            conn_update_link.Close()

FetchDataFromDatabase()
        End Try

    End Sub
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now