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()
        Dim 
        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
        Try
            con.Open()
            cmd.ExecuteNonQuery()
            'lblMessage.Text = "Record inserted successfully"
        Catch ex As Exception
            Throw ex
        Finally
            con.Close()
            con.Dispose()
        End Try

    End Sub

Open in new window

derrekdeveloperAsked:
Who is Participating?
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:
Hi,

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.
0
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"), _
        TextBox)
    If Not SourceTextBox Is Nothing Then
        Label1.Text = SourceTextBox.Text
    End If
End If

For reference see the following link:

https://msdn.microsoft.com/en-us/library/ms178139(v=vs.100).aspx
0
derrekdeveloperAuthor 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.
0
jshesekCommented:
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:
https://msdn.microsoft.com/en-us/library/6c3yckfw(v=vs.140).aspx
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.
0

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
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.