ARTICLE

How to pass data between asp. net Web Pages using QueryString in VB.NET

Posted by Rohatash Kumar Articles | ASP.NET using VB.NET June 15, 2011
Here, we will see how to pass value from one page to another page using QueryString.
Download Files:
 
Reader Level:

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

tab1.gif

Figure1

Now create two page in ASP. Net To pass value from one page to another page using QueryString and accessing that value to retrieve database information. The form looks like the following figure.

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 Object, ByVal eAs System.EventArgs)Handles Me.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 Object, ByVal eAs System.EventArgs)Handles Me.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.

tab2.gif

Figure2

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

tab3.gif

Figure3

You can also download the attachment and test it yourself.

Login to add your contents and source code to this article
share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Become a Sponsor