Get and search records from database and show it in gridview


In this article, we will do search functionality and show records in gridview. I will use visual studio 2012 framework and SQL server 2008 R2 for this demo.

So, before opening visual studio let’s create and populate student table using following script.

Student table script

Create table student
Id varchar(50),
Name varchar(50),
Gender varchar(10),
City varchar(50),
Insert into student values(1,'Sara','Female','Chennai')
Insert into student values(2,'David','Male','Sydeny')
Insert into student values(3,'Dora','Female','New York')
Insert into student values(4,'Maya','Female','London')
Insert into student values(5,'Vikram', 'Male','Mumbai')
Insert into student values(6,'Nikhil','Male','Delhi')

Let’s start for this demo step by step using Asp.Net.

Step 1: Create Asp.Net project, by using following steps.

  • Open Visual Studio.
  • Click on File -> New -> Project.
  • In this project, I am using C# as a programming language, but you can also choose VISUAL BASIC if you know syntaxes of VB.
  • Inside Visual C# -> Web -> Project -> Select Asp.Net Empty Web Application -> Name your project as GridViewDemo -> Select Ok

Step 2 : After creating Asp.Net empty project, you will find project files inside ‘solution explorer’ on your right hand side.

  • Open solution explorer

  • Right click on your project name -> Add -> New Item

  • Add new item – GridviewDemo -> Web > Webform – > Rename the file to index.aspx

  • Now Index.aspx file is added to your solution explorer.

Step 3 : Write following code inside Index.aspx and Index.aspx.cs files

  • Copy and paste following code in between form element of your index.aspx.
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Go" onclick="Button1_Click" />
<table><tr><td><p><asp:Label ID="Label2" runat="server" Text="label"></asp:Label>  </p></td></tr></table>
<asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Vertical" >
    <AlternatingRowStyle BackColor="#DCDCDC" />
    <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
    <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
    <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
    <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
    <SortedAscendingCellStyle BackColor="#F1F1F1" />
    <SortedAscendingHeaderStyle BackColor="#0000A9" />
    <SortedDescendingCellStyle BackColor="#CAC9C9" />
    <SortedDescendingHeaderStyle BackColor="#000065" />
  • Now add following code inside index.aspx.cs file.


private SqlConnection conn = new SqlConnection("Data Source=TechStudy-PC;Integrated Security=true;Initial Catalog=Sample");

        protected void Page_Load(object sender, EventArgs e)
private void BindGrid()
            string query = "select * from student where name like '" + TextBox1.Text + "%'";
            SqlDataAdapter da = new SqlDataAdapter(query, conn);
            DataSet ds = new DataSet();           
            GridView1.DataSource = ds;
            if (ds.Tables[0].Rows.Count == 0)        
             lblmsg.Text = "No records found";
            else {
                lblmsg.Text = "";
protected void Button1_Click(object sender, EventArgs e)

  • Do not forget to add follwing 2 namespaces.
using System.Data;
using System.Data.SqlClient;

Step 4 : Press F5 and see result

1. List of students

2. Search record

3. Record does not exist

Leave a Comment