Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi All,

I need a help to display records in Gridview after clicking search button.
I am using asp.net and c#.net and sql server.
I have a web form with empid,empname textboxes with one Search button and below I have a Gridview to display search results.
I want to dipslay records fetching from EMP(empid,ename,....) table from database into the Gridview after clicking search button.
Can anyone Help me.
Thank you,
Posted

//code 4 search btn
protected void btnsearch_Click(object sender, EventArgs e)
{
SqlConnection con;
con = new SqlConnection("TypeConnectionStringHere");
con.Open();
string c = txtcity.Text;
string str;
str="select * from TableName where city=" + c + "";
//city is the column name in table
SqlCommand cmd;
cmd=new SqlCommand(str,con);
SqlDataReader dr;
dr=cmd.ExecuteReader();
if(dr.Read())
{

txtcity.Text=dr.GetString(0);
//if city is in 0 index of table
}
else
{
Response.Write("City does not exist");
txtc.Text="";
}
dr.Close();
con.Close();
}
 
Share this answer
 
when you click on search button than Get the data by query(filter the records by textbox value) then bind this data to gridview.
 
Share this answer
 
Create A Static Gridview in Aspx Page with All The Field You Required to Display Names In Asp BoundField Must Match with Db Data

ASP
<asp:gridview id="gdvStudio" runat="server" allowpaging="True" autogeneratecolumns="False" backcolor="White" bordercolor="Black" xmlns:asp="#unknown"></asp:gridview>


HTML
<columns>
</columns>


XML
<asp:BoundField DataField="EMPID"
    HeaderStyle-HorizontalAlign="Left" HeaderText="EMPID"
    InsertVisible="true" ItemStyle-HorizontalAlign="Left" ItemStyle-Width="300"
    ReadOnly="true" SortExpression="EMPID">
    <HeaderStyle HorizontalAlign="Left" />
    <ItemStyle HorizontalAlign="Left" />
</asp:BoundField>



XML
<asp:BoundField DataField="COLUMN2"
   HeaderStyle-HorizontalAlign="Left" HeaderText="COLUMN2"
    InsertVisible="true" ItemStyle-HorizontalAlign="Left" ItemStyle-Width="300"
     ReadOnly="true" SortExpression="COLUMN2">
    <HeaderStyle HorizontalAlign="Left" />
    <ItemStyle HorizontalAlign="Left" />
&lt;/asp:BoundField>


HTML





Create Your Grid View Like This and CodeBehind You Have to bind with this Gridview...


In Aspx.cs file


C#
DataTable dtsearch=new DataTable 
dtsearch = SearchStudio();
          
              if (dtsearch.Rows.Count > 0)
              {
                  pnlStudioInfo.Visible = true;
                  gdvStudio.DataSource = dtsearch;
                  ViewState["file"] = dtsearch;
                  gdvStudio.DataBind();
              }
 
Share this answer
 
Comments
jaipal0908 4-Jun-12 6:20am    
Hi Tony,
I designed the page with one search text box empid and search button and Gridview.
But I want the code in search button click to bind the Gridview.
Can you help me.
Tony Tom.k 12-Jun-12 5:59am    
Hi Jaipal You are using stored procedures or you are directly using select queries.....
Use This Code Inside Button Click

DataTable dtsearch=new DataTable
dtsearch = SearchStudio();

if (dtsearch.Rows.Count > 0)
{
pnlStudioInfo.Visible = true;
gdvStudio.DataSource = dtsearch;
ViewState["file"] = dtsearch;
gdvStudio.DataBind();
}
Tony Tom.k 12-Jun-12 6:00am    
IF its not getting triggered use this trigger Inside your
Aspx page....

<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnSearch" EventName="Click">
</Triggers>
This is a vast question to be answer,
If you have tried something and got some error then let us know. The people here will help you out from that.

Here you should connect to the database and the next process of searching the record can be done in two ways -
1. Either you write the SqlCommand at front end to search for the entries textboxes. Refer this -> http://asp-net-example.blogspot.in/2008/11/aspnet-sqlcommand-example-how-to-use.html[^]

2.Else you can write the procedure and call that procedure with the input parameter as the search criteria Refer this-http://forums.codeguru.com/showthread.php?threadid=373179[^]

you will get the result in the Dataset which you have to bind to the Grid view.
 
Share this answer
 
Please refer following links:
Display Large Amount of Data in GridView with Search Functionality[^] -This article explains how to display large amount of data and implement search functionality in GridView in ASP.NET
ASP.NET GridView with search option (SearchableGridView)[^] -An ASP.NET GridView with a search option in the footer.
GridView-Search Control[^] -Search control with basic/advanced modes for DataTable filtering capabilities
 
Share this answer
 
Comments
thatraja 4-Jun-12 15:57pm    
5!
Prasad_Kulkarni 5-Jun-12 0:08am    
Thank you thatRaja!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900