i created a page for searching records from database it is working well for repeater control but i want to display result in gridview......plz help

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /><asp:Repeater ID="Repeater1" runat="server">
    <HeaderTemplate><table width="100%"><tr><th>NAME</th><th>LAST_NAME</th><th>EMAIL</th></tr></HeaderTemplate>
         <ItemTemplate>
            <tr><td><%#DataBinder.Eval(Container.DataItem, "fname")%></a></td>
            <td><%#DataBinder.Eval(Container.DataItem, "lname")%></td>
            <td><%#DataBinder.Eval(Container.DataItem, "email")%></td></tr>
         </ItemTemplate>
         <FooterTemplate></table></FooterTemplate>
    </asp:Repeater>

stored procedure:

CREATE PROCEDURE spSearchByString @Search_String varchar(50) AS SELECT     [db_main].fname, [db_main].lname, [db_main].email
                                                                                            FROM         [db_main]
                                                                                           WHERE     ([db_main].fname LIKE '%' + @Search_String + '%' OR
                                                                                                                                                                                 [db_main].lname LIKE '%' + @Search_String + '%')
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.SqlClient;

public partial class Default3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["searchString"] != null)
        {
            DisplaySearchResults(Request.QueryString["searchString"]);
        }
    }
    public void DisplaySearchResults(string strSearch)

    {
        string conString = ConfigurationManager.ConnectionStrings["abcConnectionString"].ConnectionString;
        SqlCommand cmd = new SqlCommand("spSearchByString", new SqlConnection(conString));
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@Search_String", strSearch);
        cmd.Connection.Open();

        Repeater1.DataSource = cmd.ExecuteReader();
        Repeater1.DataBind();
       

        cmd.Connection.Close();
        cmd.Connection.Dispose();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default3.aspx?searchString=" + Server.UrlEncode(TextBox1.Text));
    }
}

stored pr

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /><asp:Repeater ID="Repeater1" runat="server">
    <HeaderTemplate><table width="100%"><tr><th>NAME</th><th>LAST_NAME</th><th>EMAIL</th></tr></HeaderTemplate>
         <ItemTemplate>
            <tr><td><%#DataBinder.Eval(Container.DataItem, "fname")%></a></td>
            <td><%#DataBinder.Eval(Container.DataItem, "lname")%></td>
            <td><%#DataBinder.Eval(Container.DataItem, "email")%></td></tr>
         </ItemTemplate>
         <FooterTemplate></table></FooterTemplate>
    </asp:Repeater>
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.SqlClient;

public partial class Default3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["searchString"] != null)
        {
            DisplaySearchResults(Request.QueryString["searchString"]);
        }
    }
    public void DisplaySearchResults(string strSearch)

    {
        string conString = ConfigurationManager.ConnectionStrings["abcConnectionString"].ConnectionString;
        SqlCommand cmd = new SqlCommand("spSearchByString", new SqlConnection(conString));
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@Search_String", strSearch);
        cmd.Connection.Open();

        Repeater1.DataSource = cmd.ExecuteReader();
        Repeater1.DataBind();
       

        cmd.Connection.Close();
        cmd.Connection.Dispose();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default3.aspx?searchString=" + Server.UrlEncode(TextBox1.Text));
    }
}
ALTER PROCEDURE spSearchByString @Search_String varchar(50) AS SELECT     [db_main].fname, [db_main].lname, [db_main].email
                                                                                                                                                           FROM         [db_main]
                                                                                                                                                           WHERE     ([db_main].fname LIKE '%' + @Search_String + '%' OR
                                                                                                                                                                                 [db_main].lname LIKE '%' + @Search_String + '%')

plz help

so wot's the problem with gridview..
it would the same ...just like you created ItemTemplate in Repeater and
same would be the case for Gridview..create a itemTemplate and done..

i dnt understand wot is your problem..?

sir i tried the same ....but unable to do so..........can u plz write the part of d code need to be changed,,,,thank u

<%#DataBinder.Eval(Container.DataItem, "fname")%>

when i use this inside gridview control ,error is generated

"Error 4 C:\Users\Deeptaksh Dargan\Documents\Visual Studio 2005\WebSites\ContactBook\Default3.aspx: ASP.NET runtime error: Code blocks are not supported in this context. C:\Users\Deeptaksh Dargan\Documents\Visual Studio 2005\WebSites\ContactBook\Default3.aspx 17 1 C:\...\ContactBook\
"


plz help .its urgent.......

thank you for being kind n helping me solving d problem...........now the major part of the problem is solved but still can u please help me out to change the header part of gridveiw as it has been displaying the column name of the db table........thank u :)

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.