Click here to Skip to main content
15,905,612 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
hi,

I have used an StoredProcedure to return some records from an table. Hence i tried to saved it in an temp table and return those records from temp table to my application side.

I have tried as above but i got only EMPTY datareader with certain fields(columns) i have generated for temp table in SP.

Here is my SP,

SQL
ALTER procedure [dbo].[SP_SELECT_BILLDELETE]
As
Begin
SET NOCOUNT ON
IF EXISTS
(
SELECT *
FROM tempdb.dbo.sysobjects
WHERE ID = OBJECT_ID(N'tempdb..#MyTempTable')
)
BEGIN
DROP TABLE #MyTempTable
END
--Here i have Created an TempTable
select * into #MyTempTable from BillDelete where CONVERT(date,billdate,101) = CONVERT(date,GETDATE(),101) union
select * from WBillDelete  where CONVERT(date,billdate,101) = CONVERT(date,GETDATE(),101)

return
End



Here is my C# code to retrieve records from SP


C#
sqlcon.Open();
               sqlcom = new SqlCommand("SP_SELECT_BILLDELETE", sqlcon);
               sqlcom.CommandType = CommandType.StoredProcedure;

               sqldr = sqlcom.ExecuteReader();
               if(sqldr.Read()) //Here it passed this condition and get into it
               {
                   dtable = new DataTable();

                   dtable.Load(sqldr); //Here i'm getting structure alone with no records
               }

               return dtable;
Posted

change the procedure to this..

SQL
ALTER procedure [dbo].[SP_SELECT_BILLDELETE]
As
Begin
SET NOCOUNT ON
IF EXISTS
(
SELECT *
FROM tempdb.dbo.sysobjects
WHERE ID = OBJECT_ID(N'tempdb..#MyTempTable')
)
BEGIN
DROP TABLE #MyTempTable
END
--Here i have Created an TempTable
select * into #MyTempTable from BillDelete where CONVERT(date,billdate,101) = CONVERT(date,GETDATE(),101) 
union
select * from WBillDelete  where CONVERT(date,billdate,101) = CONVERT(date,GETDATE(),101)

select * from #MyTempTable 

End


add select * from #MyTempTable at the bottom before end.

hope it works..
 
Share this answer
 
v2
Comments
J.Karthick 4-Jan-12 6:57am    
Sorry Karthik, still it retrieves the empty table only.
I thought the records from temp table got cleared.

Even i executed it in Query Browser it retrieves the records...But i can't use it into my Application side
baskaran chellasamy 28-Jul-12 5:55am    
same problem for my application.did you able solve the problem? if so please let me know.
you can get datatable from sqldatareader like this..

C#
SqlDataReader dr = cmd.ExecuteReader();
 DataTable dt = new DataTable();
 dt.Load(dr);


try this..
 
Share this answer
 
Comments
J.Karthick 4-Jan-12 8:31am    
This is what i used before...but not works..
Please check my solution
Instead of datereader i used dataAdapter like below in application side...

SqlDataAdapter sqlda = new SqlDataAdapter(sqlcom);

               dtable = new DataTable();
               sqlda.Fill(dtable);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900