Frage

I have a web-user control, and i want to call the web-service from it. What is my main motive:

1. i am creating a web-user control for advanced search, for that i am adding the bound fields and buttons[Edit,Delete] dynamically to a gridview. 2. Now i am using the ajax to go edit and delete (there is specific reason to go for this approach as i have already mentioned that i am adding the boundfields and buttons dynamically, for that firstly i m clearing all the columns of grid then added the new ones. now if button's click fires then its post back the page and this make the buttons will disappear from the grid, so i want to use the Json for that)

Here is my code of grid::

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" CellPadding="4"
                BorderWidth="1px" ForeColor="#333333" GridLines="None">
                <AlternatingRowStyle BackColor="White" />
                <FooterStyle BackColor="#990000" ForeColor="White" Font-Bold="True" />
                <HeaderStyle Height="30px" BackColor="#990000" Font-Bold="True" ForeColor="White" />
                <PagerStyle ForeColor="#333333" HorizontalAlign="Center" BackColor="#E2E2E2" />
                <RowStyle CssClass="test" BackColor="#E2E2E2" Height="25px" BorderWidth="1px" ForeColor="#333333" />
                <SelectedRowStyle BackColor="#E2E2E2" Font-Bold="True" ForeColor="Navy" />
                <SortedAscendingCellStyle BackColor="#FDF5AC" />
                <SortedAscendingHeaderStyle BackColor="#4D0000" />
                <SortedDescendingCellStyle BackColor="#FCF6C0" />
                <SortedDescendingHeaderStyle BackColor="#820000" />
            </asp:GridView>

Here is the code for Json call

$('.DeleteButton').live('click', function () {
        alert('hellllo');
        $.ajax({
            url: '<%=ResolveUrl("~/Control/WebService/WebService.asmx/HelloWorld") %>',
            data: "{ }",
            dataType: "json",
            type: "POST",
            contentType: "application/json;charset=utf-8",
            success: function () {
                alert('hi;');
            },
            error: function () {
                alert('Record could not be deleted. \n Please try again later. hye');
            },
            failure: function () {
                alert('Record could not be deleted. \n Please try again later. hello');
            }
        });
    });

I am always getting the following Error:

500 internal Error please help me to get out of this prob i am stuck on it from last 2 days.

War es hilfreich?

Lösung 3

I have resolved it for me as below:

Jquery Code is:

$(document).ready(function () {
$('.DeleteButton').live('click', function (e) {
    var td = $(this).parent('td').parent('tr');
    if (confirm('Do you want to delete this record?')) {
        var Index = $(this).attr('id');
        var pos = Index.indexOf("_");
        var position = Index.indexOf("_");
        while (pos > -1) {
            pos = Index.indexOf("_", pos + 1);
            if (pos > -1) {
                position = pos;
            }
        }
        Index = Index.substr(position + 1);

        var Id = $(this).attr('alt');
        var TableName = $('[id$=hdfTableName]').val();
        var PrimaryKey = $('[id$=hdfPrimaryKey]').val();

        $.ajax({
            type: "POST",
            url: "Search.aspx/DeleteRecord",
            data: "{ 'Id': '" + Id + "','TableName':'" + TableName + "','PrimaryKey':'" + PrimaryKey + "' }",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {
                if (msg.d == '1') {
                    td.hide();
                    $('.lblMessage').addClass("SuccessMessage");
                    $('.lblMessage').text('Record is deleted sucessfuly.');
                    $('.lblMessage').delay('5000').fadeOut('10000');
                } else {
                    $('.lblMessage').addClass("ErrorMessage");
                    $('.lblMessage').text('There is some error, while deleting the record.');
                    $('.lblMessage').delay('5000').fadeOut('10000');
                }
            }
        });
    }
});
$('.EditButton').live('click', function (e) {
    var Id = $(this).attr('alt');
    window.location.href = 'Default.aspx?id=' + Id + '';
});

});

And i have called the Web Method from the page as below:

[WebMethod]
public static string DeleteRecord(string Id, string TableName, string PrimaryKey)
{
    String result = "";
    try
    {
        Id = "'" + Id + "'";
        clsSearch objSearch = new clsSearch();
        result = objSearch.DeleteRecord(TableName, PrimaryKey, Id);
        result = "1";
    }
    catch (Exception ex)
    {
        result = ex.Message;
    }
    return result;
}

By the way thanks for your replies.....

Andere Tipps

If you are using chrome browser to debug it, do the following:

  1. Open the developer (correct me if wrong name) panel pressing F12
  2. Set the tab to Network tab
  3. Call the method

If internal server error 500 is occured, then you will find a record somewhere in the bottom with red fonts. You can debug with the URL stated in there. The information there also gives you the response information.

Moreover, you can try to debug the service using visual studio debugging activated to the webservice. Then try to call the webservice to see if it catches any exceptions.

500 internal error - From that I can guess is :
dont use

var html = '<%=ResolveUrl("~/Control/WebService/WebService.asmx/HelloWorld") %>';

Instead use:

var html= '<%=ResolveUrl("full path /Control/WebService/WebService.asmx/HelloWorld") %>';

or

var html = '<%=ResolveUrl("../../Control/WebService/WebService.asmx/HelloWorld") %>';
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top