Hi edaadura,
Check this example. Now please take its reference and correct your code.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
HTML
PageA
Country:
<asp:DropDownList ID="ddlCountry" runat="server">
<asp:ListItem Value="1">--Seletc--</asp:ListItem>
<asp:ListItem Value="2">UK</asp:ListItem>
<asp:ListItem Value="3">USA</asp:ListItem>
</asp:DropDownList>
<br />
City:
<asp:DropDownList ID="ddlCity" runat="server">
<asp:ListItem Value="1">--Seletc--</asp:ListItem>
<asp:ListItem Value="2">London</asp:ListItem>
<asp:ListItem Value="3">Seattle</asp:ListItem>
<asp:ListItem Value="4">Tacoma</asp:ListItem>
<asp:ListItem Value="5">Redmond</asp:ListItem>
</asp:DropDownList>
<br />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="Submit_OnClick" />
PageB
<asp:GridView ID="gvEmployee" runat="server" BorderColor="White" BorderWidth="0px"
AutoGenerateColumns="false" GridLines="None" EmptyDataText="No record found">
<Columns>
<asp:TemplateField HeaderText="">
<ItemStyle HorizontalAlign="Center" Width="50px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:HyperLink ID="hplEmployeeID" runat="server" Text='<%# Eval("EmployeeID")%>'
NavigateUrl='<%# Eval("EmployeeID","~/parliment/dewan/dn/year/2017_sesi1(J).aspx?id={0}")%>'></asp:HyperLink></h3>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("Name")%>'></asp:Label><br />
<asp:Label ID="lblCountry" runat="server" Text='<%# Eval("Country")%>'></asp:Label>
<asp:Label ID="lblcity" runat="server" Text='<%# Eval("City")%>'></asp:Label><br />
<hr />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Code
C#
PageA
protected void Submit_OnClick(object sender, EventArgs e)
{
string country = ddlCountry.SelectedItem.Text.Trim();
string city = ddlCity.SelectedItem.Text.Trim();
Response.Redirect("~/PageB.aspx?Country=" + country + "&City=" + city);
}
PageB
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
if (!string.IsNullOrEmpty(Request.QueryString["Country"]) && !string.IsNullOrEmpty(Request.QueryString["City"]))
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conString"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("SELECT EmployeeID,LastName+' '+FirstName Name,Country,City FROM Employees WHERE Country=@country AND City=@city ", con))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@country", Request.QueryString["Country"]);
cmd.Parameters.AddWithValue("@city", Request.QueryString["City"]);
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
sda.Fill(dt);
this.gvEmployee.DataSource = dt;
this.gvEmployee.DataBind();
}
}
}
}
}
}
VB.Net
PageA
Protected Sub Submit_OnClick(ByVal sender As Object, ByVal e As EventArgs)
Dim country As String = ddlCountry.SelectedItem.Text.Trim()
Dim city As String = ddlCity.SelectedItem.Text.Trim()
Response.Redirect("~/PageB.aspx?Country=" & country & "&City=" & city)
End Sub
PageB
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
If Not String.IsNullOrEmpty(Request.QueryString("Country")) AndAlso Not String.IsNullOrEmpty(Request.QueryString("City")) Then
Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("conString").ConnectionString)
Using cmd As SqlCommand = New SqlCommand("SELECT EmployeeID,LastName+' '+FirstName Name,Country,City FROM Employees WHERE Country=@country AND City=@city ", con)
cmd.CommandType = CommandType.Text
cmd.Parameters.AddWithValue("@country", Request.QueryString("Country"))
cmd.Parameters.AddWithValue("@city", Request.QueryString("City"))
Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
Me.gvEmployee.DataSource = dt
Me.gvEmployee.DataBind()
End Using
End Using
End Using
End If
End If
End Sub
Screenshot
