Solved

asp.net VB refresh page after button click

Posted on 2010-09-18
3
931 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

830 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