Pass values from one page to another and use insert stored procedure

I have two separate pages. I want to pass values from Form FIeld on one page to a second page. On the second page I want to use a stored procedure to insert the values being passed from Page one. In this case a single form field called name. I have used the following code successfully to pass values on a single page. My question is how should the code be modified to receive values from Page1.aspx with the FOrm Field "Name" to Page2.aspx and of course Pages.aspx.VB. I am making the assumption that I need to do a Page_Load event but I am not sure about that either.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)

        Dim strConnString As String = ConfigurationManager.ConnectionStrings("SQL2008R2_504887_golivefitnesConnectionString").ConnectionString
        Dim con As New SqlConnection(strConnString)
        Dim cmd As New SqlCommand()
        cmd.CommandType = CommandType.StoredProcedure
        cmd.CommandText = "namesearchs"
        cmd.Parameters.Add("@nameser", SqlDbType.VarChar).Value = name.Text.Trim()
        'cmd.Parameters.Add("@LastName", SqlDbType.VarChar).Value = txtLastName.Text.Trim()
        'cmd.Parameters.Add("@BirthDate", SqlDbType.DateTime).Value = txtBirthDate.Text.Trim()
        'cmd.Parameters.Add("@City", SqlDbType.VarChar).Value = txtCity.Text.Trim()
        'cmd.Parameters.Add("@Country", SqlDbType.VarChar).Value = txtCountry.Text.Trim()
        cmd.Connection = con
            'lblMessage.Text = "Record inserted successfully"
        Catch ex As Exception
            Throw ex
        End Try

    End Sub

Open in new window

derrekdeveloperEnterprise Solution ArchitectAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Lokesh B RDeveloperCommented:

Why are you passing the values from one page to another page?

You can directly insert the values to the database when the button is clicked in the Page1.aspx.

So write the code in the Button Click event in Page1.
Fareed Ali KhanCommented:
You can use the ASP.NET cross page post back feature if using Web Forms:

If Not Page.PreviousPage Is Nothing Then
    Dim SourceTextBox As TextBox
    SourceTextBox = CType(PreviousPage.FindControl("TextBox1"), _
    If Not SourceTextBox Is Nothing Then
        Label1.Text = SourceTextBox.Text
    End If
End If

For reference see the following link:
derrekdeveloperEnterprise Solution ArchitectAuthor Commented:
Lokesh, passing the values back to the same page works most of the time. However, users will be able to type search criteria in other pages such as the Home.aspx and the values need to be posted to the results page Results.aspx. I definitely understand what you are saying but I felt like it would be a good feature to let them perform or submit "Search" criteria from a few of the other pages within the site also.
Here's a couple of things I found you might want to try.  
This is a really old thing you might want to try.
Do a view source on page 1 and get the id of the "Name" textbox.  It will be something like this: ctl00$contentplaceholder2$Name  - then do a Request.Form("ctl00$contentplaceholder2$Name") on page 2.

2nd idea is create a session variable before you leave page 1:
So on Search button click or submit do "Session("searchName") = textboxName.text
On page 2 = Dim thisName as string
thisName = Session("searchname")

When they close the browser windows it's gone.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.