i want to make a seacrh page using c# in asp.net..
at the first page, i want to display all the record in a gridview..then,after user click a search button, the result will appear in another gridview..
what should i put to visible the previous gridview and display only the result..
here are my code:

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="search.aspx.cs" Inherits="search" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server">
    
        <asp:TextBox ID="catID" runat="server" MaxLength="1"></asp:TextBox><asp:Button ID="button2" runat="server" Text="Search" onclick="button2_Click" /><br />
        <asp:CompareValidator ID="CompareValidator1" runat="server" Text="Please Enter the Category ID" ControlToValidate="catID" Type="Integer" Operator="DataTypeCheck"></asp:CompareValidator>
                
            <asp:GridView ID="GridView1" runat="server" DataKeyNames="categoryID" AutoGenerateColumns="false">
                    <Columns>
                            <asp:BoundField DataField="categoryID" HeaderText="Category ID" ReadOnly="true" SortExpression="categoryID" />
                            <asp:BoundField DataField="CategoryName" HeaderText="Category Name" SortExpression="CategoryName" />
                            <asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
                    </Columns>
            </asp:GridView>
            
            <asp:GridView ID="GridView2" runat="server" DataKeyNames="categoryID" AutoGenerateColumns="false">
                    <Columns>
                            <asp:BoundField DataField="categoryID" HeaderText="Category ID" ReadOnly="true" SortExpression="categoryID" /> 
                            <asp:BoundField DataField="CategoryName" HeaderText="Category Name" SortExpression="CategoryName" />
                            <asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
                    </Columns>
            </asp:GridView>
            
</asp:Content>
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

public partial class search : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        catID.Focus();

        SqlConnection con;
        SqlCommand cmd;

        SqlDataAdapter da;
        DataSet ds = new DataSet();

        string cmdString = "SELECT * FROM Categories";

        con = new SqlConnection("Server=(local); Database=test; user id=sa;  password=itdept");
        cmd = new SqlCommand(cmdString, con);
        con.Open();

        da = new SqlDataAdapter(cmd);
        da.Fill(ds);
        GridView2.DataSource = ds;
        GridView2.DataBind();

        con.Close();  
    }

    protected void button2_Click(object sender, EventArgs e)
    {
        SqlConnection con;
        SqlCommand cmd;
        SqlDataAdapter da;
        DataSet ds = new DataSet();
    
        var cateID = catID.Text;
        string cmdString = "SELECT * FROM Categories WHERE categoryID = ' " + cateID + " ' ";

        con = new SqlConnection("Server=(local); Database=test; user id=sa;  password=itdept");
        cmd = new SqlCommand(cmdString, con);
        con.Open();

        da = new SqlDataAdapter(cmd);
        da.Fill(ds);

        GridView1.DataSource = ds;
        GridView1.DataBind();
                
        con.Close();  
       
    }
}
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.Sql;
using System.Data.SqlClient;

public partial class webTest : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("Server=(local); Database=test; user id=sa; password=itdept");
    SqlCommand cmd;
    SqlDataAdapter da;
    DataSet ds = new DataSet();

    protected void Page_Load(object sender, EventArgs e)
    {
        catID.Focus();
        if (!this.IsPostBack)
        {
            string cmdString = "SELECT * FROM Categories";
            //cmd = new SqlCommand(cmdString, con);//no need to use sqlcommand here.
            if (con.State == ConnectionState.Open)
                con.Close();//without this check it will throw exception if connection is already open.
            con.Open();
            da = new SqlDataAdapter(cmdString, con);
            if (ds.Tables["Catergory"] != null)
                ds.Tables["Catergory"].Clear();
            da.Fill(ds,"Catergory");
            GridView1.DataSource = ds;
            GridView1.DataMember = "Catergory";
            GridView1.DataBind();
            con.Close();
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string cmdString = "SELECT * FROM Categories where categoryID='"+catID.Text.Trim()+"' ";
        if (con.State == ConnectionState.Open)
            con.Close();//without this check it will throw exception if connection is already open.
        con.Open();
        da = new SqlDataAdapter(cmdString, con);
        if (ds.Tables["Catergory"] != null)
            ds.Tables["Catergory"].Clear();
        da.Fill(ds, "Catergory");
        GridView1.DataSource = ds;
        GridView1.DataMember = "Catergory";
        GridView1.DataBind();
        con.Close();
    }
}

You can try this code.... Hope this helps

wow..amazing..you solved it..
TQ very much =)

Hi

I have same problem.thanks for this code. It will help me to solve the issue of display and search using 2 gridview in 1page.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.