Click here to Skip to main content
15,880,891 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All

I am building a web API for Address.
If a user enters postcode , the rest of the text boxes should be populated with the correct values.
I have done something but the complete address is showing in one single text box.
Can please some one help me


.
C#
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AutoCompletetextBox.aspx.cs" Inherits="AutoTextBox.AutoCompletetextBox" %>

<!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>AutoComplete Textbox with webservice using jQuery</title>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        SearchText();
      
       
    });
    function SearchText() {
        $(".autosuggest").autocomplete({
            source: function (request, response) {
                $.ajax({
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    url: "AutoCompleteService.asmx/GetAutoCompleteData",
                    data: "{'postcode':'" + document.getElementById('txtSearch').value + "'}",
                    dataType: "json",
                    success: function (data) {
                        response(data.d);
                    },
                    error: function (result) {
                        alert("Please Enter Valid PostCode");
                    }
                });
            }
        });
    }
</script>
</head>
<body>
<form id="form1" runat="server">
<div class="demo">
<div class="ui-widget">
<label for="tbAuto">Post Code:                 </label>
<input type="text" id="txtSearch" class="autosuggest" /><br />
    <label for="tbAuto">Building Number:       </label>
<input type="text" id="txtSearch1" class="autosuggest" /><br />
    <label for="tbAuto">Building Name:          </label>
<input type="text" id="txtSearch2" class="autosuggest" />
</div>
</div>
</form>
</body>
</html>

.
C#
namespace AutoTextBox
{
    /// <summary>
    /// Summary description for AutoCompleteService
    /// </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]
    public class AutoCompleteService : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
        [WebMethod]
        public List<string> GetAutoCompleteData(string postcode)
        {
            
            List<string> result = new List<string>();
            using (SqlConnection con = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=Test;Integrated Security=True"))
            {
                using (SqlCommand cmd = new SqlCommand("select (CAST(ISNULL(BuildingNumber,0) AS NVARCHAR(20))  +' '+ ISNULL(BuildingName,'') +' '+ ISNULL(Postcode,'')) as CompleteAddress from AddressRecord where( CAST(ISNULL(BuildingNumber,0) AS NVARCHAR(20))  +' '+ ISNULL(BuildingName,'') +' '+ ISNULL(Postcode,'') )like '%'+@SearchText+'%'", con))
                {
                    con.Open();
                    cmd.Parameters.AddWithValue("@SearchText", postcode);

                    SqlDataReader dr = cmd.ExecuteReader();
                    while (dr.Read())
                    {
                        result.Add(dr["CompleteAddress"].ToString());
                    }
                    return result;
                }
            }
        }
   }
}


Please anyone can guide me.

Thanks
Posted

1 solution

Yes its showing in one single textbox, because you have done nothing special for it to understand how to split the returned response from the web service and assign it to textboxes as per need. Please use jquery/javascript and set proper text to the different textboxes as per the returned response.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900