0

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

Edited by deeptakshd: n/a

2
Contributors
6
Replies
9
Views
7 Years
Discussion Span
Last Post by dnanetwork
0
<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

0

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..?

Edited by dnanetwork: n/a

0

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

0

<%#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.......

Edited by deeptakshd: n/a

0

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 :)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.