Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6986827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:51:31+00:00 2026-05-27T18:51:31+00:00

Im currently developing an webapplication that are going to have an Autocomplete search form,

  • 0

Im currently developing an webapplication that are going to have an Autocomplete search form, the results are being updated from an Sql Database. The whole “functionality” is working except of one thing. The client is calling an C# Webservice which are going to return an array to the client. The thing is. The list of the client shows the same results multiply times. So if theres only one match from the database the client shows that match like 20-25 times in an vertical list. I don’t know how to solve this, please help.

C# Webservice:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data.SqlClient;

namespace WebApplication6
{

public class searchResult
{
    public string Title;
    public string img;
    public string href;
}

/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService]
[System.Web.Script.Services.GenerateScriptType(typeof(searchResult))]
public class WebService : System.Web.Services.WebService
{

    [WebMethod]
    public string HelloWorld()
    {
        return "Hello World";
    }

    [WebMethod]
    public searchResult[] Search(string txtSearch)
    {
        //Semuler to slow internet connection
        //System.Threading.Thread.Sleep(2000);

        //Declare collection of searchResult
        List<searchResult> resultList = new List<searchResult>();

        string constr = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; ;
        SqlConnection con = new SqlConnection(constr);
        SqlCommand cmd = con.CreateCommand();

        cmd.CommandText = "SELECT * FROM [DriverInfo], [Teams], [Tracks] WHERE UserName LIKE '" + txtSearch + "%' OR TeamName LIKE '" + txtSearch + "%' OR TrackName LIKE '" + txtSearch + "%'";
        try
        {
            con.Open();

            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                searchResult DriverResult = new searchResult();
                DriverResult.Title = dr["UserName"].ToString();
                DriverResult.img = "driver.png";
                DriverResult.href = dr["UserId"].ToString();

                if (DriverResult.Title.ToLower().Contains(txtSearch.ToLower()))
                {
                    resultList.Add(DriverResult);
                }

                searchResult TeamResult = new searchResult();
                TeamResult.Title = dr["TeamName"].ToString();
                TeamResult.img = "team.png";
                TeamResult.href = dr["Id"].ToString();

                if (TeamResult.Title.ToLower().Contains(txtSearch.ToLower()))
                {
                    resultList.Add(TeamResult);
                }

                searchResult TrackResult = new searchResult();
                TrackResult.Title = dr["TrackName"].ToString();
                TrackResult.img = "track.png";
                TrackResult.href = dr["TrackId"].ToString();

                if (TrackResult.Title.ToLower().Contains(txtSearch.ToLower()))
                {
                    resultList.Add(TrackResult);
                }
            }
            con.Close();
            return resultList.ToArray();
        }
        catch
        {
            return null;
        }

    }
}
  }

HTML:

<!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>
<title></title>
<script src="http://code.jquery.com/jquery-1.4.3.min.js" type="text/javascript></script>
<link href="main.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
        function search() {
        if ($("#txtSearch").val() != "") {

        $(".divResult").show(); //show div block that contains on result
        $(".loading").show(); // show loading text while getting result

    //call web searvice
            $.ajax({ type: "POST",
                url: "WebService.asmx/Search", //function that in web service
                data: "{txtSearch:'" + $("#txtSearch").val() + "'}",// passing value of txtSearch input
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(response) {
        //declaer client object and set to it returned result from web sevice function
                    var result = response.d;
                    $(".record").html(""); // clear previous result

        //looping in 'result' array to get data and fill it inside ".record" div as html
                    $.each(result, function(index, res) {

            //append img tag inside ".record" div
                        $('<img />', {
                            src: 'Images/' + res.img,
                            alt: res.Title
                        }).addClass("img").appendTo('.record');

            //append anchor tag inside ".record" div
                        $('<a></a>', {
                            href: res.href,
                            text: res.Title
                        }).addClass("txtResult").appendTo('.record');

                        $(".record").append("<hr />");
                    });
        //hide loading div when the data was got
                    $(".loading").hide();
                },
                error: function(msg) {
                    $(".record").html(msg.d);
                }

            });

        }
        else {
        $(".divResult").hide(); //hide div that contains result when the input text is empty
        $(".record").html(""); //also loading text when the input text is empty
        }
    }
</script>
</head>
<body>
<div class="content">
    <input id="txtSearch" onkeyup="search()" type="text" />
<div class="divResult">
<div class="loading">Loading..</div>
<div class="record"></div>
</div>
</div>
</body>
</html>

CSS:

        .content
    {
        margin:50px auto;
        text-align:center;
        width:322px;
    }

    #txtSearch
    {
        border:solid 1px #cccccc;
        width:320px;
        color:#555555;
        font: 18pt tahoma;
        height: 20px;
        font-size: 12px;
        font-family: "lucida grande",tahoma,verdana,arial,sans-serif;
    }
    .divResult
    {
        position:absolute;
        background-color:#F2F2FF;
        border-style:solid;
        border-width:1px;
        border-color:#999999;
        width:320px;
        text-align:left;
        display:none;
    }
    .img
    {
        padding-top: 2px;
        width:30px;
        height:30px;
        float:left;
    }
    .txtResult
    {
        display:block;
        width:320px;
        height:30px;
        color:#3c5899;
        font-family: "lucida grande",tahoma,verdana,arial,sans-serif;
        font-size: 14px;
        font-weight: bold;
        text-decoration:none;
    }
    .txtResult:hover {
        background-color: #3c5899;
        color: White;
    }
    .loading
    {
        font: 10pt tahoma;
        text-align:center;
    }
    .record
    {
        margin:0px;
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T18:51:32+00:00Added an answer on May 27, 2026 at 6:51 pm

    The type of join you’re doing in your SQL query is not recommended, and it may account for your duplicate results. I’d suggest something more like this:

    select 'Driver' as Type, UserId as Id, UserName as Name 
    from DriverInfo 
    where UserName like '%foo%'
    
    union 
    
    select 'Team' as Type, Id, TeamName 
    from Teams
    where TeamName like '%foo%'
    
    union 
    
    select 'Track' as Type, TrackId, TrackName 
    from Tracks
    where TrackName like '%foo%'
    

    This would give you results like this:

    Type      Id       Name
    ----      --       ----
    Driver    23       Foo
    Driver    73       Foo Jr.
    Team      27       Team Foo
    Team      64       Team Foobar
    Track     98       Bar Foo Field
    

    Here’s how you could consume these results:

    while (dr.Read())
    {
    
        resultList.Add(
            new searchResult {
                Title = dr["Name"].ToString(),
                img = dr["Type"].ToString() + ".png",
                href = dr["UserId"].ToString() } );
    }
    

    As is, you’re probably getting results like these (a cartesian product), where the number of results wont be A + B + C, but rather A * B * C:

    UserId  UserName      Id  TeamName       TrackId  TrackName      ...
    ------  --------      --  --------       -------  ---------      ---
    23      Foo           27  Team Foo       98       Bar Foo Field  ...
    23      Foo           64  Team Foobar    98       Bar Foo Field  ...
    73      Foo Jr.       27  Team Foo       98       Bar Foo Field  ...
    73      Foo Jr.       64  Team Foobar    98       Bar Foo Field  ...
    

    Also, notice that since select * is being used, you’ll get more columns than you need.

    Update:

    You said you weren’t sure with how to add your variables to the query. You should use a parameterized query instead of string concatenation to make your code cleaner and to protect against SQL injection attacks:

    cmd.CommandText = "... where UserName like @SearchPattern ... where TeamName like @SearchPattern ...";
    cmd.Parameters.Add("@SearchPattern", txtSearch + "%");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently developing a web application that receives data from an on-site database.
I am currently developing a search engine and I have some implemented algorithms that
I'm currently developing a web application that has one feature while allows input from
In the web application I am currently developing, I have quite a few database
Our team is currently developing a web application, in that we have a class
I'm currently developing a web application in ASP.Net with SQL Server and I would
I am developing a C#/SQL ASP.NET web application in VS 2008. Currently I am
Currently developing a PHP framework and have ran into my first problem. I need
I am currently developing an approval routing WCF service that will allow an user
I'm currently developing a web application and have run into a little problem. I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.