Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Counting Records from Temp Table

Author  Topic 

escapist
Starting Member

3 Posts

Posted - 2004-07-27 : 02:17:11
Hey, if anyone here can help me with this it would save my life. Okay maybe not that, but it could save my job. In taking some code and adapting it to my needs I'm wondering if I've missed something I needed. I admitedly don't know enough about .NET or SQL 2k Stored Procedure Programming....anyways here is my situation. I have this SP I've ran though lots and lots of bugs, and I'm hung on this one. My Output is comming up DBNull. Here is the code (and yes I know it should pull 6 records right now). Ignore the ALTER PROC.
------------------------------------------------------


ALTER PROCEDURE dbo.sp_GetAllTicketsPaged
@CurrentPage int,
@PageSize int,
@TotalRecords int output
AS
--Create a temp table to hold the current page of data
--Add and ID column to count the records
CREATE TABLE #TempTable
(
ID int IDENTITY PRIMARY KEY,
Ticket_ID INT,
Ticket_Author_ID INT,
Creation_Date datetime,
Tech_Start_DT datetime,
Issue varchar (200),
Part_ID varchar (12),
Priority varchar (20),
Status varchar (20),
NotesPublic varchar (2000),
NotesPrivate varchar (4000),
Company varchar (60),
ContactPerson varchar (60),
Phone varchar (14),
eMail varchar (90)
)
--Fill the temp table with the Customers data
INSERT INTO #TempTable
(
Ticket_ID,
Ticket_Author_ID,
Creation_Date,
Tech_Start_DT,
Issue,
Part_ID,
Priority,
Status,
NotesPublic,
NotesPrivate,
Company,
ContactPerson,
Phone,
eMail
)
SELECT
dbo.Tickets.Ticket_ID,
dbo.Tickets.Ticket_Author_ID,
dbo.Tickets.Creation_Date,
dbo.Tickets.Tech_Start_DT,
dbo.Tickets.Issue,
dbo.Tickets.Part_ID,
dbo.Tickets.Priority,
dbo.Tickets.Status,
dbo.Tickets.NotesPublic,
dbo.Tickets.NotesPrivate,
dbo.TicketAuthor.Company,
dbo.TicketAuthor.ContactPerson,
dbo.TicketAuthor.Phone,
dbo.TicketAuthor.eMail
FROM
dbo.TicketAuthor FULL OUTER JOIN
dbo.Tickets ON dbo.TicketAuthor.Ticket_Author_ID = dbo.Tickets.Ticket_Author_ID
WHERE
dbo.TicketAuthor.Ticket_Author_ID = dbo.Tickets.Ticket_Author_ID
RETURN

--Create variable to identify the first and last record that should be selected
DECLARE @FirstRec int, @LastRec int
SELECT @FirstRec = (@CurrentPage - 1) * @PageSize
SELECT @LastRec = (@CurrentPage * @PageSize + 1)
--Select one page of data based on the record numbers above
SELECT
*
FROM
#TempTable
WHERE
ID > @FirstRec
AND
ID < @LastRec
--Return the total number of records available as an output parameter
SELECT @TotalRecords = COUNT(*) FROM #TempTable

------------------------------------------------------Need more code? Need more Info? You Can actually help me? Let me know. And my good Karma and fortune smile upon you today.

Oh and the Original SP can be found here. [url]http://www.dotnetjunkies.com/Tutorial/EA868776-D71E-448A-BC23-B64B871F967F.dcik[/url]

mohdowais
Sheikh of Yak Knowledge

1456 Posts

Posted - 2004-07-27 : 03:06:11
Are you using the SQLCommand.ExecuteReader() function to execute this procedure? If I recall correctly, you need to close the reader returned by the function to access the Output parameter. Just to be on the safe side, have you defined the parameter direction as InputOutput in your .NET code?

OS
Go to Top of Page

escapist
Starting Member

3 Posts

Posted - 2004-07-27 : 10:41:24
Here is the applicable ASP.NET VB Code:
----------------------------------------------------

Dim PageSize As Integer = 3
Dim myConnection As SqlConnection = New SqlConnection(ConString)
Dim myCommand As SqlCommand = New SqlCommand("sp_GetAllTicketsPaged", myConnection)
myCommand.CommandType = CommandType.StoredProcedure
myCommand.Parameters.Add(New SqlParameter("@CurrentPage", SqlDbType.Int)).Value = _currentPageNumber
myCommand.Parameters.Add(New SqlParameter("@PageSize", SqlDbType.Int)).Value = PageSize
myCommand.Parameters.Add(New SqlParameter("@TotalRecords", SqlDbType.Int)).Direction = ParameterDirection.Output
Try
myConnection.Open()
DataList1.DataSource = myCommand.ExecuteReader()
DataList1.DataBind()
Finally
myConnection.Close()
End Try
CurrentPage.Text = _currentPageNumber.ToString()
Dim _totalPages As Double = 1
If Not Page.IsPostBack Then
Dim _totalRecords As Int32 = CType(myCommand.Parameters("@TotalRecords").Value, Int32)
_totalPages = _totalRecords / PageSize
TotalPages.Text = (System.Math.Ceiling(_totalPages)).ToString()
Else
_totalPages = Double.Parse(TotalPages.Text)
End If

----------------------------------------------------
Go to Top of Page

escapist
Starting Member

3 Posts

Posted - 2004-07-27 : 11:44:15
Well my good friend Andy Green of [url]andrewdgreen.com[/url] found the error. Take out the RETURN from the SP and it runs PERFECT.
Go to Top of Page
   

- Advertisement -