locked
Asp.NET MVC Ado.NET Get Data Kendo Grid RRS feed

  • Question

  • User197255166 posted

    Hello everyone, I am trying to get kendo grid data with ado.net. But this code does not include kendo grid data.

    What is the problem ? Can you make an example with the code I shared.

            System.Data.SqlClient.SqlConnection sqlConnection = new System.Data.SqlClient.SqlConnection(connectionString);
                sqlConnection.Open();
                string email = "email";
    
                string odemetakip = "select TOP 1 O.OrderId from Customer C INNER JOIN OdemeTakip O ON O.CustomerId = C.Id Where C.Email=@email order by O.OrderId desc";
                System.Data.SqlClient.SqlCommand sqlCommand = new System.Data.SqlClient.SqlCommand(odemetakip, sqlConnection);
                sqlCommand.Parameters.AddWithValue("email", email);
    
                System.Data.SqlClient.SqlDataReader sqlDataReaders = sqlCommand.ExecuteReader();
    
                string odemetakipdetay = "select OT.OrderId,OT.BankResponseContent,OT.ShortErrorMessage,OT.CreatedOnUtc from OdemeTakipDetay OT Where OT.OrderId=@odemetakip ";
    
                string odeme = "";
                while (sqlDataReaders.Read())
                {
                    odeme = sqlDataReaders["OrderId"].ToString();
                }
                sqlDataReaders.Close();
    
    
                System.Data.SqlClient.SqlCommand sqlCommands = new System.Data.SqlClient.SqlCommand(odemetakipdetay, sqlConnection);
                sqlCommands.Parameters.AddWithValue("odemetakip", odeme);
    
                System.Data.SqlClient.SqlDataReader sqlDataReader = sqlCommands.ExecuteReader();
                sqlDataReader.Close();
    
                System.Data.DataTable dt = new System.Data.DataTable();
                System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(sqlCommands);
                da.Fill(dt);
    
    
    
    
                sqlConnection.Close();
    
    
                string json = JsonConvert.SerializeObject(dt);
    
                var result = new DataSourceResult
                {
                    Data = json,
                    Total = 10,
                    Errors = ""               
                    
                   
                };
    
                return Json(result.Data, JsonRequestBehavior.AllowGet);

    HTML

    @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()
        <div class="content-header clearfix">
            <h1 class="pull-left">
                @T("Admin.Orders")
            </h1>
    
        </div>
    
        <div class="content">
            <div class="form-horizontal">
                <div class="panel-group">
                    <div class="panel panel-default panel-search">
                        <div class="panel-body">
                            <div class="row">
                                <div class="col-md-5">                        
    
                                    <div class="form-group">
                                        <div class="col-md-4">
                                           <b>Ödeme Takip Numarası</b>
                                        </div>
                                        <div class="col-md-4">
                                            <input type="text" id="search-product-name" placeholder="Ödeme Takip Numarası" autocomplete="off" class="form-control" />
                                            <span id="search-product-friendly-name"></span>
    
    
                                        </div>
                                        <div class="col-md-4">
                                            <span class="input-group-btn">
                                                <button type="submit" id="go-to-order-by-number" name="go-to-order-by-number" class="btn btn-info btn-flat">
                                                   Sorgula
                                                </button>
                                            </span>
                                        </div>
                                    </div>
    
                                  
    
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-md-7 col-md-offset-5">
                                    <button type="button" id="search-orders" class="btn btn-primary btn-search">
                                        <i class="fa fa-search"></i>
                                        @T("Admin.Common.Search")
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
    
                    <div class="panel panel-default">
                        <div class="panel-body">
                            <div id="orders-grid"></div>
    
                            <script>
                                $(document).ready(function() {
                                    $("#orders-grid").kendoGrid({
                                        dataSource: {
                                            type: "json",
                                            transport: {
                                                read: {
                                                    url: "@Html.Raw(Url.Action("OdemeTakipGet", "Order"))",
                                                    type: "POST",
                                                    dataType: "json",
                                                    data: additionalData
                                                }
                                            },
                                            schema: {
                                                data: "Data",
                                                total: "Total",
                                                errors: "Errors"
                                            },
                                            requestEnd: function (e) {
                                                if (e.type == "read") {
                                                    var response = e.response;
                                                    if (response) {
                                                        //store extra data
                                                        reportAggregates = e.response["Data"];
                                                    }
                                                }
                                            },
                                            error: function(e) {
                                                display_kendoui_grid_error(e);
                                                // Cancel the changes
                                                this.cancelChanges();
                                            },
                                            pageSize: @(defaultGridPageSize),
                                            serverPaging: true,
                                            serverFiltering: true,
                                            serverSorting: true
                                        },
                                        pageable: {
                                            refresh: true,
                                            pageSizes: [@(gridPageSizes)]
                                        },
                                        editable: {
                                            confirmation: "@T("Admin.Common.DeleteConfirmation")",
                                            mode: "inline"
                                        },
                                        scrollable: false,
                                        dataBound: onDataBound,
                                        columns: [                                       
    
                                            {
                                                field: "OrderId",
                                                title: "@T("Admin.Orders.Fields.Customer")",
                                                width: 250,
                                                template: '#=CustomerFullName# (#=CustomerEmail#)'
                                            },
                                            {
                                                field: "BankResponseContent",
                                                title: "@T("Admin.Orders.Fields.Store")",
                                                width: 100
                                            }, {
                                                field: "ShortErrorMessage",
                                                title: "@T("Admin.Orders.Fields.IsReconciliated")",
                                                width: 50,
                                                attributes: { style: "text-align:center" },
                                            }, {
                                                field: "CreatedOnUtc",
                                                title: "@T("Admin.Orders.Fields.CreatedOn")",
                                                width: 150,
                                                type: "date",
                                                format: "{0:G}"
                                            },
    
    
    
                                        ]
                                    } );
                                });
                            </script>
    
                            <script type="text/javascript">
                                var selectedIds = [];
                                var reportAggregates = "";
    
                                $(document).ready(function () {
                                    //search button
                                    $('#search-orders').click(function () {
                                        //search
                                        var grid = $('#orders-grid').data('kendoGrid');
                                        grid.dataSource.page(1); //new search. Set page size to 1
                                        //grid.dataSource.read(); we already loaded the grid above using "page" function
                                        //clear selected checkboxes
                                        $('.checkboxGroups').attr('checked', false).change();
                                        selectedIds = [];
                                        return false;
                                    });
    
                                  
                          
                                });
    
                                function additionalData() {
                                    var data = {
                                       @*StartDate: $('#@Html.FieldIdFor()').val(),
                                        EndDate: $('#@Html.FieldIdFor('BankResponseContent')').val(),
                                        OrderStatusIds: $('#@Html.FieldIdFor(model => model.OrderStatusIds)').val(),
                                        PaymentStatusIds: $('#@Html.FieldIdFor(model => model.PaymentStatusIds)').val(),*@
                                     
    
                                    };
                                    addAntiForgeryToken(data);
                                    return data;
                                }
    
                                function onDataBound(e) {
                                    $('#orders-grid input[type=checkbox][id!=mastercheckbox]').each(function () {
                                        var currentId = $(this).val();
                                        var checked = jQuery.inArray(currentId, selectedIds);
                                        //set checked based on if current checkbox's value is in selectedIds.
                                        $(this).attr('checked', checked > -1);
                                    });
    
                                    updateMasterCheckbox();
    
                                    //update order totals summary
                                    if (reportAggregates != "") {
                                        for (var key in reportAggregates) {
                                            $('#aggregator-profit-block').text(reportAggregates['aggregatorprofit']);
                                            $('#aggregator-shipping-block').text(reportAggregates['aggregatorshipping']);
                                            $('#aggregator-tax-block').text(reportAggregates['aggregatortax']);
                                            $('#aggregator-total-block').text(reportAggregates['aggregatortotal']);
                                        }
                                    }
                                }
    
                                function updateMasterCheckbox() {
                                    var numChkBoxes = $('#orders-grid input[type=checkbox][id!=mastercheckbox]').length;
                                    var numChkBoxesChecked = $('#orders-grid input[type=checkbox][id!=mastercheckbox]:checked').length;
                                    $('#mastercheckbox').attr('checked', numChkBoxes == numChkBoxesChecked && numChkBoxes > 0);
                                }
                            </script>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    }

    Sunday, March 7, 2021 9:58 AM

All replies

  • User475983607 posted

    You are posting the wrong support forum.  This is an ASP.NET support forum.    You need Telerik support which is a paid for framework.

    https://demos.telerik.com/kendo-ui/grid/index

    I reviewed your code though and found the code makes no logical sense.   The place the code tries to get data is on an AJAX post to OdemeTakipGet.  You did not include the OdemeTakipGet action signature or the additional data. 

    Anyway, you really need Telerik support.  

    Sunday, March 7, 2021 12:29 PM
  • User1120430333 posted

    What is the problem ? Can you make an example with the code I shared. 

    I don't think the code you have shown works best with Kendo grid and a datatable. The Kendo grid I have worked with used a custom class/type in a collection. So my advice would be to use ADO.NET, SQL Command objects, parametrized T-SQL, a datareader populating a custom object, loading the object into a collection and presenting the collection in a Json array to the grid.

    You can also post to the Telerik kendo UI forum that also has plenty code examples.

    Sunday, March 7, 2021 5:57 PM
  • User197255166 posted

    Hello,

    I am getting error "The required anti-forgery cookie "__RequestVerificationToken" is not present."

     I'm trying to give the mail parameter. Either way, he doesn't go to action. What is the problem ?

    What should I do

    var mail = $('#search-product-name').val();
    <script type="text/javascript">
       
        $('#search-orders').click(function () {
             
                var mail = $('#search-product-name').val();
                $("#orders-grid").kendoGrid({
                   
                    dataSource:
                    {
                       
                        type: "application/json",
                        transport: {
                            read: {
                             
                                url: "Order/OdemeTakipGet/",
                              url: "@Html.Raw(Url.Action("OdemeTakipGet", "Order",new {email= mail }))",
                                type: "POST",
                                 data: additionalData
     
                            }
     
                        }
     
                    },
     
                    height: 700,
                    pageable: { refresh: true, pageSizes: [2, 25, 50] },
                    groupable: false,
                    sortable: true,
                    columns: [
                        { field: "OrderId", title: "Sipariş Numarası", width: 20 },
                        { field: "BankResponseContent", title: "Banka Gelen Kod", width: 250 },
                        { field: "ShortErrorMessage", title: "Mesaj", width: 100 },
                        { field: "CreatedOnUtc", title: "Tarih", width: 50, type: "date", format: "{0:G}" }
                    ]
     
                });
     
                return false;
            });
    </script>


    Wednesday, March 10, 2021 4:16 AM