Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.InvalidOperationException: Missing parameter

I try to send parameter to asmx(web service file) but i get error about "System.InvalidOperationException: Missing parameter". Please help me to solve this problem and thank you so much

this is my ajax function

$("#dd_address").change(function () {

            var rowID = $(this).find(':selected').val();
            console.log(rowID);
            $.ajax({
                url: "WebService.asmx/queryCity",

                data: {
                    id: JSON.stringify(rowID),                        
                },
                type: "POST",
                dataType: "json",
                contentType: "application/json; charset-utf-8",
                success: OnSuccess,
                error: OnError
            });
        });

this is my code from asmx

 DataTable result;

    [WebMethod(EnableSession = true)]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string queryCity(string id)
    {

        DataTable dt;
        SqlConnection MRK_Conn = new SqlConnection(@"Data Source=192.168.24.30;Initial Catalog=Marketing_Data;Persist Security info=True;User ID=sa;Password=sa");
        SqlCommand cmd = new SqlCommand();
        SqlDataReader sql_dr;
        System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();

        MRK_Conn.Open();
        cmd = new SqlCommand("select [City RowID], [City Description] from City where [Des Ref Province] = '" + id + "'", MRK_Conn);
        dt = new DataTable();
        sql_dr = cmd.ExecuteReader();
        dt.Load(sql_dr);
        sql_dr.Close();
        MRK_Conn.Close();
        result = dt;

        return serializer.Serialize(result);
    }

and in web config file

  <webServices>
    <protocols>
      <add name="HttpGet"/>
      <add name="HttpPost"/>
    </protocols>
  </webServices>
like image 617
user3001046 Avatar asked Sep 02 '25 17:09

user3001046


1 Answers

Problem in your code is in the way you are passing the input parameter to the method, change it like this:-

var rowID = { "id" : $(this).find(':selected').val() };

Then, pass it like this in the method:-

data : JSON.stringify(rowID)

Apart from this your ADO.NET code is open for SQL Injection attack, so please use parametrized query instead.

like image 181
Rahul Singh Avatar answered Sep 04 '25 06:09

Rahul Singh