autocomplete source from database

Posted by Laghaterohan under jQuery on 5/21/2013 | Points: 10 | Views : 2233 | Status : [Member] | Replies : 4
Hello,
I am trying to implement autocomplete feature in my asp.net mvc application.
My source would be database values and not hardcoded, however I am not sure how should I do this? Any example or tutorial or steps would be helpful.

Pl guide me.

script type="text/javascript">
$(function() {
$("#names").autocomplete({source: }); // Here source I will have values from database.
});
</script>


I am not very clear how how my action will send the response so that source will have the values I need. please help.

Best Regards,
Rohan Laghate



Responses

Posted by: harikrishna149-10896 on: 5/21/2013 [Member] Starter | Points: 25

Up
0
Down
Hi,

refer the following links may helpful to u.

http://www.itorian.com/2013/02/jquery-ui-autocomplete-with-json-in-mvc.html
http://blogs.msdn.com/b/stuartleeks/archive/2012/04/23/asp-net-mvc-amp-jquery-ui-autocomplete.aspx

Thanks & Regards
Hari Krishna

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

Posted by: Ramsakthi on: 5/21/2013 [Member] Starter | Points: 25

Up
0
Down
script type="text/javascript">

$(function() {

$("#names").autocomplete({
source:function(requestresponse){
$.ajax(){
url:"Default.aspx/getAreas",
data:"{'name':' + request.term + '"city' :' +$(#dd span:first).text() +"}",
datatype:"json",
ContentType:"application/json;charset=utf-8;",
dataFilter:function(data) {
return data;}
success: function(data) {
(response ($.map,data.d,function(item)
{
return
label:item,
value:item
))
}
)}
},
error:Function(XmlHttpRequest,textstatus,errorthrown)
alert(textstatus);
})
minlength:0
}).click(function() {
$(this).autocomplete("search");
});
});
});
</script>

/* Code-Behind */Default.aspx.cs
[WebMethod(EnableSession = true)]
public static List<string> getAreas(string name, string city)
{
return DataManager.getAllAreas(city , name);
}
/* Code-Behind */DataManager.cs
public static string getConnectionString()
{
return System.Configuration.ConfigurationManager.ConnectionStrings["RFConnectionString"].ConnectionString;
}
public static List<string> getAllAreas(string city, string area)
{
DataSet ds = new DataSet();
try
{
SqlConnection conn = new SqlConnection(getConnectionString());
SqlCommand cmd = new SqlCommand("[GetAreas]", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@cityname", SqlDbType.VarChar);
cmd.Parameters.Add("@areaname", SqlDbType.VarChar);
cmd.Parameters["@cityname"].Value = city;
cmd.Parameters["@areaname"].Value = area;
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
List<string> s = ds.Tables[0].AsEnumerable().Select(x => x[0].ToString()).ToList();
return s;
}
catch
{
return null;
}

}


Ramu.R

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

Posted by: Laghaterohan on: 5/21/2013 [Member] Starter | Points: 25

Up
0
Down
Thanks !

Best Regards,
Rohan Laghate

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

Posted by: Rimi1289 on: 5/25/2013 [Member] Starter | Points: 25

Up
0
Down
You can check for both the options here.

http://www.encodedna.com/2013/04/autocomplete-textbox-using-jquery.htm

To get values from the database as your source, you need to call another WebMethod and the return value will the source.




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

Login to post response