ASP.Net jQuery AJAX Error: Type System.Int32 is not supported for deserialization of an array

Last Reply 3 months ago By dharmendr

Posted 3 months ago

Hi guys i have a delete javascript button in project . for some reason it is not working .

please help me. i have attached the code

        $("#btndeletebookmark").click(function (e) {
            e.preventDefault();
            var ids = [];
            $("#lstBookmark input[type='checkbox']:checked").each(function () {
                ids.push($(this).parent().find('a').prop('id'));
            });

            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "DataCollection.aspx/DeleteBookMark",
                data: JSON.stringify({ BookmarkID: ids }),
                dataType: "json",
                success: function (data) {
                    alert("Bookmark Removed Successfully");
                    location.reload();
                },
                error: function (result) {
                    alert("Error");
                }
            });
        })

 

<input id="Button1" class="button" type="button" value="Delete" />
</div>
<div class="row">
    <asp:CheckBoxList ID="Checkboxlist1" runat="server">
    </asp:CheckBoxList>
</div>

 

[WebMethod]
public static int DeleteBookMark(int BookmnarkID)
{
    BookTree objBal = new BookTree();
    return objBal.DeleteBookMark(BookmnarkID);
}

public int DeleteBookMark(int BookmarkID)
{
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["connString"].ConnectionString);
    conn.Open();
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = conn;
    cmd.CommandText = "usp_RemoveBookmark";
    cmd.CommandType = System.Data.CommandType.StoredProcedure;
    SqlParameter param1 = new SqlParameter("@IN_BookmarkID", BookmarkID);
    cmd.Parameters.Add(param1);
    return cmd.ExecuteNonQuery();
}

 

You are viewing reply posted by: dharmendr 3 months ago.
Posted 3 months ago Modified on 3 months ago

Hi suhaas121,

Since you are passing Javascript Array to WebMethod through Ajax call you need to define int array in the WebMethod as parameter. 

Check the below modified code.

HTML

<div>
    <input id="btndeletebookmark" class="button" type="button" value="Delete" />
</div>
<div class="row">
    <asp:CheckBoxList ID="lstBookmark" runat="server">
        <asp:ListItem Text="Mango" Value="1" />
        <asp:ListItem Text="Apple" Value="2" />
        <asp:ListItem Text="Banana" Value="3" />
        <asp:ListItem Text="Orange" Value="4" />
    </asp:CheckBoxList>
</div>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
    $(function () {
        $("#btndeletebookmark").click(function (e) {
            e.preventDefault();
            var ids = [];
            $("#lstBookmark input[type='checkbox']:checked").each(function () {
                ids.push(parseInt($(this).val()));
            });
            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "Default.aspx/DeleteBookMark",
                data: JSON.stringify({ BookmnarkID: ids }),
                dataType: "json",
                success: function (data) {
                    alert("Bookmark Removed Successfully");
                    location.reload();
                },
                error: function (result) {
                    alert(result.responseText);
                }
            });
        })
    });
</script>

Namespace

C#

using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.Services;

VB.Net

Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Services

Code

C#

[WebMethod]
public static void DeleteBookMark(int[] BookmnarkID)
{
    foreach (int id in BookmnarkID)
    {
        DeleteBookMark(id);
    }
}

public static void DeleteBookMark(int BookmarkID)
{
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["connString"].ConnectionString);
    conn.Open();
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = conn;
    cmd.CommandText = "usp_RemoveBookmark";
    cmd.CommandType = CommandType.StoredProcedure;
    SqlParameter param1 = new SqlParameter("@IN_BookmarkID", BookmarkID);
    cmd.Parameters.Add(param1);
    cmd.ExecuteNonQuery();
    conn.Close();
}

VB.Net

<WebMethod()>
Public Shared Sub DeleteBookMark(ByVal BookmnarkID As Integer())
    For Each id As Integer In BookmnarkID
        DeleteBookMark(id)
    Next
End Sub

Public Shared Sub DeleteBookMark(ByVal BookmarkID As Integer)
    Dim conn As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("connString").ConnectionString)
    conn.Open()
    Dim cmd As SqlCommand = New SqlCommand()
    cmd.Connection = conn
    cmd.CommandText = "usp_RemoveBookmark"
    cmd.CommandType = CommandType.StoredProcedure
    Dim param1 As SqlParameter = New SqlParameter("@IN_BookmarkID", BookmarkID)
    cmd.Parameters.Add(param1)
    cmd.ExecuteNonQuery()
    conn.Close()
End Sub

Screenshot