Best way to pass multiple query string value from one page to another page in Asp.Net C# VB.Net

In most of the cases developers need to pass more than one or multiple query string value from one page to another page. Here in this example i will show you how one can pass multiple values from one page to another page in an efficient way. Let I have some personal information and we want to send data from Default.aspx page to About.aspx page. I will show the best way in below:

Under Default.aspx Page_Load event Pass the Query String Like below:
C# Code:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Int32 nRollID = 16;
            Int32 nShiftID = 34;
            string sNickName = "Scott";
            Response.Redirect(string.Format("About.aspx?qs1={0}&qs2={1}&qs3={2}", nRollID, nShiftID, sNickName));
        }
    }

VB.Net Code:

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim nRollID As Int32 = 16
            Dim nShiftID As Int32 = 34
            Dim sNickName As String = "Scott"
            Response.Redirect(String.Format("About.aspx?qs1={0}&qs2={1}&qs3={2}", nRollID, nShiftID, sNickName))
        End If
    End Sub

Now read Query String Value from About.aspx Page:
C# Code:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if ((Request.QueryString["qs1"] != null && Request.QueryString["qs2"] != null) && Request.QueryString["qs3"] != null)
            {
                Int32 nRollID =Convert.ToInt32(Request.QueryString["qs1"]);
                Int32 nShiftID =Convert.ToInt32(Request.QueryString["qs2"]);
                string sNickName = Request.QueryString["qs3"];
                Response.Write(nRollID + "</br>");
                Response.Write(nShiftID + "</br>");
                Response.Write(sNickName);
            }
        }
    }

VB.Net Code:

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            If (Request.QueryString("qs1") IsNot Nothing AndAlso Request.QueryString("qs2") IsNot Nothing) AndAlso Request.QueryString("qs3") IsNot Nothing Then
                Dim nRollID As Int32 = Convert.ToInt32(Request.QueryString("qs1"))
                Dim nShiftID As Int32 = Convert.ToInt32(Request.QueryString("qs2"))
                Dim sNickName As String = Request.QueryString("qs3")
                Response.Write(nRollID & "</br>")
                Response.Write(nShiftID & "</br>")
                Response.Write(sNickName)
            End If
        End If
    End Sub

Output:
QueryString_Csharp_VB.Net

Download Code Example C#        Download Code Example VB.Net

Leave a Reply

Your email address will not be published. Required fields are marked *

AlphaOmega Captcha Classica  –  Enter Security Code