Hi all,
I'm trying to develop an ASP.NET 2.0 website and am running into some
real problems with what I thought would be a relatively simple thing
to do.
In a nutshell, I'm stuck on trying to display data in a "GridView"
which is tied to an "ObjectDataSour ce".
In turn, this ObjectDatasourc e gets it's data from a strongly-typed
business object within my code.
I'm also trying to use custom paging, so my class has correctly
implemented methods to return the total count of records, and also the
method which retrieves the actual page of records is accepting two
parameters for the startRowIndex and maximumRows. I do NOT need any
sorting capability for this gridview, nor do I need any updating,
deleting, selecting functionality, either. I simply need to display
all the relevant records from the database (via my object), paged
(using SQL2005's ROW_NUMBER style paging).
I have this working beautifully when I use a strongly-typed dataset
for the ObjectDatasourc e's data, however, when I switch to using a
"business object", the Gridview doesn't seem to want to display any
data.
I can step through my code, and confirm that I am retrieving some
records from the database, which is then "returned" from the
"selectmeth od" function call as a Generic List (ie. List(of T)).
Below, I've pasted some code that highlights my problem (I re-did the
code to focus on this one problem - in my real code, my DAL and BLL
are separate, whereas here they're in the same class, just for
brevity's sake). If your interested, you can download the entire
zipped up webproject (about 580kb) from here:
http://putstuff.putfile.com/17957/6448935
Has anyone encountered this problem before?? Does anyone know what
I'm doing wrong?? (I'm convinved it's something really simple that
I'm missing).
Any/all replies are greatly appreciated.
Regards,
Greg
The database has one table, as follows:
CREATE TABLE [dbo].[TestTable] (
[UniqueID] [uniqueidentifie r] NOT NULL ,
[Name] [varchar] (50) NOT NULL ,
[BirthDate] [datetime] NOT NULL ,
[Comment] [varchar] (100) NOT NULL
)
There's 2 stored procedures, as follows:
ALTER PROCEDURE dbo.usp_GetTest Table
(
@startRowIndex int,
@maximumRows int
)
AS
SELECT UniqueID, [Name], BirthDate, Comment
FROM
(SELECT UniqueID, [Name], BirthDate, Comment, ROW_NUMBER()
OVER(ORDER BY BirthDate) AS RowNum
FROM TestTable
) AS TestTableList
WHERE RowNum BETWEEN @startRowIndex AND (@startRowIndex +
@maximumRows) - 1
and:
ALTER PROCEDURE dbo.usp_GetTest TableCount
AS
SELECT
COUNT(*)
FROM
TestTable
The "custom class" is defined as follows:
Imports Microsoft.Visua lBasic
Imports System.Collecti ons.Generic
Imports system.data
Imports System.Data.Sql
Imports system.Data.Sql Client
'
'
'
Public Class TestTableClass
'
'
Private m_UniqueID As String = ""
Private m_Name As String = ""
Private m_BirthDate As DateTime = Nothing
Private m_Comment As String = ""
'
'
Public Property UniqueID() As String
Get
Return m_UniqueID
End Get
Set(ByVal value As String)
m_UniqueID = value
End Set
End Property
'
Public Property Name() As String
Get
Return m_Name
End Get
Set(ByVal value As String)
m_Name = value
End Set
End Property
'
Public Property BirthDate() As DateTime
Get
Return m_BirthDate
End Get
Set(ByVal value As DateTime)
m_BirthDate = value
End Set
End Property
'
Public Property Comment() As String
Get
Return m_Comment
End Get
Set(ByVal value As String)
m_Comment = value
End Set
End Property
'
'
'
Public Function GetTestTableCou nt() As Integer
'
Dim oSQLConn As SqlConnection
Dim oSQLCmd As SqlCommand
Dim intResult As Integer = 0
'
oSQLConn = New
SqlConnection(C onfigurationMan ager.Connection Strings("SiteDB ").ToString )
oSQLConn.Open()
'
oSQLCmd = New SqlCommand
oSQLCmd.Connect ion = oSQLConn
oSQLCmd.Command Type = CommandType.Sto redProcedure
oSQLCmd.Command Text = "usp_GetTestTab leCount"
'
intResult = oSQLCmd.Execute Scalar
'
oSQLConn.Close( )
'
End Function
'
'
Public Function GetTestTable(By Val startRowIndex As Integer, ByVal
maximumRows As Integer) As List(Of TestTableClass)
'
Dim oSQLConn As SqlConnection
Dim oSQLCmd As SqlCommand
Dim oSQLParam As SqlParameter
Dim oDR As SqlDataReader
Dim mTTC As TestTableClass
Dim mColl As List(Of TestTableClass) = New List(Of
TestTableClass)
Dim intResult As Integer = 0
'
oSQLConn = New
SqlConnection(C onfigurationMan ager.Connection Strings("SiteDB ").ToString )
oSQLConn.Open()
'
oSQLCmd = New SqlCommand
oSQLCmd.Connect ion = oSQLConn
oSQLCmd.Command Type = CommandType.Sto redProcedure
oSQLCmd.Command Text = "usp_GetTestTab le"
'
oSQLParam = New SqlParameter
oSQLParam.SqlDb Type = SqlDbType.Int
oSQLParam.Direc tion = ParameterDirect ion.Input
oSQLParam.Param eterName = "startRowIn dex"
oSQLParam.Value = startRowIndex
oSQLCmd.Paramet ers.Add(oSQLPar am)
'
oSQLParam = New SqlParameter
oSQLParam.SqlDb Type = SqlDbType.Int
oSQLParam.Direc tion = ParameterDirect ion.Input
oSQLParam.Param eterName = "maximumRow s"
oSQLParam.Value = maximumRows
oSQLCmd.Paramet ers.Add(oSQLPar am)
'
oDR = oSQLCmd.Execute Reader()
'
While oDR.Read
'
mTTC = New TestTableClass
mTTC.UniqueID = oDR.Item("Uniqu eID").ToString
mTTC.Name = oDR.Item("Name" ).ToString
mTTC.BirthDate = oDR.Item("Birth Date")
mTTC.Comment = oDR.Item("Comme nt").ToString
'
mColl.Add(mTTC)
'
End While
'
oSQLConn.Close( )
'
Return mColl
'
End Function
'
'
End Class
And the "Default.as px" page (which has nothing in it's code-behind) is
defined as follows:
<%@ Page Language="VB" AutoEventWireup ="false"
CodeFile="Defau lt.aspx.vb" Inherits="_Defa ult" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitl ed Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ObjectData Source ID="ObjectDataS ource1" runat="server"
TypeName="TestT ableClass" SelectCountMeth od="GetTestTabl eCount"
SelectMethod="G etTestTable" EnablePaging="T rue">
</asp:ObjectDataS ource>
<asp:GridView ID="GridView1" runat="server" AllowPaging="Tr ue"
AutoGenerateCol umns="False"
DataSourceID="O bjectDataSource 1">
<Columns>
<asp:BoundFie ld DataField="Uniq ueID"
HeaderText="Uni queID" SortExpression= "UniqueID" />
<asp:BoundFie ld DataField="Birt hDate"
HeaderText="Bir thDate" SortExpression= "BirthDate" />
<asp:BoundFie ld DataField="Name " HeaderText="Nam e"
SortExpression= "Name" />
<asp:BoundFie ld DataField="Comm ent"
HeaderText="Com ment" SortExpression= "Comment" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>