Solved

KendoDropDownList WebService JSON serilaized list datasource

Posted on 2014-11-19
5
245 Views
Last Modified: 2014-12-29
Hello,
I'm trying to bind a kendodropdownlist data source to an asp.net c# webservice

 <p>
                   <label for="dates">Date:</label><input id="dates" style="width: 270px" />
                </p>
 
 
$("#dates").kendoDropDownList({
            optionLabel: "Select date...",
            dataTextField: "datestamp",
            dataValueField: "datestamp",
            autoBind: false,
            dataSource: {
                transport: {
                    read: {
                        type: "POST",
                        url: "CGCSService.asmx/DropDownListDates",
                        //contentType: "application/json; charset=utf-8",
                        dataType: "json"
                    },
                    success: function (msg) {
                        alert(msg.d)
                        options.success($.parseJSON(msg.d))
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        //alert(textStatus);
                    }
                },
                schema: {
                    data: "datestamp"
                }
            }
        });
 
 
 
 
[WebMethod]
    public String DropDownListDates()
    {
        try
        {
            var connectString = WebConfigurationManager.ConnectionStrings["CGCS"].ConnectionString;
            using (SqlConnection sqlconn = new SqlConnection(connectString))
            {
                using (System.Data.SqlClient.SqlCommand sqlCmd = sqlconn.CreateCommand())
                {
                    sqlCmd.CommandText = "select distinct CONVERT(DATE,datestamp,101) as datestamp from serverdataHistory order by datestamp desc";
                    sqlCmd.CommandTimeout = 0;
                    sqlconn.Open();
 
                    DataSet ds = new DataSet();
                    SqlDataAdapter da = new SqlDataAdapter(sqlCmd);
                    da.Fill(ds);
 
                    return JsonConvert.SerializeObject(ds);
 
                   
                   
                }
            }
        }
        catch (Exception)
        {
            return null;
        }
    }
0
Comment
Question by:mcazabonnet
  • 3
5 Comments
 
LVL 4

Author Comment

by:mcazabonnet
ID: 40453607
Here is the output from the webservice
trying to build out a dropdown list from the webservice output to the kendodropdownlist

{"Table":[{"datestamp":"2014-08-28T00:00:00"},{"datestamp":"2014-08-27T00:00:00"},{"datestamp":"2014-08-26T00:00:00"},{"datestamp":"2014-08-25T00:00:00"},{"datestamp":"2014-08-24T00:00:00"},{"datestamp":"2014-08-23T00:00:00"},{"datestamp":"2014-08-22T00:00:00"},{"datestamp":"2014-08-21T00:00:00"},{"datestamp":"2014-08-20T00:00:00"},{"datestamp":"2014-08-18T00:00:00"},{"datestamp":"2014-08-11T00:00:00"},{"datestamp":"2014-08-04T00:00:00"},{"datestamp":"2014-08-01T00:00:00"},{"datestamp":"2014-07-28T00:00:00"},{"datestamp":"2014-07-01T00:00:00"},{"datestamp":"2014-06-01T00:00:00"},{"datestamp":"2014-05-01T00:00:00"},{"datestamp":"2014-04-01T00:00:00"},{"datestamp":"2014-03-01T00:00:00"},{"datestamp":"2014-02-01T00:00:00"},{"datestamp":"2014-01-01T00:00:00"},{"datestamp":"2013-12-01T00:00:00"},{"datestamp":"2013-11-01T00:00:00"},{"datestamp":"2013-10-01T00:00:00"},{"datestamp":"2013-09-01T00:00:00"},{"datestamp":"2013-08-01T00:00:00"}]}
0
 
LVL 4

Author Comment

by:mcazabonnet
ID: 40470434
$.ajax({
            type: "POST",
            url: "CGCSService.asmx/DropDownListDates",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (result) {
                var data = result.d;
                var obj = JSON.parse(data);
                var datesobj = [];
                datesobj.push({ "name": 'Most Current', "value": 'Most Current' });
                $.each(obj.Table, function (idx, item) {
                    var shortdate = item.datestamp.split('T');
                    datesobj.push({ "name": shortdate[0], "value": shortdate[0] });
                });
                var dates = $("#dates").kendoDropDownList({
                    change: function () {
                        datechange(dates.value());
                    },
                    optionLabel: "Select date...",
                    dataTextField: "name",
                    dataValueField: "value",
                    dataSource: datesobj
                }).data("kendoDropDownList");
            }
        });
0
 
LVL 58

Expert Comment

by:Gary
ID: 40522217
I've requested that this question be deleted for the following reason:

The question has either no comments or not enough useful information to be called an "answer".
0
 
LVL 4

Accepted Solution

by:
mcazabonnet earned 0 total points
ID: 40522216
I was able to address my issue
Here is the solution

$.ajax({
            type: "POST",
            url: "CGCSService.asmx/DropDownListDates",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (result) {
                var data = result.d;
                var obj = JSON.parse(data);
                var datesobj = [];
                datesobj.push({ "name": 'Most Current', "value": 'Most Current' });
                $.each(obj.Table, function (idx, item) {
                    var shortdate = item.datestamp.split('T');
                    datesobj.push({ "name": shortdate[0], "value": shortdate[0] });
                });
                var dates = $("#dates").kendoDropDownList({
                    change: function () {
                        datechange(dates.value());
                    },
                    optionLabel: "Select date...",
                    dataTextField: "name",
                    dataValueField: "value",
                    dataSource: datesobj
                }).data("kendoDropDownList");
            }
        });
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It is becoming increasingly popular to have a front-page slider on a web site. Nearly every TV website,  magazine or online news has one on their site, and even some e-commerce sites have one. Today you can use sliders with Joomla, WordPress or …
In this tutorial I will show you how to provide a dynamic RTF document on your website generated with data from your database. For this tutorial you will need Microsoft Word or WordPad, WhizBase and Microsoft Access. In this tutorial I will show …
The viewer will learn how to count occurrences of each item in an array.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now