Pass data between asp. net Web Pages in VB.NET

Here, we will see how to pass value from one page to another page using Query String.
  • 4331
Download Files:
 

Here, we will see how to pass value from one page to another page using QueryString. To do that create a table in a SQL Server database with userId, Firstname, lastname, age fields. The table looks like this.

create table usertable

(

UserId varchar(100),

FirstName varchar(50),

LastName varchar(30),

Age varchar(30)

)

Now enter some values into the table.

insert into usertable values(5, 'rohatash','Kumar','23')

go

insert into usertable values(3, 'monu','rathor','20') 

go

insert into usertable values(4, 'rahul','sharma','24') 

go

insert into usertable values(8, 'ram','singh','26') 

Now using select statement.

select * from usertable;

OUTPUT

output-pass-data-between-asp. net-Web-Pages.gif
 


 

Page 1:

Default1.aspx

<div>

   <a href="showdetail.aspx?userid=5" title="Show records where userId is 5">Show records where userId is 5</a>

<p><asp:HyperLink ID="hyper1" runat="server" Text="Show Record where userID is 4"></asp:HyperLink> </p>

   </div>

 

Default1.aspx.cs

Protected Sub Page_Load(ByVal sender As ObjectByVal eAs System.EventArgs)HandlesMe.Load

       If Not IsPostBack Then

            hyper1.NavigateUrl = "showdetail.aspx?userid=4&com=show"

       End If

   End Sub

Now adding new page.

Page 2:

Showdetail.aspx

<div>

    <asp:DetailsView ID="DetailsView1" runat="server" EnableViewState="False"

           Width="146px">

       </asp:DetailsView>   

   </div>

Showdetail.aspx.cs

That QueryString value is retrieved into the Page_Load event of ShowDetails.aspx and passed to GetData method. GetData method uses ADO.NET to retrieve the values from the database and populates to the DetailsView.

Protected Sub Page_Load(ByVal sender As ObjectByVal eAs System.EventArgs)HandlesMe.Load

       If Not IsPostBack Then

           If Not String.IsNullOrWhiteSpace(Request.QueryString("userid"))Then

               Dim userId As Integer = 0

               Integer.TryParse(Request.QueryString("userid"), userId)

               If Not userId.Equals(0) Then

 

                    GetData(userId)

               End If

           End If

 

           Dim command As String = Request.QueryString("com")

       End If

   End Sub

   Private Sub GetData(ByVal userIdAs Integer)

       Dim table As New DataTable()

       Dim conn As New SqlConnection("Data Source=.;uid=sa;pwd=Password$2;database=master")

       If True Then

           Dim sql As String = "SELECT userId, FirstName, LastName, Age FROM usertable WHERE userId = @userId ORDER By userId"

           Using cmd As New SqlCommand(sql, conn)

               Dim ad As New SqlDataAdapter(cmd)

               If True Then

                   Dim prm As New SqlParameter("@userId",SqlDbType.Int)

                    prm.Value = userId

                    cmd.Parameters.Add(prm)

                    ad.Fill(table)

               End If

           End Using

       End If

        DetailsView1.DataSource = table

        DetailsView1.DataBind()

   End Sub

Now run the application. and test it.

pass-data-between-asp. net-Web-Pages.gif
 

Figure2

Now click of the hyperlink on the default.aspx. this will be redirect to ShowDetails.aspx page with QueryString values.

Redirect-pass-data-between-asp. net-Web-Pages.gif
 

Figure3

You can also download the attachment and test it yourself.

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.