search engine and displaying search results with gridview

 Hi all,

I am a newbie with ASP.NET.
I have a gridview that display data from the database.

I want to implement a search engine. When the user enters a keyword in the textbox then click on the search button the result will be displayed
to the gridview.

How do I do this? How do I implement a search engine so that when the user enters a keyword on the textbox and hit the search button the data will then be displayed in the gridview?

 

Here's is my code. The texbox and search button are highlighted in bold case:

 

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
    </div>
    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
        DataSourceID="ObjectDataSource1" DataTextField="State" DataValueField="State">
    </asp:DropDownList>
    <asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
        OldValuesParameterFormatString="original_{0}" SelectMethod="GetStates"
        TypeName="DataSet1TableAdapters.StatesTableAdapter"></asp:ObjectDataSource>
    <br />
    <br />
   
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:Button ID="Search_btn" runat="server" Text="Search" />
   
    <asp:GridView ID="GridView1" runat="server" AllowPaging="True"
        AllowSorting="True" AutoGenerateColumns="False" CellPadding="4"
        DataKeyNames="AuthorID" DataSourceID="ObjectDataSource2" ForeColor="#333333"
        GridLines="None">
        <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
        <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
        <Columns>
            <asp:BoundField DataField="AuthorID" HeaderText="AuthorID"
                InsertVisible="False" ReadOnly="True" SortExpression="AuthorID" />
            <asp:BoundField DataField="FirstName" HeaderText="FirstName"
                SortExpression="FirstName" />
            <asp:BoundField DataField="LastName" HeaderText="LastName"
                SortExpression="LastName" />
            <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
            <asp:BoundField DataField="State" HeaderText="State" SortExpression="State" />
            <asp:HyperLinkField DataNavigateUrlFields="AuthorID"
                DataNavigateUrlFormatString="Details.aspx?AuthorID={0}" HeaderText="Details"
                Text="detail" />
        </Columns>
        <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
        <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
        <AlternatingRowStyle BackColor="White" />
    </asp:GridView>
    <asp:ObjectDataSource ID="ObjectDataSource2" runat="server"
        DeleteMethod="Delete" InsertMethod="Insert"
        OldValuesParameterFormatString="original_{0}" SelectMethod="GetData"
        TypeName="DataSet1TableAdapters.AuthorsTableAdapter" UpdateMethod="Update">
        <DeleteParameters>
            <asp:Parameter Name="Original_AuthorID" Type="Int32" />
        </DeleteParameters>
        <UpdateParameters>
            <asp:Parameter Name="FirstName" Type="String" />
            <asp:Parameter Name="LastName" Type="String" />
            <asp:Parameter Name="City" Type="String" />
            <asp:Parameter Name="State" Type="String" />
            <asp:Parameter Name="Zip" Type="String" />
            <asp:Parameter Name="Phone" Type="String" />
            <asp:Parameter Name="Original_AuthorID" Type="Int32" />
        </UpdateParameters>
        <SelectParameters>
            <asp:ControlParameter ControlID="DropDownList1" Name="State"
                PropertyName="SelectedValue" Type="String" />
        </SelectParameters>
        <InsertParameters>
            <asp:Parameter Name="FirstName" Type="String" />
            <asp:Parameter Name="LastName" Type="String" />
            <asp:Parameter Name="City" Type="String" />
            <asp:Parameter Name="State" Type="String" />
            <asp:Parameter Name="Zip" Type="String" />
            <asp:Parameter Name="Phone" Type="String" />
        </InsertParameters>
    </asp:ObjectDataSource>
    </form>
</body>
</html>


 
0
vincentcartero
1/24/2008 5:21:36 AM
asp.net.presentation-controls 72751 articles. 3 followers. Follow

15 Replies
744 Views

Similar Articles

[PageSpeed] 30
Get it on Google Play
Get it on Apple App Store

use sql server fulltextsearch feature  which will be much efficient . for more information on this read the following

http://www.freevbcode.com/ShowCode.asp?ID=4224

http://www.sqlteam.com/article/using-sql-server-2005-fulltext-search-from-aspnet-20 

 


http://www.Aneef.net
0
shadowcodes
1/24/2008 5:54:12 AM

Hi shadowcodes, thanks for the help but what I want to do is to solve the problem with my code. I do not want to implement the Fulltextsearchcode describe in the article.

0
vincentcartero
1/24/2008 6:09:56 AM

Hi:

  What's the usage of the DropDownList? You can just try:

   <SelectParameters>
            <asp:ControlParameter ControlID="DropDownList1" Name="State"
                PropertyName="SelectedValue" Type="String" />

    <asp:ControlParameter ControlID="TextBox1" Name="keyword"
                PropertyName="Text" Type="String" />

        </SelectParameters>

  In the GetData method just add one more parameter string keyword and return the expected DataTable.

Regards


Sincerely,
Allen Chen
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
0
Allen
1/25/2008 7:12:03 AM

you can do the following ...

1st make a method to return a datatable to you [this datatable will contain your query result]

public DataTable searchEngine(String text)
{
String mySQL;

mySQL =
@"select column1, column2, column3, column4
from SecurityFileManager
WHERE lower(column1) LIKE '%' + @text + '%'"
;                                                            <-- where text = the text taken from you textbox control
SqlCommand command = new SqlCommand(mySQL, connection);
command.Parameters.AddWithValue(
"text", text);
SqlDataAdapter adptr = new SqlDataAdapter(command);
adptr.Fill(ds);

return ds.Tables[0];
}


now bind its result to your gridView

GridView1.DataSource =  searchEngine(text);
GridView1.DataBind();


Hope that helps ...


Please, “Mark as Answer” the post that helped you.
0
XGmarX
1/25/2008 7:57:32 AM

Hi all,


I want to make my problem simpler. So instead of searching the whole table, I just want to search by FirstName.

I created a dataset and created a method which has the following query:

SELECT        Authors_tbl.*
FROM            Authors_tbl
WHERE        (Firstname= @Firstname)

 

My problem now is how do I make the textbox and the search button so that when the user type in the FirstName in the textbox and then click on the button

the data will then be presented in the gridview. 

The algorithmn for this will be something like:

User type in FirstName

User hit the Search button

Data is presented in the gridview ( I know I have to use binding for this but how?)

 

Like I said I am a complete newbie with ASP.NET. Hope this makes sense.
 

0
vincentcartero
1/25/2008 11:33:47 AM

 Hi Allen Chen

I tried adding a new parameter string and I named it keyword in the GetData method and still not able to solve the problem.

How exactly do i add one more parameter and return the expected DataTable

0
vincentcartero
1/25/2008 3:17:10 PM

Hi:

vincentcartero:

Hi Allen Chen

I tried adding a new parameter string and I named it keyword in the GetData method and still not able to solve the problem.

How exactly do i add one more parameter and return the expected DataTable

  The logic is just as what XGmarX  mentioned. You can use SqlDataAdapter to fill a DataTable and return it in the select method of your BLL.

Regards


Sincerely,
Allen Chen
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
0
Allen
1/28/2008 9:03:43 AM

 hi XGmarX i tried the following with my code

   

    protected void search_btn_Click(object sender, EventArgs e)
{
public DataTable searchEngine(String text)
{
text = TextBox1.Text
String mySQL;

mySQL = @"ContactID, FirstName, LastName, City, Country, Address, PostCode,Telephone, Email
from SecurityFileManager
WHERE lower(ContactID) LIKE '%' + @text + '%'";
SqlCommand command = new SqlCommand(mySQL, connection);
command.Parameters.AddWithValue("text", text);
SqlDataAdapter adptr = new SqlDataAdapter(command);
adptr.Fill(ds);

return ds.Tables[0];
}


GridView1.DataSource = searchEngine(text);
GridView1.DataBind();
}



But I get an error message saying:

Compilation Error

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS1513: } expected

Source Error:


Line 24:     }
Line 25: protected void search_btn_Click(object sender, EventArgs e)
Line 26: {
Line 27: public DataTable searchEngine(String text)
Line 28: {


Source File: c:\WebSites\contact2k8\Default.aspx.cs    Line: 26

 

 

My curly brackets are correct I counted them.
 

0
vincentcartero
1/28/2008 1:20:46 PM

Hi:

  Your code should be like this:

   <SelectParameters>
            <asp:ControlParameter ControlID="DropDownList1" Name="State"
                PropertyName="SelectedValue" Type="String" />

    <asp:ControlParameter ControlID="TextBox1" Name="text"
                PropertyName="Text" Type="String" />

        </SelectParameters>

  The select method in BLL(Here an extra parameter is State which matches the select parameter defined in your aspx):

    public DataTable GetData(string text,string State)
        {

        String mySQL;

        mySQL = @"ContactID, FirstName, LastName, City, Country, Address, PostCode,Telephone, Email
        from SecurityFileManager
        WHERE lower(ContactID) LIKE '%' + @text + '%'";                                                          
        SqlCommand command = new SqlCommand(mySQL, connection);
        command.Parameters.AddWithValue("text", text);
        SqlDataAdapter adptr = new SqlDataAdapter(command);
        adptr.Fill(ds);

        return ds.Tables[0];
        }

 

Regards


Sincerely,
Allen Chen
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
0
Allen
1/29/2008 1:32:27 AM
Line 24:     }
Line 25:     protected void search_btn_Click(object sender, EventArgs e)
Line 26:     {
Line 27:         public DataTable searchEngine(String text)
Line 28:         {
 
**
The problem isnt’ with the searchEngine(String text) method, check the curly braces for the search_btn_Click event.
** Error CS1513 occure when the compiler expect a closing curly brace } and it wasn't found.


** Please, let me know if that helps.
  

 


Please, “Mark as Answer” the post that helped you.
0
XGmarX
1/29/2008 2:22:25 AM
Hi i still get the same error. I will show you my Default.aspx file and my Default.aspx.cs file. 

//Here is my Default.aspx file 

<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<asp:Content ID="Content1" runat="server" ContentPlaceHolderID="ContentPlaceHolder1">
    <asp:LoginView ID="LoginView1" runat="server">
    <LoggedInTemplate>
        Welcome back
        <br />
        <asp:LoginName ID="LoginName1" runat="server" />
        <br />
        <asp:LoginStatus ID="LoginStatus1" runat="server" />
        <br />
        <a href="ChangePassword.aspx">ChangePassword.aspx</a><br />
        <br />
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
            DataSourceID="ObjectDataSource1" DataTextField="Country"
            DataValueField="Country">
        </asp:DropDownList>
        <asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
            OldValuesParameterFormatString="original_{0}" SelectMethod="GetCountry"
            TypeName="DataSet1TableAdapters.CountryListTableAdapter">
        </asp:ObjectDataSource>
        <br />
        <br />
        <asp:TextBox ID="TextBox1" runat="server" ontextchanged="TextBox1_TextChanged1"></asp:TextBox>
        <asp:Button ID="search_btn" runat="server" onclick="search_btn_Click"
            Text="Search" />

        <br />
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px"
            CellPadding="3" DataKeyNames="ContactID" DataSourceID="ObjectDataSource2"
            GridLines="Vertical" AllowPaging="True" AllowSorting="True">
            <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
            <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
            <Columns>
                <asp:BoundField DataField="ContactID" HeaderText="ContactID"
                    InsertVisible="False" ReadOnly="True" SortExpression="ContactID" />
                <asp:BoundField DataField="FirstName" HeaderText="FirstName"
                    SortExpression="FirstName" />
                <asp:BoundField DataField="LastName" HeaderText="LastName"
                    SortExpression="LastName" />
                <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
                <asp:BoundField DataField="Country" HeaderText="Country"
                    SortExpression="Country" />
                <asp:BoundField DataField="Address" HeaderText="Address"
                    SortExpression="Address" />
                <asp:BoundField DataField="PostCode" HeaderText="PostCode"
                    SortExpression="PostCode" />
                <asp:BoundField DataField="Telephone" HeaderText="Telephone"
                    SortExpression="Telephone" />
                <asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />
                <asp:HyperLinkField DataNavigateUrlFields="ContactID"
                    DataNavigateUrlFormatString="Details.aspx?ContactID={0}" HeaderText="Details"
                    Text="detail" />
            </Columns>
            <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
            <AlternatingRowStyle BackColor="#DCDCDC" />
        </asp:GridView>
        <asp:ObjectDataSource ID="ObjectDataSource2" runat="server"
            DeleteMethod="Delete" InsertMethod="Insert"
            OldValuesParameterFormatString="original_{0}" SelectMethod="GetData"
            TypeName="DataSet1TableAdapters.Contact_tblTableAdapter" UpdateMethod="Update">
            <DeleteParameters>
                <asp:Parameter Name="Original_ContactID" Type="Int32" />
            </DeleteParameters>
            <UpdateParameters>
                <asp:Parameter Name="FirstName" Type="String" />
                <asp:Parameter Name="LastName" Type="String" />
                <asp:Parameter Name="City" Type="String" />
                <asp:Parameter Name="Country" Type="String" />
                <asp:Parameter Name="Address" Type="String" />
                <asp:Parameter Name="PostCode" Type="String" />
                <asp:Parameter Name="Telephone" Type="String" />
                <asp:Parameter Name="Email" Type="String" />
                <asp:Parameter Name="Original_ContactID" Type="Int32" />
            </UpdateParameters>
            <SelectParameters>
                <asp:ControlParameter ControlID="DropDownList1" Name="Country"
                    PropertyName="SelectedValue" Type="String" />
            </SelectParameters>
            <InsertParameters>
                <asp:Parameter Name="FirstName" Type="String" />
                <asp:Parameter Name="LastName" Type="String" />
                <asp:Parameter Name="City" Type="String" />
                <asp:Parameter Name="Country" Type="String" />
                <asp:Parameter Name="Address" Type="String" />
                <asp:Parameter Name="PostCode" Type="String" />
                <asp:Parameter Name="Telephone" Type="String" />
                <asp:Parameter Name="Email" Type="String" />
            </InsertParameters>
        </asp:ObjectDataSource>
        <br />
        <br />
    </LoggedInTemplate>
    <AnonymousTemplate>
        You are not logged in.<br />
        <br />
        <a href="Login.aspx">Login.aspx</a><br />
        <a href="CreateAccount.aspx">CreateAccount.asp</a>
    </AnonymousTemplate>
</asp:LoginView>
</asp:Content>

 

 

//Here is my Default.aspx.cs file:

using System;
using System.Data;
using System.Configuration;
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;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {

    }
    protected void TextBox1_TextChanged1(object sender, EventArgs e)
    {

    }
    protected void search_btn_Click(object sender, EventArgs e)
    {
       public DataTable GetData(string text,string State)
        {

        String mySQL;

        mySQL = @"ContactID, FirstName, LastName, City, Country, Address, PostCode,Telephone, Email
        from SecurityFileManager
        WHERE lower(ContactID) LIKE '%' + @text + '%'";                                                         
        SqlCommand command = new SqlCommand(mySQL, connection);
        command.Parameters.AddWithValue("text", text);
        SqlDataAdapter adptr = new SqlDataAdapter(command);
        adptr.Fill(ds);

        return ds.Tables[0];
        }

      
       GridView1.DataSource =  searchEngine(text);
       GridView1.DataBind();
    }

}

 
0
vincentcartero
1/29/2008 3:21:37 PM
protected void search_btn_Click(object sender, EventArgs e) { 

String state = DropDownList1.SelectedItem.Text;

String text = TextBox1.Text;

GridView1.DataSource = searchData(text, state);

GridView1.DataBind();

} //Closing the search_btn_Click Event

 

//Starting the private method that will return the datatable...  

private DataTable searchData(string text, string State) {

String mySQL;mySQL = @"ContactID, FirstName, LastName, City, Country, Address, PostCode,Telephone, Email

from yourTableName

WHERE lower(ContactID) LIKE '%' + @text + '%'" + @"AND state = @state";

SqlCommand command = new SqlCommand(mySQL, connection); command.Parameters.AddWithValue("text", text);

command.Parameters.AddWithValue("state", state);

SqlDataAdapter adptr = new SqlDataAdapter(command);

adptr.Fill(ds);

return ds.Tables[0];

}

 

** Please, let me know if it helps ...


Please, “Mark as Answer” the post that helped you.
0
XGmarX
1/29/2008 3:35:31 PM

 

Hi now I get this error:

Compilation Error

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0103: The name 'TextBox1' does not exist in the current context

Source Error:


Line 41:  //String state = DropDownList1.SelectedItem.Text; //<-- The DropDown control (Replace with your own control)
Line 42:
Line 43: String text = TextBox1.Text; //<-- The TextBox where the user will insert the text (Replace with your own control)
Line 44:
Line 45: GridView1.DataSource = searchData(text);

Source File: c:\WebSites\contact2k8\Default.aspx.cs    Line: 43


The name of my textbox is Textbox1 but is the error message trying to say that Textbox1 doesn't exist?

Here is my source code: 

 //Default.aspx

 <%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<asp:Content ID="Content1" runat="server" ContentPlaceHolderID="ContentPlaceHolder1">
    <asp:LoginView ID="LoginView1" runat="server">
    <LoggedInTemplate>
        Welcome back
        <br />
        <asp:LoginName ID="LoginName1" runat="server" />
        <br />
        <asp:LoginStatus ID="LoginStatus1" runat="server" />
        <br />
        <a href="ChangePassword.aspx">ChangePassword.aspx</a><br />
        <br />
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
            DataSourceID="ObjectDataSource1" DataTextField="Country"
            DataValueField="Country">
        </asp:DropDownList>
        <asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
            OldValuesParameterFormatString="original_{0}" SelectMethod="GetCountry"
            TypeName="DataSet1TableAdapters.CountryListTableAdapter">
        </asp:ObjectDataSource>
        <br />
        <br />
        <asp:TextBox ID="TextBox1" runat="server" ontextchanged="TextBox1_TextChanged1"></asp:TextBox>
        <asp:Button ID="search_btn" runat="server" onclick="search_btn_Click"
            Text="Search" />
        <br />
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px"
            CellPadding="3" DataKeyNames="ContactID" DataSourceID="ObjectDataSource2"
            GridLines="Vertical" AllowPaging="True" AllowSorting="True">
            <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
            <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
            <Columns>
                <asp:BoundField DataField="ContactID" HeaderText="ContactID"
                    InsertVisible="False" ReadOnly="True" SortExpression="ContactID" />
                <asp:BoundField DataField="FirstName" HeaderText="FirstName"
                    SortExpression="FirstName" />
                <asp:BoundField DataField="LastName" HeaderText="LastName"
                    SortExpression="LastName" />
                <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
                <asp:BoundField DataField="Country" HeaderText="Country"
                    SortExpression="Country" />
                <asp:BoundField DataField="Address" HeaderText="Address"
                    SortExpression="Address" />
                <asp:BoundField DataField="PostCode" HeaderText="PostCode"
                    SortExpression="PostCode" />
                <asp:BoundField DataField="Telephone" HeaderText="Telephone"
                    SortExpression="Telephone" />
                <asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />
                <asp:HyperLinkField DataNavigateUrlFields="ContactID"
                    DataNavigateUrlFormatString="Details.aspx?ContactID={0}" HeaderText="Details"
                    Text="detail" />
            </Columns>
            <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
            <AlternatingRowStyle BackColor="#DCDCDC" />
        </asp:GridView>
        <asp:ObjectDataSource ID="ObjectDataSource2" runat="server"
            DeleteMethod="Delete" InsertMethod="Insert"
            OldValuesParameterFormatString="original_{0}" SelectMethod="GetData"
            TypeName="DataSet1TableAdapters.Contact_tblTableAdapter" UpdateMethod="Update">
            <DeleteParameters>
                <asp:Parameter Name="Original_ContactID" Type="Int32" />
            </DeleteParameters>
            <UpdateParameters>
                <asp:Parameter Name="FirstName" Type="String" />
                <asp:Parameter Name="LastName" Type="String" />
                <asp:Parameter Name="City" Type="String" />
                <asp:Parameter Name="Country" Type="String" />
                <asp:Parameter Name="Address" Type="String" />
                <asp:Parameter Name="PostCode" Type="String" />
                <asp:Parameter Name="Telephone" Type="String" />
                <asp:Parameter Name="Email" Type="String" />
                <asp:Parameter Name="Original_ContactID" Type="Int32" />
            </UpdateParameters>
            <SelectParameters>
                <asp:ControlParameter ControlID="DropDownList1" Name="Country"
                    PropertyName="SelectedValue" Type="String" />
            </SelectParameters>
            <InsertParameters>
                <asp:Parameter Name="FirstName" Type="String" />
                <asp:Parameter Name="LastName" Type="String" />
                <asp:Parameter Name="City" Type="String" />
                <asp:Parameter Name="Country" Type="String" />
                <asp:Parameter Name="Address" Type="String" />
                <asp:Parameter Name="PostCode" Type="String" />
                <asp:Parameter Name="Telephone" Type="String" />
                <asp:Parameter Name="Email" Type="String" />
            </InsertParameters>
        </asp:ObjectDataSource>
        <br />
        <br />
    </LoggedInTemplate>
    <AnonymousTemplate>
        You are not logged in.<br />
        <br />
        <a href="Login.aspx">Login.aspx</a><br />
        <a href="CreateAccount.aspx">CreateAccount.asp</a>
    </AnonymousTemplate>
</asp:LoginView>
</asp:Content>

 

 

 

 //Defaul.aspx.cs

using System;
using System.Data;
using System.Configuration;
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;

 public partial class _Default : System.Web.UI.Page

{
protected void Page_Load(object sender, EventArgs e)

{

}
protected void TextBox1_TextChanged(object sender, EventArgs e)

{

}

 

 




protected void TextBox1_TextChanged1(object sender, EventArgs e)

{

}
protected void search_btn_Click(object sender, EventArgs e)

{


String text = TextBox1.Text; //<-- The TextBox where the user will insert the text (Replace with your own control)

GridView1.DataSource = searchData(text);

GridView1.DataBind();

}

 
private DataTable searchData(string text)

{
String mySQL;mySQL = @"ContactID, FirstName, LastName, City, Country, Address, PostCode,Telephone, Email

from yourTableName

WHERE lower(ContactID) LIKE '%' + @text + '%'";SqlCommand command = new SqlCommand(mySQL, connection);

command.Parameters.AddWithValue(
"text", text);SqlDataAdapter adptr = new SqlDataAdapter(command);

adptr.Fill(ds);
return ds.Tables[0];

}

}
 

0
vincentcartero
1/29/2008 4:15:17 PM
protected void search_btn_Click(object sender, EventArgs e) { 

String state = DropDownList1.SelectedItem.Text;                   // <---- replace this with your dropdown name

String text = TextBox1.Text;                                                // <---- replace this with your textbox name

GridView1.DataSource = searchData(text, state);

GridView1.DataBind();

} //Closing the search_btn_Click Event

 

//Starting the private method that will return the datatable...  

private DataTable searchData(string text, string State) {

String mySQL;mySQL = @"ContactID, FirstName, LastName, City, Country, Address, PostCode,Telephone, Email

from yourTableName

WHERE lower(ContactID) LIKE '%' + @text + '%'"
+ @"AND state = @state";

SqlCommand command = new SqlCommand(mySQL, connection); command.Parameters.AddWithValue("text", text);

command.Parameters.AddWithValue("state", state);

SqlDataAdapter adptr = new SqlDataAdapter(command);

adptr.Fill(ds);

return ds.Tables[0];

}

 

// Check the comment in the code plz ...  

 

** Please, let me know if it helps ...


Please, “Mark as Answer” the post that helped you.
0
XGmarX
1/29/2008 5:12:02 PM

Hi XGmarX I still get the same error even though i replaced the name to TextBox1 which is the name of my textbox. Could it be because this textboxt and the gridview will only show up after the user is logged in? so how do i sort this problem out?

Server Error in '/contact2k8' Application.

Compilation Error

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0103: The name 'TextBox1' does not exist in the current context

Source Error:


Line 37:         //String state = DropDownList1.SelectedItem.Text;                   // <---- replace this with your dropdown name
Line 38:
Line 39: String text = TextBox1.Text; // <---- replace this with your textbox name
Line 40:
Line 41: GridView1.DataSource = searchData(text);

Source File: c:\WebSites\contact2k8\Default.aspx.cs    Line: 39
 

0
vincentcartero
1/30/2008 10:12:05 PM
Reply:

Similar Artilces:

Search Engine Search Results
From my experience, at least with Google, the more people that search for groups of terms or phrases, and then ONLY click on a certain result, it brings that result up closer to the top, the more times it is selected. Therefore, it can change with time and clickage. I propose we all try an expiriment. Everyone who wants to join in, click on this link: http://tinyurl.com/2pthq Which searches Google with the terms: spinrite hard drive maintenance and data recovery utility Then, on the results screen, ONLY click on the GRC site link/s. Maybe after enough of us do this, ...

Displaying "Sorry there were no results for your search" for a search that yields no results
Hello, I've got an SQL datasource and a GridView control on a page called textsearch.aspx (in a VS web application). The text that the user is searching for is passed in as a querystring - e.g. if the user searches for "clematis" he's navigated to textsearch.aspx?Search=clematis. This all works fine as long as there is something that's called "clematis" (or whatevers being search for) in my database. However, if there are no matches in my database nothing is displayed (except for what's on my master page). I need to be able to display a message which says something to the effect of "Sorry...

Searching search engines
Hey guys  Management has come to me with an idea that they would like to be able to search multiple search engines from one site.  Basically what they want is to have one page where you can enter in your search term then have several tabs that will contains results for search term from each search engine (ie tabs would be Google, MSN, Web Crawler, AOL, Ask Jeeves, Ask Jeeves etc)  I'm wondering if this is possible from an API or web service from each search engine and if there are any issues with making money with the search results since they would like to sell adve...

How to Display Search Result Into a Gridview
Hi Folks! I'm stuck with my VB 2005 web project in writing the code for a button_click event that will display the search result in a gridview from the database. I have a textbox where the user types in the keyword. I have done up a query in the objectDataSource which is: SELECT * From tblBookingsWHERE Event LIKE '%' + @Event + '%' Can anyone please point me to the right direction? Much appreciate your advices. Cheers! Resty  see this example  <%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="tru...

Display Search Results in GridView
How would I go about displaying search results in a gridview.  My confusion is centered around the fact that when the page loads there is no data to display until a search is performed.  I have two thoughts on this. Put a literal in as a place holder and then in the code behind for the search button create the datasource and write out all the asp code for the datagrid to the literal.  My other thought is to have the gridview in the default.aspx but marked as not visible.  Then when I perform the search set the datasourceid property and mark it visible. I am not sure if th...

Sharepoint search-'How to provide 'Display more results' link in sharepoint search result core webpart.
 Hi,Currently i am working with sharepoint search result presentation.I am dealing with search core result webpart.My requirement is , i need to show top five results and then need to show 'X more results' link.when user clicks on this remaining results should displayed. Can anybody help me on this? Regards,Hina.  Hi Hina,             We have a paging option in Dataview Properties.i hope you know this already..Goto webpart Dataview Properties, in paging pane. Set " Display Items in the set of size " to '5 '..then you...

Search Gridview and display/highlight all data which contains what is searched.
Hi all, Im havin a hard time searching Gridview. My problem is I want to display data from my database which contains the words or letter that I have searched, but I don’t know what to put in the SqlDataSource. Example: I searched for the word ‘art’ on the textbox, then Gridview will display all the data which contains the word ‘art’ like: Arthur 2009, Bart Simpson, Arthemis Barton, Heart/Cartly. Can you guys teach me please…. READ THE BELOW LINKS:http://forums.asp.net/t/1359631.aspxhttp://csharpdotnetfreak.blogspot.com/2008/12/search-within-records-i...

Displaying randomised search results in GridView
I have an MS-SQL Stored Procedure that returns search results in a random order each time it is executed. These results are displayed in a GridView through an SqlDataSource control but I've discovered a problem with results that span across more than one page on the GridView. If a link in the pager is selected, sometimes a result appears that was also on another page. Of course, what's happening is that each time a link in the pager is selected, it executes the stored procedure and gets another set of random search results which leads to some results appearing on different pages. So what I...

Search not searching
Hey All, I have a simple asp.net & vb.net form that retrieves product data based on the user entering a product#.  My users are able to search for multiple product #s and have the corresponding data returned.  This all works fine when there is something to return to my datagrid.  When there isn't, you should be able to enter another product# and search again, but it doesn't work.  If there's nothing to return in my datagrid, the Search button won't search again. What's happening is simple to see on the page but hard to explain.  My code is b...

Display result from search with gridview only when the button is pressed
I trying to make a search function for my page.How can I start the page without showing the gridview (the data in the database). The gridview should write out the result from the search, when the button is pushed. Set the visible property of the GridView to False in the GridView declaration. Then in the Click handler, you would set the GridView.Visible back to True if you have search results to display.Mike Banavige~~~~~~~~~~~~Need a site code sample in a different language? Try converting it with: http://converter.telerik.com/ Of course..thanks!!! I get errors on the syntax, whats w...

problem displaying advance search result into gridview
hi all i tried a few example from the previous post.. why it did not work for me.. can anybody give me the hint on how to solve my problem    Which post are you referring to? i'm refering the post on "display the search result into gridview" can you give us the link ?   http://forums.asp.net/t/1322600.aspx when i click on "search button"..nothing appear Hi syud_zack , I suggest you to have a look at this link which can teach you how to do this step by step : http://www.exforsys.com/tutorials/asp.net-2.0/asp.net-2.0-gridvi...

Display different search results in one gridview
I have a grid view that called by a SP(usp_FindrecordbychecknumID -- The sp will return the matching result based on the passing value ( either Check # or Id)  ) There are two text boxes and submit buttons ( search by check # and search by ID ) on the ASP.net page. If a user type a check #   in the “ Search by Check #” text box and click submit button, the matching records in a gridview will be displayed ( it works same way for “Search by ID” text box ). The SP is working fine, but on the ASP page when I click the submit button( either...

How to search database then return results to a Gridview or other display ?
 I can connect to my datasource and by adding a gridview or other I can fill it when running the page.  Now what I have done is added a text box and button to the page. I want someone to be able to enter say a vendor # and click the button and fill the gridview or other.   I need to know how to assosicate the text box to the field of the table I am searching as well as how to pull the results back and display them?    I remember there was and old video in here that showed how it was done but its been long replaced by newer tutorials.Mike Dibble Hi, take a look ...

Advanced search, listbox filters, search results in gridView? (design problem sort of)
 Hi.  I'd like to write an advanced search form... I'm using VB.net, ASP.NET 2.0, VS 2005 (also asp.net ajax) I'd have many filters, mostly multiple select listBoxes. On the right-hand side of each of the filter there would be a button "Add to filter". User would select filters and add them to "current filter" list or possibly *client-side generated gridView (* <- is that possible?). Finally a user would then click on a "Search" button and a gridViewSearchResults would be displayed using dataSource with all those filters.I would also lik...

Web resources about - search engine and displaying search results with gridview - asp.net.presentation-controls

List of birds displaying homosexual behavior - Wikipedia, the free encyclopedia
This list includes birds for which there is documented evidence of homosexual or transgender behavior of one or more of the following kinds: ...

Facebook Displaying Page Insights On Page Admins’ Personal Pages?
Some Facebook page administrators are seeing modules on their personal pages displaying page insights ad, campaign , and like totals for pages ...

Facebook Displaying Unread Messages from Pages in a New Updates Module
Facebook is displaying a new “Updates” sidebar module which shows some users unread messages sent to them by Pages. Users will see the Updates ...

Don Lemon Holds Up Sign Displaying the N-Word, Uncensored, on Air CNN Live - YouTube
Don Lemon Holds Up Sign Displaying the N-Word, Uncensored, on Air Lemon held up a Confederate flag first before holding up the sign. He asked, ...

Hazelwood coal mine fire: Morwell residents displaying symptoms similar to cigarette smokers, expert ...
An expert says some locals near Victoria's Hazelwood mine fire have symptoms similar to cigarette smokers.

Police accused of displaying fake G20 weapons
Toronto's top police officer misled the public by displaying fake weapons used in a medieval-themed role-playing game to help justify their actions ...

Displaying calorie labels at fast-food restaurants doesn't appear to work: study
While the high calorie counts of artery-clogging fast foods are often printed right in front of our eyes, most people ignore them and go ahead ...

Jays' Escobar suspended three games for displaying slur
Toronto Blue Jays shortstop Yunel Escobar has been suspended three games for displaying a homophobic slur on his eye-black during a game on Saturday. ...

Google Play Store now displaying age-based ratings
We reported back in May that Google was adding age-based ratings to all apps in the Play Store as determined by the International Age Rating ...

Google begins displaying “Listen Now” ads for streaming music services (including Beats) in search results ...
... , Streaming Music Continue reading more about Apps , Google , and Spotify at 9to5Mac . What do you think? Discuss "Google begins displaying ...

Resources last updated: 12/3/2015 7:43:53 PM