Click here to Skip to main content
15,883,868 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i am returning datatable from the webservice in my code . while doing R&D , i just return the dataset from the webservice but now its giving the error invalid xml .Please review the code and guide me how to solve more ...here is my code :
C#
 [WebMethod]
   public DataSet GetAllExhibitionList()
   {

       DALSearch objdal = new DALSearch();

       return objdal.GetAllExhibitionDetail();
   }


public DataSet GetAllExhibitionDetail()
       {

           try
           {
               SqlCommand cmd = new SqlCommand();
               cmd.CommandText = "proc_getAllExhibitionList";
               cmd.CommandType = CommandType.StoredProcedure;
               cmd.Connection = con;
               SqlDataAdapter da = new SqlDataAdapter(cmd);

               da.Fill(dtexhibitiondetail1);
           }
           catch (Exception ex)
           {
               throw ex;
           }
           finally
           {

           }
           return dtexhibitiondetail1;
       }

JavaScript
<script type="text/javascript">
    $(function () {
        debugger;
        $.ajax({
      
            type: 'post',
            url: 'WebServices/DataLoading.asmx/GetAllExhibitionList',
            data: {},
            contentType: 'application:JSON;charset:UTF-8',
            success: OnSuccess,
            failure: function (response) {
                alert(response.d);
            },
            error: function (response) {
                alert(response.d);
            }

        });


    });
    function OnSuccess(response) {
        $("[id*=dlallexhibitation]").attr("border", "1");
        var xmlDoc = $.parseXML(response.d);
        alert(xmlDoc);
        var xml = $(xmlDoc);
        var customers = xml.find("Table");
        var row = $("[id*=dlallexhibitation] tr:last-child").clone(true);
        $("[id*=dlallexhibitation] tr:last-child").remove();
        $.each(customers, function () {
            var customer = $(this);
            $("#lblMsg", row).html($(this).find("#lblMsg").text());
            $(".mainRow", row).attr('indusid');
            $(".mainRow", row).attr('cid');
            $(".mainRow", row).attr('coid');
            $(".mainRow", row).attr('mid');
            $(".mainRow", row).attr('yid');
            $(".in_venues_des2_mn", row).find('.in_ven_hd2 a').html($(this).find(".in_ven_hd2").text());
            $(".in_venues_des2_mn", row).find('.sub_in_venues_des2_mn').text();
            $(".dates", row).find('.sub_dates').text();
            $(".dates", row).find('.filter_dates').text();
            $(".in_venues_des3", row).text();
            $(".img_thumb2", row).find('img').attr('src');
            $('.in_venues_des6').text();
            $('in_ven_navs2').find('.in_ven_more').find('a').attr('href');
            $('in_ven_navs2').find('.in_ven_more').find('a').attr('exhibitionid');
            $('in_ven_navs2').find('.in_nav3').attr('exhibitionid');           
            $('in_ven_navs2').find('.in_nav3').find('a').attr('href');
            $("[id*=dlallexhibitation]").append(row);
            row = $("[id*=dlallexhibitation] tr:last-child").clone(true);
        });
    }

</script>
Posted
Updated 4-May-14 23:41pm
v3

you can return string of DataSet XML

C#
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
public DataSet GetAllExhibitionList()
{
    DALSearch objdal = new DALSearch();
    return objdal.GetAllExhibitionDetail().GetXml();         
}

JavaScript
<script type="text/javascript">
$(function () {
    debugger;

    $.ajax({
        type: 'post',
        url: 'WebServices/DataLoading.asmx/GetAllExhibitionList',
        data: {},
        contentType: 'application:JSON;charset:UTF-8',
        success: OnSuccess,
        failure: function (response) {
            alert(response.d);
        },
        error: function (response) {
            alert(response.d);
        }
    });

});
function OnSuccess(response) {
    $(response).find('Table').each(function () {
      alert($(this).find("Column1").text());
    });
}
</script>
 
Share this answer
 
v4
Comments
Naina2 5-May-14 3:37am    
DamithSL : it is showing error like this :

uncaught exception: Invalid XML: undefined
Naina2 5-May-14 4:37am    
now it is not showing error but the result is not showing also.. it is not displaying anything
DamithSL 5-May-14 5:06am    
what are the column names in the table?
Naina2 5-May-14 5:11am    
basically there is a procedure , i m using for retrieving the data ..and there are joins in the same .. so what would be the benefit of having column names
DamithSL 5-May-14 5:15am    
you are searching for #lblMsg in datatable records, that's why I'm asking. if you have column called lblMsg, then you can get value like alert($(this).find("lblMsg").text());
translate you backed code to json:
StringBuilder JsonString = new StringBuilder();
            //Exception Handling        
            if (dt != null && dt.Rows.Count > 0)
            {
                JsonString.Append("{ ");
                JsonString.Append("\"T_blog\":[ ");
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    JsonString.Append("{ ");
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        if (j < dt.Columns.Count - 1)
                        {
                            JsonString.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":" + "\"" + dt.Rows[i][j].ToString() + "\",");
                        }
                        else if (j == dt.Columns.Count - 1)
                        {
                            JsonString.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":" + "\"" + dt.Rows[i][j].ToString() + "\"");
                        }
                    }
                    /**//**/
                    /**//*end Of String*/
                    if (i == dt.Rows.Count - 1)
                    {
                        JsonString.Append("} ");
                    }
                    else
                    {
                        JsonString.Append("}, ");
                    }
                }
                JsonString.Append("]}");
                return JsonString.ToString();
            }
            else
            {
                return null;
            }
 
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