user3857870 user3857870 - 1 month ago 11
ASP.NET (C#) Question

how to return a string[] in autocomplete error System.String[]

my script in the head

var ProductNames;
$(document).ready(function () {
$("#inputSuccess2").autocomplete({
source: function (request, response) {
var texttyped = $("#inputSuccess2").val();
$.ajax({
url: "@Url.Content("~/Products/FetchName")",
dataType: "json",
data: "search=" + texttyped,
type: "get",
contentType: "application/json; charset=utf-8",
success: function (data) {
var msg = Assessments_JqueryAutoComplete.AutoComplete(request.term).value; //(ajaxpro functions)
response(msg.d);
response($.map(data, function (item) {
return {


//value: '' + item

}
}
))
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("Error: " + textStatus);
}
});
},
minLength: 1,
select: function (event, ui) {

window.location = "/Product/EditSingleProduct?PrdNm=" + ui.item.value;

}
}
)
});


and my controller is

public string[] FetchName(String search){
SqlConnection SearchProduct = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ToString());

SearchProduct.Open();
SqlCommand cmd = SearchProduct.CreateCommand();
List<string> Product1 = new List<string>();
cmd.CommandText = String.Format("(SELECT [ProductName],[ProductType] FROM [Rush].[dbo].[Product] where ProductName like '%{0}%')", search);
SqlDataReader reader = cmd.ExecuteReader();

while (reader.Read())
{
Product1.Add(reader.GetString(0));

}
SearchProduct.Close();
return Product1.ToArray();
}


I am getting parse error while I manually check the output is System.String[]

so I guess it is unable to parse string[] in that dropdown list because I checked the return value it is holding 11 items in the array
thanks in advance

Answer
 public  JsonResult FetchName(String search){
  SqlConnection SearchProduct = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ToString());

        SearchProduct.Open();
        SqlCommand cmd = SearchProduct.CreateCommand();
        List<string> Product1 = new List<string>();
        cmd.CommandText = String.Format("(SELECT [ProductName],[ProductType] FROM [Rush].[dbo].[Product] where ProductName like '%{0}%')", search);
        SqlDataReader reader = cmd.ExecuteReader();

        while (reader.Read())
        {
            Product1.Add(reader.GetString(0));

        }
        SearchProduct.Close();
        return  Json(Product1.ToArray(),JsonRequestBehavior.AllowGet);
}