Problem with autocomplete

Posted by Rickeybglr under ASP.NET AJAX on 8/20/2012 | Points: 10 | Views : 1320 | Status : [Member] | Replies : 2
in my below code autocomplete is working but name and city both textboxes showing the mix result of name and city. name should display only names and city should display only cities name. i am new to AJAX .

.aspx code
<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: "demoSearch.aspx/GetAutoCompleteData",
data: "{'username':'" + document.getElementById('txtName').value + "','City':'" + document.getElementById('txtCity').value + "'}",
dataType: "json",
success: function (data) {
response(data.d);
},
error: function (result) {
alert("Error");
}
});
}
});
}
</script>

----
<div class="ui-widget">
<label for="tbAuto">
Enter UserName:
</label>
<input type="text" id="txtName" class="autosuggest" />
<label for="tbAuto">
Enter city:
</label>
<input type="text" id="txtCity" class="autosuggest" />

</div>

.aspx.cs

public static List<string> GetAutoCompleteData(string username,string City)
{
List<string> result = new List<string>();

SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);
using (SqlCommand cmd = new SqlCommand("select UserName,City from userRegistrationTB where UserName LIKE '%'+@txtName+'%' OR City Like '%'+@txtCity+'%'", con))
{
con.Open();
cmd.Parameters.AddWithValue("@SearchText", username);
cmd.Parameters.AddWithValue("@txtCity",City);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
result.Add(dr["UserName"].ToString());
result.Add(dr["City"].ToString());
}
return result;

}
}




Responses

Posted by: Nadh123 on: 8/21/2012 [Member] Starter | Points: 25

Up
0
Down
write javascript
<asp:TextBox ID="txtcityauto" runat="server"></asp:TextBox>
<ajaxToolkit:AutoCompleteExtender ServiceMethod="GetcityAuto" MinimumPrefixLength="0"
CompletionInterval="100" ContextKey="NEW_CITY_NAME" UseContextKey="true" EnableCaching="false"
CompletionSetCount="10" CompletionListItemCssClass="autocomplete_listItem" CompletionListCssClass="autocomplete_completionListElement"
CompletionListHighlightedItemCssClass="autocomplete_highlightedListItem" TargetControlID="txtcityauto"
ID="cityauto" ServicePath="~/AreacityService.asmx" OnClientItemSelected="Oncityselection"
runat="server" FirstRowSelected="false">
</ajaxToolkit:AutoCompleteExtender>
<asp:TextBox ID="txtname" runat="server"></asp:TextBox>
function Oncityselection(sender, eventArgs) {
//debugger;
var results = eval('(' + eventArgs.get_value() + ')');
document.getElementById('<%= txtcityauto.ClientID %>').value = results.Text;
document.getElementById('<%= txtname.ClientID %>').value = results.Value;

}

Mahendra
91-9908699686

Rickeybglr, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rickeybglr on: 8/23/2012 [Member] Starter | Points: 25

Up
0
Down
Hi Nadh123,

So query would be the same or it cud be the different ?

Rickeybglr, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response