Get and search records from database and show it in gridview

Introduction

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.
<div>  
   <table>
    <tr>
    <td> 
       Search
        </td>
        <td>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        </td>
        <td> 
        <asp:Button ID="Button1" runat="server" Text="Go" onclick="Button1_Click" />
        </td>
        
        </tr>
 
</table>
<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" />
    </asp:GridView> 
    </div>
  • Now add following code inside index.aspx.cs file.

Index.aspx.cs

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

        protected void Page_Load(object sender, EventArgs e)
        {
            BindGrid();  
        }
private void BindGrid()
        {
            conn.Open();
            string query = "select * from student where name like '" + TextBox1.Text + "%'";
            SqlDataAdapter da = new SqlDataAdapter(query, conn);
            DataSet ds = new DataSet();           
            da.Fill(ds);
            GridView1.DataSource = ds;
            if (ds.Tables[0].Rows.Count == 0)        
            {
             lblmsg.Text = "No records found";
             GridView1.DataBind();                
            }
            else {
                
                GridView1.DataBind();
                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