Saturday, May 11, 2013

How to Search Records in Gridview in Asp.net


Introduction: In this post I have try explain how to Search the Records in Gridview.
Description:
I have created a table name STUDENT_DETAIL and insert data into table.
STUDENT_ID
int
STUDENT_NAME
varchar(50)
STUDENT_ADDRESS
varchar(50)
STUDENT_CLASS
varchar(50)

STUDENT_ID is primary key.
Now open the Visual Studio>Go to File>New>Website. Add the Connectionstring in web.config file of website.
<configuration>
       <connectionStrings>
    <add name="connection" connectionString="Data Source=SYS-1F78031ED0A;Initial Catalog=TestBlog;Integrated Security=True"/>
       </connectionStrings>
       <system.web>
        <compilation debug="true" targetFramework="4.0" />
    </system.web>
</configuration>


After that add new web form to website, drag and drop the Gridview data control from Toolbox.
  <asp:TextBox ID="txtsearch" runat="server"></asp:TextBox>
&nbsp;&nbsp;
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Search" />
        <br />
    <asp:GridView ID="grdstudentdetail" runat="server" AutoGenerateColumns="False"
            DataKeyNames="STUDENT_ID">
            <Columns>
                <asp:TemplateField HeaderText="STUDENT NAME">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtname" runat="server" Text='<%# Eval("STUDENT_NAME") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblname" runat="server" Text='<%# Eval("STUDENT_NAME") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="STUDENT ADDRESS">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtaddress" runat="server" Text='<%# Eval("STUDENT_ADDRESS") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lbladdress" runat="server" Text='<%# Eval("STUDENT_ADDRESS") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="STUDENT CLASS">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtclass" runat="server" Text='<%# Eval("STUDENT_CLASS") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblclass" runat="server" Text='<%# Eval("STUDENT_CLASS") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                           </Columns>
            <PagerSettings PageButtonCount="2" />
        </asp:GridView>

Now go to .aspx.cs page.
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ToString());
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bindgrid();
        }
    }
    public void Bindgrid()
    {
        DataTable dt = new DataTable();
        try
        {
            SqlCommand cmd = new SqlCommand("BIND_GRID", con);
            SqlDataAdapter adp = new SqlDataAdapter(cmd);
            cmd.CommandType = CommandType.StoredProcedure;
                      
            adp.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                grdstudentdetail.DataSource = dt;
                grdstudentdetail.DataBind();
            }
            else
            {
                dt.Rows.Add(dt.NewRow());
                grdstudentdetail.DataSource = dt;
                grdstudentdetail.DataBind();
                int columncount = grdstudentdetail.Rows[0].Cells.Count;
                grdstudentdetail.Rows[0].Cells.Clear();
                grdstudentdetail.Rows[0].Cells.Add(new TableCell());
                grdstudentdetail.Rows[0].Cells[0].ColumnSpan = columncount;
                grdstudentdetail.Rows[0].Cells[0].Text = "No Records Available";
            }
        }
        catch (Exception ex)
        {
        }
        finally
        {
            dt.Clear();
            dt.Dispose();
        }

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            string serach = "select * from STUDENT_DETAIL where STUDENT_NAME like '%" + txtsearch.Text + "%'";
            SqlCommand cmd = new SqlCommand(serach, con);
            DataTable dt = new DataTable();
            SqlDataAdapter adp = new SqlDataAdapter(cmd);
            adp.Fill(dt);
            grdstudentdetail.DataSource = dt;
            grdstudentdetail.DataBind();
        }
        catch (Exception ex)
        {
        }
    }

In VB

Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration

Private con As New SqlConnection(ConfigurationManager.ConnectionStrings("connection").ToString())
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Bindgrid()
        End If
    End Sub
    Public Sub Bindgrid()
        Dim dt As New DataTable()
        Try
            Dim cmd As New SqlCommand("BIND_GRID", con)
            Dim adp As New SqlDataAdapter(cmd)
            cmd.CommandType = CommandType.StoredProcedure

            adp.Fill(dt)
            If dt.Rows.Count > 0 Then
                grdstudentdetail.DataSource = dt
                grdstudentdetail.DataBind()
            Else
                dt.Rows.Add(dt.NewRow())
                grdstudentdetail.DataSource = dt
                grdstudentdetail.DataBind()
                Dim columncount As Integer = grdstudentdetail.Rows(0).Cells.Count
                grdstudentdetail.Rows(0).Cells.Clear()
                grdstudentdetail.Rows(0).Cells.Add(New TableCell())
                grdstudentdetail.Rows(0).Cells(0).ColumnSpan = columncount
                grdstudentdetail.Rows(0).Cells(0).Text = "No Records Available"
            End If
        Catch ex As Exception
        Finally
            dt.Clear()
            dt.Dispose()
        End Try

    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            Dim serach As String = "select * from STUDENT_DETAIL where STUDENT_NAME like '%" + txtsearch.Text & "%'"
            Dim cmd As New SqlCommand(serach, con)
            Dim dt As New DataTable()
            Dim adp As New SqlDataAdapter(cmd)
            adp.Fill(dt)
            grdstudentdetail.DataSource = dt
            grdstudentdetail.DataBind()
        Catch ex As Exception
        End Try
    End Sub

Now run the project and check the result.

Related Articles on Gridview:

Ø  How to bind Gridview using Sqldataadapter, Datatable andQuery in Asp.net

Ø  How to highlight row on mouse hover in Gridview

Ø  How to edit and update Dropdownlist in Gridview datacontrol in Asp.net

Ø  How to Bind Gridview with Datareader in asp.net

Ø  How to Bind Gridview using Store Procedure, SqlDataAdapterand Datatable in Asp.net

Ø  How to use RadioButtonList control inside the Gridview inAsp.net

                         
  ØHow to use Fileupload control in Gridview inAsp.net

Is it helpful?

If yes post your comment to admire my work. You can like me on Facebook, Google+, Linkedin and Twitter via hit on Follow us Button and also can get update follow by Email.

No comments:

Post a Comment