Imports Telerik.Web.UI
Imports System.Data.SqlClient
Imports System.Data
Imports System.Web.Services

Partial Class _Default
    Inherits System.Web.UI.Page
    <WebMethod()> _
    Public Shared Function GetProductCount() As Integer
        Return 50000
    End Function

    <WebMethod()> _
    Public Shared Function GetProductData(ByVal startRowIndex As Integer, ByVal maxRows As Integer) As IEnumerable(Of Product)
        Dim ret As List(Of Product) = New List(Of Product)
        Dim [select] As String = "SELECT ID, ProductName, QuantityPerUnit, UnitPrice, UnitsInStock FROM LargeProducts WHERE ID > @StartIndex AND ID <= @EndIndex"

        Dim parameters As New Dictionary(Of String, Object)()
        parameters("StartIndex") = Math.Max(startRowIndex, 0)
        parameters("EndIndex") = Math.Max(startRowIndex + maxRows, maxRows)
        Dim data As DataTable = ExecuteSelect([select], parameters, ConfigurationManager.ConnectionStrings("TelerikConnectionString").ConnectionString)

        For Each row As DataRow In data.Rows
            Dim product As New Product()
            product.ID = DirectCast(row("ID"), Integer)
            product.ProductName = DirectCast(row("ProductName"), String)
            product.QuantityPerUnit = DirectCast(row("QuantityPerUnit"), String)
            product.UnitPrice = DirectCast(row("UnitPrice"), Decimal)
            product.UnitsInStock = DirectCast(row("UnitsInStock"), Short)

            ret.Add(product)
        Next

        Return ret
    End Function

    <WebMethod()> _
    Public Shared Function GetOrderCount() As Integer
        Return 200
    End Function

    <WebMethod()> _
    Public Shared Function GetOrderData(ByVal startRowIndex As Integer, ByVal maxRows As Integer) As IEnumerable(Of Order)
        Dim ret As List(Of Order) = New List(Of Order)
        Dim [select] As String = "SELECT * FROM (SELECT ROW_NUMBER() OVER (ORDER BY OrderID) AS 'ID', OrderID, CustomerID, OrderDate, ShipName, ShipCity, ShipCountry FROM Orders) AS OrderData WHERE OrderData.ID > @StartIndex AND OrderData.ID <= @EndIndex"

        Dim parameters As New Dictionary(Of String, Object)()
        parameters("StartIndex") = Math.Max(startRowIndex, 0)
        parameters("EndIndex") = Math.Max(startRowIndex + maxRows, maxRows)
        Dim data As DataTable = ExecuteSelect([select], parameters, ConfigurationManager.ConnectionStrings("NorthwindConnectionString").ConnectionString)

        For Each row As DataRow In data.Rows
            Dim order As New Order()
            order.ID = Convert.ToInt32(row("ID"))
            order.CustomerID = DirectCast(row("CustomerID"), String)
            order.OrderDate = DirectCast(row("OrderDate"), DateTime)
            order.ShipName = DirectCast(row("ShipName"), String)
            order.ShipCity = DirectCast(row("ShipCity"), String)
            order.ShipCountry = DirectCast(row("ShipCountry"), String)

            ret.Add(order)
        Next

        Return ret
    End Function

    Private Shared Function ExecuteSelect(ByVal selectCommand As String, ByVal parameters As Dictionary(Of String, Object), ByVal connectionString As String) As DataTable
        Dim MySqlConnection As New SqlConnection(connectionString)
        Dim MySqlDataAdapter As New SqlDataAdapter()

        MySqlDataAdapter.SelectCommand = New SqlCommand(selectCommand, MySqlConnection)

        For Each pair As KeyValuePair(Of String, Object) In parameters
            MySqlDataAdapter.SelectCommand.Parameters.Add(New SqlParameter(pair.Key, pair.Value))
        Next

        Dim data As New DataTable()
        MySqlConnection.Open()

        Try
            MySqlDataAdapter.Fill(data)
        Finally

            MySqlConnection.Close()
        End Try

        Return data
    End Function
End Class

Public Class Product
    Private _id As Integer
    Private _unitsInStock As Short
    Private _productName As String, _quantityPerUnit As String
    Private _unitPrice As Decimal

    Public Property ID() As Integer
        Get
            Return _id
        End Get
        Set(ByVal value As Integer)
            _id = value
        End Set
    End Property
    Public Property ProductName() As String
        Get
            Return _productName
        End Get
        Set(ByVal value As String)
            _productName = value
        End Set
    End Property
    Public Property QuantityPerUnit() As String
        Get
            Return _quantityPerUnit
        End Get
        Set(ByVal value As String)
            _quantityPerUnit = value
        End Set
    End Property
    Public Property UnitPrice() As Decimal
        Get
            Return _unitPrice
        End Get
        Set(ByVal value As Decimal)
            _unitPrice = value
        End Set
    End Property
    Public Property UnitsInStock() As Short
        Get
            Return _unitsInStock
        End Get
        Set(ByVal value As Short)
            _unitsInStock = value
        End Set
    End Property
End Class

Public Class Order
    Private _id As Integer
    Private _customerId As String
    Private _orderDate As DateTime
    Private _shipName As String
    Private _shipCity As String
    Private _shipCountry As String

    Public Property ID() As Integer
        Get
            Return _id
        End Get
        Set(ByVal value As Integer)
            _id = value
        End Set
    End Property
    Public Property CustomerID() As String
        Get
            Return _customerId
        End Get
        Set(ByVal value As String)
            _customerId = value
        End Set
    End Property
    Public Property OrderDate() As DateTime
        Get
            Return _orderDate
        End Get
        Set(ByVal value As DateTime)
            _orderDate = value
        End Set
    End Property
    Public Property ShipName() As String
        Get
            Return _shipName
        End Get
        Set(ByVal value As String)
            _shipName = value
        End Set
    End Property
    Public Property ShipCity() As String
        Get
            Return _shipCity
        End Get
        Set(ByVal value As String)
            _shipCity = value
        End Set
    End Property
    Public Property ShipCountry() As String
        Get
            Return _shipCountry
        End Get
        Set(ByVal value As String)
            _shipCountry = value
        End Set
    End Property
End Class