revise static method to return dataset through ajax

0

I am trying to fill a gridview from a static method since I use ajax but it was difficult to know how some sentences would have to go according to an example that works but they create a DataTable with data that they specify and what I try to do is migrate that example to data that I get from my BD, this is my code:

$('#btnBuscar').click(function () {
                var nombre = $('#inputBuscaNombre').val();
                var curp = $('#inputBuscaCurp').val();
                
                var actionData = "{'nombre': '" + nombre + "',  'CURP': '" + curp + "'}";
                $.ajax({
                    type: "POST",
                    url: "TEST.aspx/BuscarPersona",
                    data: actionData,
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: OnSuccess,
                    failure: function (response) {
                        alert(response.d);
                    },
                    error: function (response) {
                        alert(response.d);
                    }
                });
            });
            
            
            function OnSuccess(response) {
                alert("hola");
                var xmlDoc = $.parseXML(response.d);
                var xml = $(xmlDoc);
                var customers = xml.find("Personas");
                var row = $("[id*=GVPersonas] tr:last-child").clone(true);
                $("[id*=GVPersonas] tr").not($("[id*=GVPersonas] tr:first-child")).remove();
                $.each(customers, function () {
                    //  debugger;
                    var customer = $(this);
                    $("td", row).eq(0).html($(this).find("ID").text());
                    $("td", row).eq(1).html($(this).find("Nombre").text());
                    $("td", row).eq(2).html($(this).find("Apellido_Paterno").text());
                    $("[id*=GVPersonas]").append(row);
                    row = $("[id*=GVPersonas] tr:last-child").clone(true);
                });
            }
<input type="button" class="btn btn-primary" id="btnBuscar" value="Buscar" />

        [WebMethod]
        public static string BuscarPersona(int curp)
        {
            SqlConnection con = new SqlConnection(@"Data Source =IVXN-LAP\SQLEXPRESS; Integrated Security =true; Initial Catalog=Personas;User ID=;Password=;");
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = System.Data.CommandType.Text;
            cmd.CommandText = "SELECT*FROM PERSONAS WHERE CURP=muuu";
            //cmd.Parameters.AddWithValue("@curp", curp);

            cmd.Connection = con;
            con.Open();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet("hola");
            da.FillSchema(ds, SchemaType.Source, "Personas");
            da.Fill(ds, "Personas");
            DataTable tblPersonas;
            tblPersonas = ds.Tables["Personas"];
            //da.Fill(tblPersonas);

            
            ds.Tables.Add(tblPersonas);
            return ds.GetXml();
            //return dt.GetXml();
        }

Here I put the example code established by the default registers

<script type="text/javascript" src="Scripts/jquery.min.js"></script>
<script type="text/javascript">
    $(function () {
 
        GetCustomers();
 
    });
    function GetCustomers() {
        //     debugger;
        $.ajax({
            type: "POST",
            url: "Default.aspx/GetCustomers",
            data: '{}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: OnSuccess,
            failure: function (response) {
                alert(response.d);
            },
            error: function (response) {
                alert(response.d);
            }
        });
    }
 
    function OnSuccess(response) {
        // debugger;
        var xmlDoc = $.parseXML(response.d);
        var xml = $(xmlDoc);
        var customers = xml.find("Customers");
        var row = $("[id*=gvCustomers] tr:last-child").clone(true);
        $("[id*=gvCustomers] tr").not($("[id*=gvCustomers] tr:first-child")).remove();
        $.each(customers, function () {
            //  debugger;
            var customer = $(this);
            $("td", row).eq(0).html($(this).find("CustomerID").text());
            $("td", row).eq(1).html($(this).find("ContactName").text());
            $("td", row).eq(2).html($(this).find("City").text());
            $("[id*=gvCustomers]").append(row);
            row = $("[id*=gvCustomers] tr:last-child").clone(true);
        });
    };
</script>

codebehind

        private void BindDummyRow()
        {
            DataTable dummy = new DataTable();
            dummy.Columns.Add("CustomerID");
            dummy.Columns.Add("ContactName");
            dummy.Columns.Add("City");
            dummy.Rows.Add();
            gvCustomers.DataSource = dummy;
            gvCustomers.DataBind();
        }
 
        [WebMethod]
        public static string GetCustomers()
        {
            DataTable dt = new DataTable();
            dt.TableName = "Customers";
            dt.Columns.Add("CustomerID", typeof(string));
            dt.Columns.Add("ContactName", typeof(string));
            dt.Columns.Add("City", typeof(string));
            DataRow dr1 = dt.NewRow();
            dr1["CustomerID"] = 1;
            dr1["ContactName"] = "Customer1";
            dr1["City"] = "City1";
            dt.Rows.Add(dr1);
            DataRow dr2 = dt.NewRow();
            dr2["CustomerID"] = 2;
            dr2["ContactName"] = "Customer2";
            dr2["City"] = "City2";
            dt.Rows.Add(dr2);
            DataRow dr3 = dt.NewRow();
            dr3["CustomerID"] = 3;
            dr3["ContactName"] = "Customer3";
            dr3["City"] = "City3";
            dt.Rows.Add(dr3);
            DataRow dr4 = dt.NewRow();
            dr4["CustomerID"] = 4;
            dr4["ContactName"] = "Customer4";
            dr4["City"] = "City4";
            dt.Rows.Add(dr4);
            DataRow dr5 = dt.NewRow();
            dr5["CustomerID"] = 5;
            dr5["ContactName"] = "Customer5";
            dr5["City"] = "City5";
            dt.Rows.Add(dr5);
            DataSet ds = new DataSet();
            ds.Tables.Add(dt);
            return ds.GetXml();
        }
    
asked by Ivxn 25.02.2018 в 23:25
source

0 answers