how to add hashtable in jquery

Posted by Klbaiju under jQuery on 7/7/2015 | Points: 10 | Views : 2539 | Status : [Member] | Replies : 2
Hi,

I want to add key value pair like hashtable in jquery.

following is my code for adding key value pair to hashtable

$('#btnview').click(function (e) {
e.preventDefault();


var vc = $('#ContentPlaceHolder1_txtvoucherid').val();
var type = $('#ContentPlaceHolder1_txtvtype').val();
var hashtable = {};
hashtable['tablename'] = "vchr";
hashtable['type'] = type;
hashtable['vchrid'] = vc;
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "CashPaymentVoucher.aspx/dp",
data: "{'id':'"+hashtable+"'}",
dataType: "json",
success: function (data) {
}
});
});


and following is the webmethod in the next page

[WebMethod]
public static VoucherDetails[] dp(Hashtable id )
{
List<VoucherDetails> details = new List<VoucherDetails>();
i know this is wrong .and showing error .

my requirement is to pass hashtable to another page

like this


BAL obj=new BAL();
DataSet ds = obj.Getsearch(id);
return details.ToArray();
}


i know this is wrong .and showing error .

my requirement is to pass hashtable to another page

like this
DataSet ds = obj.Getsearch(id);


here id is the hashtable from aspx page

error showed was

{"Message":"Cannot convert object of type \u0027System.String\u0027 to type \u0027System.Collections
.Hashtable\u0027","StackTrace":" at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal
(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object\u0026 convertedObject
)\r\n at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o, Type type
, JavaScriptSerializer serializer, Boolean throwOnError, Object\u0026 convertedObject)\r\n at System
.Web.Script.Services.WebServiceMethodData.StrongTypeParameters(IDictionary`2 rawParams)\r\n at System
.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(Object target, IDictionary`2 parameters
)\r\n at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData
methodData, IDictionary`2 rawParams)\r\n at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall
(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.InvalidOperationException"


how to solve this

regards

Baiju




Responses

Posted by: Sheonarayan on: 7/8/2015 [Administrator] HonoraryPlatinum | Points: 25

Up
0
Down
Dear Klbaiju,

A web service should not accept hashtable as input parameter as we can only transmit data over web service with object that can be xml serialized. Instead of hashtable, use Generic list List<string> and add string into it separated by special character like "|".

"tablename|type|vchrid".

In this case as you are consuming the webservice via Ajax so i would encourage you to send JSON string as parameter. Change the web service implementation accordingly.

Thanks


Regards,
Sheo Narayan
http://www.dotnetfunda.com

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Klbaiju on: 7/8/2015 [Member] Starter | Points: 25

Up
0
Down
do you have any sample code

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response