Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does IDataReader lose a row?

I am facing this problem. I have a stored procedure which returns 6 rows when I execute it.

But when I am retrieving the rows in my app by using ExecuteReader, it only returns only 5 rows. Why is it losing a row??

My stored procedure consists of 5 union statements which are getting filled from a single table:

 dbase.AddInParameter(cmd, "@LoginUser", DbType.String, UserID);

   try
   {
      using (IDataReader  dr = dbase.ExecuteReader(cmd))
      if (dr.Read())
      {
         dt = new DataTable("DashBoard");
         dt.Load(dr);
      }
   }

dbase is my database object. And cmd is the SqlCommand used to call the stored procedure.

UserID is parameter is passing

Stored procedure code is:

ALTER PROCEDURE [dbo].[USP_ViewAdminDashBoard](@LoginUser varchar(75)) 
    -- Add the parameters for the stored procedure here
AS
BEGIN

    SET NOCOUNT ON;
    SET DATEFORMAT DMY;
    DECLARE @LastLoginDate  as DateTime

        Select @LastLoginDate = dbo.UDF_GetLastLoginByUser(@LoginUser)
    Select 'Last Login Date', convert(varchar(12),@LastLoginDate,105)

    Union 
    Select  'Nos. Records pending for Upload' as Title, convert(varchar(5),COUNT(s.BatchID)) Total from  dbo.BREGISTRATIONENTRY s, Dbo.TBL_iBATCH B
    where  B.BatchID = s.BatchID And b.Forwarded = 0 and b.isBatchClosed = 1
END
like image 354
joshua Avatar asked Dec 06 '11 07:12

joshua


1 Answers

Your first dr.Read is advancing to the first row. The DataTable.Load is reading the remaining rows but not the first row

Use HasRows to test for existence and don't use Read before the DataTable.Load

Edit:

Just load the DataTable without testing first: then test on the DataTable. There is no HasRows in the IDataReader interface.

like image 200
gbn Avatar answered Sep 20 '22 12:09

gbn