You must Sign In to post a response.
  • Category: JQuery

    How to add hashtable in jquery

    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>();



    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
  • #760443
    Hello Baiju,

    Refer the below code :

    var ary = [];

    function pushToAry(name, val) {
    var obj = {};
    obj[name] = val;
    ary.push(obj);
    }

    pushToAry("myName", "myVal");
    $(your collection of form els).serializeArray();


    See the below code is easy :
    Hashtable ht = new Hashtable();
    ht.Add("1", "abc");
    ht.Add("2", "def");
    Session["myHashtable"] = ht;
    Response.Redirect("Page2.aspx");


    on Page2 :
    if (Session["myHashtable"] != null)
    {
    Hashtable ht = (Hashtable)Session["myHashtable"];
    foreach (DictionaryEntry dEntry in ht) {
    Response.Write(dEntry.Key.ToString() + " " + dEntry.Value.ToString());
    }
    }


    Hope this will help you.

    Regards,
    Nirav Lalan
    DNS Gold Member
    "If you can dream it, you can do it."


  • Sign In to post your comments