Greetings, I have developed a C# method which looks like this:
[WebMethod]
protected static void populateDropdown(HiddenField hiddenControl, System.Web.UI.WebControls.DropDownList listinc)
{
int data = 0;
string query;
data = Convert.ToInt32(hiddenControl.Value);
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["default"].ToString());
connection.Open();
query = "SELECT DISTINCT incident FROM IncidentReports WHERE period = " + data;
SqlCommand command = new SqlCommand(query, connection);
System.Data.DataTable table = new System.Data.DataTable();
command.CommandTimeout = 36000;
table.Load(command.ExecuteReader());
foreach (DataRow row in table.Rows)
{
listinc.Items.Add(row[0].ToString());
}
}
I have tested this method using an onclick function and it works perfectly. Now, I need to call it from an Ajax function:
$.ajax({
type: "POST",
url: "Homepage.aspx/populateDropdown",
data: '{hiddenControl , listinc, row }',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data)
{
$.each(data, function (){
$("listinc").append($("<option />").val(this.incident).text(this.row[0]));
});
},
failure: function () {
alert("Oops! Something went wrong!");
}
});
This is my Ajax function, but unfortunately, it is not populating the dropdown list as expected.