How to search related data from database in asp.net like Google

By
Hi friends you will have to seen there are lots of records in database at this time.In this tutorial i will explain how to make search to user friendly in asp.net application.you can easily search related data from the database.In this application you can search lots of data by entering the single character in search box. Google also search lots of records from the database or cache memory like this application but it use very complex algorithm.This concept is also known as Dictionary search algorithm.But we can not compare with Google search engine. To provide relevant search to your user you can use this concepts on your asp.net application.
There are some steps to implement this concept on your asp.net application which are given below:-
Step 1:- First open your visual studio-->File--> New -->website-->select ASP.NET Empty website--> Add-->open Solution Explorer-->Add a New web form --> drag and drop label ,Text Box, SqlDataSource and Gridview control from toolbox as shown below:-



form

Step 2:- Now go properties of every Text Box control-->set AutoPostBack = True.


properties


Step 3:- Now create Database .mdf file on your website -->create an employee table --> Insert some values in employee table as shown below:-


database


Note :- You can create employee table in your installed sql server database also.If are facing any problem in database read below:-
  1. How to add .mdf file on your website easily
  2. How to solve sql server problem in .NET
Step 4:- Now Double click on each Text Box and write the following codes which are given below:
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Drawing;
using System.Data;

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

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

    }
    protected void searchtxt_TextChanged(object sender, EventArgs e)
    {
 SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
        con.Open();
 SqlDataAdapter da = new SqlDataAdapter("Select*from employee where [Emp Name] like'" + searchtxt.Text + "%'", con);
   string text = ((TextBox)sender).Text;
   DataSet ds = new DataSet();
   da.Fill(ds);
   if (ds.Tables[0].Rows.Count > 0)
   {
       myGridview1.DataSource = ds.Tables[0];
       myGridview1.DataBind();
       Label2.Visible = false;

   }
   else
   {
       Label2.Visible = true;
       Label2.Text = "No Record Found";
       this.Label2.ForeColor = Color.Red;
   }
    }
    protected void txtsearch_TextChanged(object sender, EventArgs e)
    {
 SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
        con.Open();
 SqlDataAdapter da = new SqlDataAdapter("Select*from employee where Mobile like'" + txtsearch.Text + "%'", con);
        string text = ((TextBox)sender).Text;
        DataSet ds = new DataSet();
        da.Fill(ds);
        if (ds.Tables[0].Rows.Count > 0)
        {
            myGridview1.DataSource = ds.Tables[0];
            myGridview1.DataBind();
            Label2.Visible = false;
        }
        else
        {
            Label2.Visible = true;
            Label2.Text = "No Record Found";
            this.Label2.ForeColor = Color.Red;
        }
    }
}

Step 5:- Now Run the application (press F5)-->enter some value in first text box-->you will see following output-


output
Step 6:- Enter some value in Second text box-->you will see following output-


output

I hope this is helpful for you.
For More...
  1. Microsoft Sql server
  2. Store procedure in sql server
  3. Views in sql server
  4. Data List Real Application
  5. .Net Interview Questions and answers
  6. Composit application  in asp.net
  7. Threading in c#
  8. File Handling Real application
  9. Web form controls
  10. Host asp.net website free on server
Click below for download whole application
                   Download

4 comments:

  1. Sir i have tried as u said above step by step but its showing the following error

    Server Error in '/ASP.NETCONTROLS' Application.
    The user instance login flag is not supported on this version of SQL Server. The connection will be closed.
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.Data.SqlClient.SqlException: The user instance login flag is not supported on this version of SQL Server. The connection will be closed.

    Source Error:

    ReplyDelete
  2. Tis is My Error Both DataSource and DataSourceID are defined on 'GridView1'. Remove one definition.

    ReplyDelete
  3. Sir how can display Record in Two Date with calendar in grid view

    ReplyDelete
  4. nice 1 its very helpful and it working thanks

    ReplyDelete

Powered by Blogger.