As Textbox autocomplete is not working. This is my code for controller and script
public JsonResult GetProduct(string WarehouseName)
{
var cn = new SqlConnection();
var ds = new DataSet();
string strCn = ConfigurationManager.ConnectionStrings["MySqlConnection"].ToString();
cn.ConnectionString = strCn;
var cmd = new SqlCommand
{
Connection = cn,
CommandType = CommandType.Text,
CommandText = "select WarehouseName from Warehouse Where WarehouseName like @myParameter"
};
cmd.Parameters.AddWithValue("@myParameter", "%" + WarehouseName + "%");
try
{
cn.Open();
cmd.ExecuteNonQuery();
var da = new SqlDataAdapter(cmd);
da.Fill(ds);
}
catch (Exception)
{
}
finally
{
cn.Close();
}
DataTable dt = ds.Tables[0];
var txtItems = (from DataRow row in dt.Rows
select row["WarehouseName"].ToString()
into dbValues
select dbValues.ToLower()).ToList();
return Json(txtItems, JsonRequestBehavior.AllowGet);
}
@section scripts {
<script type="text/javascript">
var textbox;
var selectValue;
$(function () {
textbox = $("#txtCountrty");
selectValue = $('ul#selectedValue');
textbox.on("input", function () {
getAutoComplete(textbox.val());
});
});
function getAutoComplete(countryName) {
var uri = "StockAdjustment/GetProduct";
$.getJSON(uri, { countryName: countryName })
.done(function (data) {
selectValue.html("");
var count = 0;
$.each(data, function (key, item) {
//$('<option>').text(item).appendTo('#selectedValue');
var li = $('<li/>').addClass('ui-menu-item').attr('role', 'menuitem')
.html("<a href='#' onclick=\"setText('" + item + "') \">" + item + "</a>").appendTo(selectValue);
count++;
});
});
}
function setText(text) {
textbox.val(text);
getAutoComplete(text);
}
</script>
Kindly help me where the mistake occurs