Solved

asp.net VB refresh page after button click

Posted on 2010-09-18
3
924 Views
Last Modified: 2012-06-27
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
Comment
Question by:elliottbenzle
3 Comments
 
LVL 30

Expert Comment

by:Reza Rad
ID: 33710507
what is your page_load method content?
0
 
LVL 4

Author Comment

by:elliottbenzle
ID: 33710642
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
 
LVL 8

Accepted Solution

by:
Mohit Vijay earned 500 total points
ID: 33710892
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now