Click here to Skip to main content
15,892,480 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi all,
how to solve my error,

{"Message":"Cannot convert object of type \u0027System.String\u0027 to type \u0027System.Collections.Generic.IDictionary`2[System.String,System.Object]\u0027","StackTrace":" at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject)\r\n at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)\r\n at System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext context, JavaScriptSerializer serializer)\r\n at System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData, HttpContext context)\r\n at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.InvalidOperationException"}

calling web method using jquery,
my script code like
JavaScript
<script language="javascript" type="text/javascript">

         $(document).ready(function () {


             $("#btnok").click(function () {
                 alert('');
                 var username = $("#name").val();
                 var password = $("#password").val();
                 var clientid = $("#clientid").val();
                 var data = { username: $("#name").val(), password: $("#password").val(), clientid: $("#clientid").val() };

                 //  var data = { title:$("#title").val(),name: $("#name").val(), city: $("#city").val(), state: $("#state").val(), date: $("#date").val(),duration:$("#duration").val(),pay:$("#pay").val(),type: $("#type").val(),industry:$("#industry").val(),status:$("#status").val(),description:$("#description").val(),qualification: $("#qualification").val() };
                 var data = JSON.stringify(data);
                 alert(data);
                 $.ajax({
                     type: "POST",
                     url: "http://localhost:3658/HostingService.asmx/SETusers",
                     data: JSON.stringify(data), //"{'title':" + title + ",'C.Name':" + name + ",'city':" + city + ",'state':" + state + ",'date':" + date + ",'duration':" + duratin + ",'pay':" + pay + ",'industry':" + industry + ",'type':" + type + ",'status':" + status + ",'description':" + description + ",'qualification':" + qualification + "}",
                     //     data: // "{'title':'" + $("#title").val() + "','C.Name':'" + $("#name").val() + "','city':'" + $("#city").val() + "','state':'" + $("#state").val() + "','date':'" + $("#date").val() + "','duration':'" + $("#duration").val() + "','pay':'" + $("#pay").val() + "','industry':'" + $("#industry").val() + "','type':'" + $("#type").val() + "','status':'" + $("#status").val() + "','description':'" + $("#description").val() + "','qualification':'" + $("#qualification").val() + "'}",
                     contentType: "application/json;charset=utf-8",
                     processData: false,
                     dataType: "json",
                     success: function (data) {
                         alert('Success');
                     },
                     error: function (data, status, error) {
                         alert(data.responseText);

                     }
                 });

                  });
        });
       </script>


my html code,
XML
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="form3">
        <tr>
            <td width="25%" align="left" valign="middle">
                USERNAME
            </td>
            <td width="75%" align="left" valign="middle">
                <input id="name" type="text" class="textbox2" name="name" size="30" />
            </td>
        </tr>
        <tr>
            <td align="left" valign="middle">
               PASSWORD
            </td>
            <td align="left" valign="middle">
                <input id="password" type="text" class="textbox2" name="name" size="30" />
            </td>
        </tr>
        <tr>
            <td align="left" valign="middle">
               CLIENTID
            </td>
            <td align="left" valign="middle">
                <input id="clientid" type="text" class="textbox2" name="name" size="30" />
            </td>
        </tr>
        <tr>
        <td>
 <input id="btnok" name="Submit" type="button" value="Submit" class="btn2" border="0" />
        </td>
        </tr>
        </table>


my web services code is,
C#
[WebMethod, ScriptMethod(ResponseFormat = ResponseFormat.Json,UseHttpGet=false)]
        public string SETusers(string username, string password,string clientid)
        {  DataSet ds = new DataSet();
            try
            {
                SqlDataAdapter adp = new SqlDataAdapter();
                adp.SelectCommand = new SqlCommand();
                adp.SelectCommand.CommandType = CommandType.StoredProcedure;
                adp.SelectCommand.Connection = (SqlConnection)OpenConnection1();
                adp.SelectCommand.CommandText = "SP_USERDETAILS_SET";
                SqlParameter[] Parameters = new SqlParameter[3];
                Parameters[0] = new SqlParameter("@NAME", SqlDbType.VarChar,50);
                Parameters[0].Value = username;
                Parameters[1] = new SqlParameter("@PASSWORD", SqlDbType.VarChar, 50);                Parameters[1].Value = password;
                Parameters[2] = new SqlParameter("@CLIENT_ID", SqlDbType.Int);
                Parameters[2].Value =Convert.ToInt32(clientid);              
                adp.SelectCommand.Parameters.AddRange(Parameters);
                adp.Fill(ds);
                string[][] JaggedArray = new string[ds.Tables[0].Rows.Count][];
                //int i = 0;
                //Dictionary<object, object> resultMain = new Dictionary<object, object>();
                List<object> resultMain = new List<object>();
                foreach (DataRow rs in ds.Tables[0].Rows)
                {
                    Dictionary<string,> result = new Dictionary<string,>();                    foreach (DataColumn dc in ds.Tables[0].Columns)
                    {
                        result.Add(dc.ColumnName, rs[dc].ToString());
                    }
                    resultMain.Add(result);
                    //i = i + 1;
                }
                JavaScriptSerializer js = new JavaScriptSerializer();
                string strJSON = js.Serialize(resultMain);
                return strJSON;
            }
            catch
            {
                CloseConnection1();
                //return ds.GetXml();
                return "-1";
            }
            finally
            {
                CloseConnection1();
            }
        }
Posted
Updated 4-Jan-13 0:22am
v2
Comments
Member 9581488 4-Jan-13 9:41am    
Please Refer following link.
It might help you.

http://elegantcode.com/2009/02/21/javascript-arrays-via-jquery-ajax-to-an-aspnet-webmethod/

I am not so sure but giving you suggestion.

var data = { username: username, password: password , clientid: clientid };
Try using above line. and check that variables for username password and clientid has some values.

Verify with the code below but all looks fine to me, make sure your server code has no error. Or you will see the error message from 'alert(thrownError)' hope this helps
$("#btnok").click(function () {
	var username = $("#name").val();
    var password = $("#password").val();
    var clientid = $("#clientid").val();
    var data = { username: $("#name").val(), password: $("#password").val(), clientid: $("#clientid").val()};
	
	var json_data = JSON.stringify(data);
	
	//ajax call
	$.ajax({
    	type: "POST",
    	url: "http://localhost:3658/HostingService.asmx/SETusers",
    	data: json_data,
    	contentType: "application/json; charset=utf-8",
    	dataType: "json",
    	success: function(data) {
        	alert(data.d);
    	},
    	error: function(xhr, ajaxOptions, thrownError){
        	  alert(thrownError);
    	}
	}); //ajax call end

}); //btnok end
 
Share this answer
 
What about converting
C#
Dictionary<string,> result = new Dictionary<string,>(); 
to
C#
Dictionary<string,string> result = new Dictionary<string,string>(); 
?
 
Share this answer
 
Pass the parameters carefully

var params = '{name: "' + $("#<%=txtUserName.ClientID%>")[0].value + '" , dob:18 }';


or
fetch values into variable

var params = '{GroupId: "' + groupId + '" , TeamId:"' + teamId + '" }';

And note that
no overloaded methods are allowed on the server side.

for this on server side it must as follows

C#
[System.Web.Services.WebMethod]
       public static string SetActionRequiredImage(string GroupId, string TeamId)
       {
 
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