Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
here is the error that i get

System.InvalidOperationException: Missing parameter: MyParameterName.
   at System.Web.Services.Protocols.ValueCollectionParameterReader.Read(NameValueCollection collection)
   at System.Web.Services.Protocols.UrlParameterReader.Read(HttpRequest request)
   at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()
   at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()



C#
<pre>     
[WebService(Namespace="http://microsoft.com/webservices/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
    public class WebService1 : System.Web.Services.WebService
    {
        [WebMethod]
        [System.Web.Script.Services.ScriptMethod(ResponseFormat = System.Web.Script.Services.ResponseFormat.Json)]

         public List<string> MethodName(string parameterName)
            {
               
                List<string> result = new List<string>();          
               

                    using (System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection("connectionstring"))
                    {
                        using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand("mydbasequery, con))

                        {
                            con.Open();
                            cmd.Parameters.AddWithValue("@xxx", parameterName);

                            System.Data.SqlClient.SqlDataReader dr = cmd.ExecuteReader();
                            while (dr.Read())
                            {
                                result.Add(dr["column_name"].ToString());
                            }
                            return result.ToList();
                        }                      
                    }
                }
                else
                {
                    result.Add("sample");
                    return result.ToList();
                }
            }




<pre lang="Javascript">
<pre>  <script type="text/javascript">
      $(document).ready(function () {
          functionName();
      });
      function functionName() {

          $('.classname').autocomplete({
              source: function (request, response) {
                  $.ajax({
                      async: true,
                      type: "POST",
                      contentType: "application/json; charset=utf-8",
                      data: "{'service_parameter' :'" + $('.classname').val() + "'}",
                      url: 'WebService1.asmx/MethodName',
                      dataType: "json",
                      crossDomain: true,
                      success: function (data) {
                          if (data != null) {                            
                              response(data.d);
                          }
                          else {
                              response("rule");
                          }
                       },
                      
                      error: function (error) {
                          response("rule");
                      }
                  });
              },
              minLength: 10,            
          });
      }
</script>


What I have tried:

I tried this code and still getting this error over and over again..
Posted
Updated 22-Feb-18 23:46pm
v3
Comments
Richard Deeming 9-Feb-18 9:00am    
The parameter on your method is called parameterName, but the name you're passing from Javascript is service_parameter.

Is that just an inconsistency introduced when you anonymised your code, or do the names not match in your real code?
Richard Deeming 9-Feb-18 9:02am    
Also, you're building the JSON string manually, which won't work if a user enters a single quote in the textbox. You'd do better to use the built-in JSON object:
data: JSON.stringify({ 'parameterName': $('.classname').val() })
JSON.stringify() - JavaScript | MDN[^]
jessrubio 11-Feb-18 22:41pm    
thank you for your comment, I try to modify my solution but the error still the same.
ZurdoDev 12-Feb-18 14:15pm    
Then you did something wrong. You need to debug it. We can't run it for you. Richard is right, you need to match the parameter name.
jessrubio 12-Feb-18 20:49pm    
I already correct the parameterName and service_parameter. they both same as parameterName, I run my code and it runs exactly the way i want it the text is now autocomplete based on my character input but once it inspect it using google chrome Iand on my newtwork tab i've got this error.. "System.InvalidOperationException: Missing parameter: parameterName", is there something with my network. or is it my parameterName?

1 solution

Hi,below is the solution :

var serpar_val = $('.classname').val() ;

assign like below:

data: JSON.stringify({ service_parameter: serpar_val }),
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900