Hi ukd,
I have created a sample. Check this example. Now please take its reference and correct your code.
For this sample I have used of NorthWind database that you can download using the link given below.
Download Northwind Database
Namespaces
C#
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
VB.Net
Imports System.Data.SqlClient
Imports System.Data
HTML
First.aspx
<div>
City:
<asp:DropDownList runat="server" ID="ddlCities">
</asp:DropDownList>
Country:
<asp:DropDownList runat="server" ID="ddlCountries">
</asp:DropDownList>
<br />
<asp:Button Text="Transfer" OnClick="Transfer" runat="server" />
</div>
Extract.aspx
<asp:GridView runat="server" ID="gvCustomers" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="CustomerID" HeaderText="CustomerID" />
<asp:BoundField DataField="ContactName" HeaderText="ContactName" />
<asp:BoundField DataField="City" HeaderText="City" />
<asp:BoundField DataField="Country" HeaderText="Country" />
</Columns>
</asp:GridView>
C#
First.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
PopulateDropDownList(ddlCities, "City", "City", "SELECT DISTINCT City FROM Customers WHERE City <> ''");
PopulateDropDownList(ddlCountries, "Country", "Country", "SELECT DISTINCT Country FROM Customers WHERE Country <> ''");
}
}
protected void Transfer(object sender, EventArgs e)
{
if (ddlCountries.SelectedItem.Text.ToLower() != "select" && ddlCities.SelectedItem.Text.ToLower() != "select")
{
Response.Redirect("Extract.aspx?Country=" + ddlCountries.SelectedValue + "&City=" + ddlCities.SelectedValue);
}
}
private void PopulateDropDownList(DropDownList ddl, string dataTextField, string dataValueField, string query)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query, con))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
DataSet ds = new DataSet();
da.Fill(ds);
ddl.DataTextField = dataTextField;
ddl.DataValueField = dataValueField;
ddl.DataSource = ds;
ddl.DataBind();
ddl.Items.Insert(0, new System.Web.UI.WebControls.ListItem("Select", "0"));
}
}
}
}
Extract.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(Request.QueryString["Country"]) && !string.IsNullOrEmpty(Request.QueryString["City"]))
{
string country = Request.QueryString["Country"];
string city = Request.QueryString["City"];
Response.Write(string.Format("Country is : <b>{0}</b> <br/> City is : <b>{1}</b>", country, city));
string str = "SELECT CustomerID,ContactName,City,Country FROM Customers WHERE City = '" + city + "' AND Country = '" + country + "'";
DataTable dt = GetDataTable(str);
if (dt.Rows.Count > 0)
{
gvCustomers.DataSource = dt;
gvCustomers.DataBind();
}
else
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('No record found as per your criteria!')", true);
}
}
}
private DataTable GetDataTable(string query)
{
DataSet ds = new DataSet();
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query, con))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
da.Fill(ds);
}
}
}
return ds.Tables[0];
}
VB.Net
First.aspx.vb
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
PopulateDropDownList(ddlCities, "City", "City", "SELECT DISTINCT City FROM Customers WHERE City <> ''")
PopulateDropDownList(ddlCountries, "Country", "Country", "SELECT DISTINCT Country FROM Customers WHERE Country <> ''")
End If
End Sub
Protected Sub Transfer(ByVal sender As Object, ByVal e As EventArgs)
If ddlCountries.SelectedItem.Text.ToLower() <> "select" AndAlso ddlCities.SelectedItem.Text.ToLower() <> "select" Then
Response.Redirect("Extract.aspx?Country=" & ddlCountries.SelectedValue & "&City=" + ddlCities.SelectedValue)
End If
End Sub
Private Sub PopulateDropDownList(ByVal ddl As DropDownList, ByVal dataTextField As String, ByVal dataValueField As String, ByVal query As String)
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand(query, con)
Using da As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim ds As DataSet = New DataSet()
da.Fill(ds)
ddl.DataTextField = dataTextField
ddl.DataValueField = dataValueField
ddl.DataSource = ds
ddl.DataBind()
ddl.Items.Insert(0, New System.Web.UI.WebControls.ListItem("Select", "0"))
End Using
End Using
End Using
End Sub
Extract.aspx.vb
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not String.IsNullOrEmpty(Request.QueryString("Country")) AndAlso Not String.IsNullOrEmpty(Request.QueryString("City")) Then
Dim country As String = Request.QueryString("Country")
Dim city As String = Request.QueryString("City")
Response.Write(String.Format("Country is : <b>{0}</b> <br/> City is : <b>{1}</b>", country, city))
Dim str As String = "SELECT CustomerID,ContactName,City,Country FROM Customers WHERE City = '" & city & "' AND Country = '" & country & "'"
Dim dt As DataTable = GetDataTable(str)
If dt.Rows.Count > 0 Then
gvCustomers.DataSource = dt
gvCustomers.DataBind()
Else
ClientScript.RegisterClientScriptBlock(Me.[GetType](), "", "alert('No record found as per your criteria!')", True)
End If
End If
End Sub
Private Function GetDataTable(ByVal query As String) As DataTable
Dim ds As DataSet = New DataSet()
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand(query, con)
Using da As SqlDataAdapter = New SqlDataAdapter(cmd)
da.Fill(ds)
End Using
End Using
End Using
Return ds.Tables(0)
End Function
Screenshot
