-1

Hi my c# method is this:

[WebMethod] 
    protected static void fillList(HiddenField hiddenControl, System.Web.UI.WebControls.DropDownList listinc)
    {
        int dato = 0;
        string strSql;
        dato = Convert.ToInt32(hiddenControl.Value);

        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["default"].ToString());
        conn.Open();
        strSql = "SELECT DISTINCT incid FROM HisReportes WHERE peri = " + dato;
        SqlCommand camAND = new SqlCommand(strSql, conn);
        System.Data.DataTable tablainc = new System.Data.DataTable();
        camAND.CommandTimeout = 36000;
        tablainc.Load(camAND.ExecuteReader());

        foreach (DataRow dtrw in tablainc.Rows)
        {            
           listinc.Items.Add(dtrw[0].ToString());
        }

    }

i tested with a onlick function and it works but i need to call it from Ajax function:

$.ajax({
        type: "POST",
        url: "Incio.aspx/fillList",
        data: '{hiddenControl , listinc, dtrw }',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(data)
            {
                $.each(data, function (){
                    $("listinc").append($("<option     />").val(this.incidente).text(this.dtrw[0]));
                });
            },
            failure: function () {
                alert("Failed!");
            }
        });

that's my ajax function but it is not filling the dropdown list.

16
  • 1
    How is it not working? Do you get an error-message? Commented Nov 21, 2019 at 19:54
  • It doesn't fill the dropdown list. Commented Nov 21, 2019 at 19:55
  • So you don't get an error message? Does the alert go off? Commented Nov 21, 2019 at 19:58
  • Yes the alert is working with the message "fail" Commented Nov 21, 2019 at 20:01
  • A lot of weird thigs are going on with this code. First fillList method takes 2 parameters but your ajax sends just one parameter and its not even match. Second your code open to SQL injections. I think you need to know how Client/Server side communicate. You can check How to fill a DropDown using Jquery Ajax Call? Commented Nov 21, 2019 at 20:06

1 Answer 1

4

you need to return the list from your WebMethod, I recommended you to return a string array from the WebMethod.

    [WebMethod]
    public static string[] fillList()
    {
        your code here....
        return listinc.ToArray();
    }

So, into Ajax in the success function

    $.ajax({
      type: "POST",
      url: "./Inicio.aspx/fillList",
      ...
      success: function (data) {
          let data = data["d"];
          let dropdown = $("#listinc");

          for (let i = 0; i < data.length; i++) {
            // put here your data
            dropdown.append(`<option>${ data[i] }</option>`);
          }
       }
    });
Sign up to request clarification or add additional context in comments.

2 Comments

You are going to have to pass in that Hidden Field value in your web method so he can use in the SQL statement
Sure, this is only an example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.