Click here to Skip to main content
16,020,381 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello, I'm calling a web service in which i have created a variable of object type which have two attribute however call goes to the service properly but I'm unable to receive sent object inside my web method using my class.

following is my JavaScript code

function newfun() {
                debugger
              
                var DbClass = new Object();
                DbClass.col1 = document.getElementById("txtName").value; //'hello';
                DbClass.col2 = document.getElementById("txtEmail").value;   //'vaibhav';

                alert(JSON.stringify(DbClass));
                $.ajax({
                    type: "POST",
                    url: "WebService.asmx/DBAddClass",
                    data: "{obj:" + JSON.stringify(DbClass) + "}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function(value) {
                                               alert("sucess");
                    },
                    error: function(value) {
                        //error_callback(response.responseText);
                        alert("Error Occured");
                    }
                });

            }




following is my webmethod

<WebMethod()> _
  Public Sub DBAddClass(ByVal obj() As DbClass)
       Try
           Dim clsObj As New DbClass


           '' clsObj._col1 = obj.col1
           ''  clsObj._col2 = obj.col2
           Dim constr As String = ConfigurationManager.ConnectionStrings("myCon").ConnectionString
           Using con As New SqlConnection(constr)
               Using cmd As New SqlCommand("INSERT INTO tab1 (Col1, Col2) VALUES (@Name, @Country)")
                   cmd.Parameters.AddWithValue("@Name", obj(0)._col1) 'obj("col1"))
                   cmd.Parameters.AddWithValue("@Country", obj(0)._col2) ' obj("col2"))
                   cmd.Connection = con
                   con.Open()
                   cmd.ExecuteNonQuery()
                   con.Close()
               End Using
           End Using

       Catch ee As Exception

       End Try
   End Sub




also I have created class as well which is as follows
Public Class DbClass
    Private col1 As String
    Public Property _col1() As String
        Get
            Return col1
        End Get
        Set(ByVal value As String)
            col1 = value
        End Set
    End Property
    Private col2 As String
    Public Property _col2() As String
        Get
            Return col2
        End Get
        Set(ByVal value As String)
            col2 = value
        End Set
    End Property
End Class



I want to catch data in the object of
DbClass
which will be send from a ajax call. any help will be of great use for me.
Posted
Comments
ZurdoDev 6-Oct-15 7:46am    
Evaluate value in your success function and see what comes back.
kedar001 6-Oct-15 7:53am    
check with....

<system.web.services.webmethod()> _
Public Shared Function DBAddClass(ByVal obj As DbClass) As DbClass

End Function
Richard Deeming 6-Oct-15 8:43am    
Sounds like the answer to me. :)

(You might need to explain that you've changed the parameter from an array to a single object, which might not be obvious to the OP.)
kedar001 6-Oct-15 9:26am    
Yeah...
It should be
Public Shared Function DBAddClass(ByVal obj() As DbClass) As DbClass
vaibhavhedaoo 6-Oct-15 8:00am    
I want to catch data in webmethod which i have sent by ajax call

The problem is that your method is expecting an array of DBClass objects, but the calling code is only passing a single DBClass object.

Since you only ever use a single object in the method, the simplest solution is to change the method to accept a single object:
VB.NET
<WebMethod()> _
Public Sub DBAddClass(ByVal obj As DbClass)
    Dim constr As String = ConfigurationManager.ConnectionStrings("myCon").ConnectionString
    Using con As New SqlConnection(constr)
        Using cmd As New SqlCommand("INSERT INTO tab1 (Col1, Col2) VALUES (@Name, @Country)", con)
            cmd.Parameters.AddWithValue("@Name", obj._col1)
            cmd.Parameters.AddWithValue("@Country", obj._col2)
            
            con.Open()
            cmd.ExecuteNonQuery()
        End Using
    End Using
End Sub

NB: An empty Catch block is a very bad idea. Only catch the specific exceptions which your code can handle, and log any exceptions which don't propagate to the caller.

If you want to pass an array of objects to the method, then you need to pass the correct JSON:
JavaScript
data: JSON.stringify({ obj: [ DbClass ] }),

(Note the extra square brackets around the obj value, indicating an array.)
 
Share this answer
 
Thanks to all for trying to help me. but none of your solution has worked correctly. Hope you guys help me out very soon.
 
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