1

This is my stored procedure

 CREATE OR ALTER PROCEDURE [dbo].[sp_TADashboardVoid] 
        @PartnerId INT,
        @UserId BIGINT,
        @TAType INT = 0,
        @Borough INT = 0,
        @LandlordId INT = 0,
        @AllocationStartDate DATE = NULL,
        @AllocationEndDate DATE = NULL,
        @PropertyRef VARCHAR(100) = NULL,
        @PropertyType INT = 0,
        @PropertyId VARCHAR(100) = NULL,
        @PropertyAddress VARCHAR(200) = NULL,
        -- @LandlordProperty VARCHAR(100) = NULL  
        @PropertyStatusId INT = NULL,
        @HouseholdId VARCHAR(10) = NULL,
        @ReferenceID VARCHAR(50) = NULL,
        @VoidStartDate DATE = NULL,
        @VoidEndDate DATE = NULL
    AS
    BEGIN
       
DECLARE @demotest TABLE (

      
      PropertyID NVARCHAR(MAX)
      ,App_id INT
      
      ,Rent DECIMAL(18, 2)
      ,RoomId INT
       ,CreatedDate Date
      ,StartDate Date
      ,EndDate Date
       ,VoidStartDate  DATE
      ,VoidEndDate  DATE
      --,VoidLoss NVARCHAR(MAX)
)


insert  into @demotest


select PropertyId,App_Id,Rent,RoomId,CreatedDate,StartDate,EndDate,EndDate+1 as VoidStartDate,
LEAD(StartDate) OVER (Partition BY PropertyId ORDER by Propertyid) As VoidEndDate


from tbl_AssignAppToProperty 




SELECT  ROW_NUMBER() OVER (
            --ORDER BY  A.Reference_id DESC
            ORDER BY dt.PropertyId
            ) AS RowNumber

          , app.Reference_ID as ApplicationRef 
          , A.Reference_ID as PropertyRefId
          , A.ID as PropertyID 
      ,vtp.TypeofProperty 
      ,vPN.PropertyName AS PropertyName
      ,c.title AS Landlord
      ,P.FirstName 
      ,P.LastName 
      ,Ho.HouseholdLoginId AS HouseholdId
      ,vrt.RentType as RentFrequency
      ,dt.Rent 
      ,A.Apptype 
      ,A.FormId  --added due to error
      , A.session_guid


       ,CASE 
            WHEN 
            app.Reference_ID  IS NULL Then  A.DateOfSubmission

            ELSE dt.VoidStartDate
            END AS VoidStartDate
            


      ,Case
          WHEN
          dt.VoidEndDate is NULL Then getdate()
          Else dt.VoidEndDate
          END AS VoidEndDate

    
          


      ,datediff(ww,dt.VoidStartDate,dt.VoidEndDate) * dt.Rent AS VoidLoss




      from tbl_Application A
     INNER JOIN tbl_Form F ON F.id = A.FormId
     
      left join @demotest dt on dt.PropertyId = A.ID

     left join tbl_Application APP on dt.App_Id = APP.ID
         
     Left JOIN tbl_household HO ON HO.Household_ID = APP.Household_Id
     
     left JOIN v_TypeOfProperty vTP ON vTP.App_Id = A.ID
     INNER JOIN v_PropertyName vPN ON A.ID = vPN.App_Id         --record dropped from 78 to 57 to display only properties with name
     --INNER JOIN tbl_ApplicationSubPortal ASS ON A.ID = ASS.AppID
     INNER JOIN v_PropertyAddress vPA ON A.Session_Guid = vPA.MapGuid
    
    LEFT JOIN tbl_applicationlandlord AL ON AL.AppId = A.id
    LEFT JOIN tbl_choice c ON C.id = AL.LandlordId

     --LEFT JOIN v_PropertyType vPT ON vPT.App_Id = A.ID

     
     LEFT JOIN v_Person P ON P.AppId = dt.App_Id
        AND P.HouseholdType = 'M'
     LEFT JOIN v_RentType VRT on VRT.APP_ID = A.ID

     where A.AppType='pro' --and A.Formid=803        

     AND F.PartnerId = @PartnerId
        AND (
            (@TAType = 0)
            OR (vTP.TypeOfPropertyId = @TAType)
            )

        AND (
            @PropertyRef IS NULL
            OR A.Reference_ID LIKE '%' + @PropertyRef + '%'
            )



        
        AND (
            @PropertyStatusId IS NULL
            OR (A.App_status = @PropertyStatusId)
            )
        AND (
            @PropertyId IS NULL
            OR A.Reference_ID LIKE '%' + @PropertyId + '%'
            )
        AND (
            @PropertyAddress IS NULL
            OR ((isnull(vPA.FlatNo, '') + ' ' + isnull(vPA.BuildingName, '') + ' ' + isnull(vPA.StreetName, '') + ' ' + isnull(vPA.TownCity, '') + ' ' + isnull(vPA.County, '') + ' ' + isnull(vPA.PostCode, '')) LIKE '%' + @PropertyAddress + '%')
            )



        AND (
              @HouseholdId IS NULL
              OR HO.HouseholdLoginId LIKE '%' + @HouseholdId + '%'
              )

        AND (
             @ReferenceID IS NULL
             OR APP.Reference_ID LIKE '%' + @ReferenceID + '%'
            )


    




        AND (
            (
                CONVERT(DATE, @VoidStartDate, 103) IS NULL
                OR CONVERT(DATE, @VoidEndDate, 103) IS NULL
                )
            OR (
                (
                    CONVERT(DATE, dt.VoidStartDate, 103) BETWEEN CONVERT(DATE,@VoidStartDate, 103)
                        AND CONVERT(DATE,  @VoidEndDate, 103)
                    )
                AND (
                    CONVERT(DATE, dt.VoidEndDate, 103) BETWEEN CONVERT(DATE, @VoidStartDate, 103)
                        AND CONVERT(DATE,  @VoidEndDate, 103)
                    )
                )
            )
    
END

This is my BindVoidData method

public System.Data.DataSet BindVoidData(int partnerId, long userId, string taType, string Borough, string landlord, string propertyRef, string propertyType, string propertyAddress , string propertyStatus)
{
    SqlCommand sqlCommand = new SqlCommand();

    SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
    DataSet dataTable = new DataSet();

    SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["HousingRegister"].ConnectionString);

    try
    {
        sqlCommand = new SqlCommand("sp_TADashboardVoid", connection);

        sqlCommand.Parameters.Add(new SqlParameter("@PartnerId", partnerId));
        sqlCommand.Parameters.Add(new SqlParameter("@UserId", userId));
        sqlCommand.Parameters.Add(new SqlParameter("@TAType", taType));
        sqlCommand.Parameters.Add(new SqlParameter("@Borough", Borough));
        sqlCommand.Parameters.Add(new SqlParameter("@LandlordId", landlord));
        sqlCommand.Parameters.Add(new SqlParameter("@PropertyId", propertyRef));
        sqlCommand.Parameters.Add(new SqlParameter("@PropertyType", propertyType));
        sqlCommand.Parameters.Add(new SqlParameter("@PropertyAddress", propertyAddress));
        sqlCommand.Parameters.Add(new SqlParameter("@PropertyStatusId", propertyStatus));
        sqlCommand.CommandType = CommandType.StoredProcedure;

        sqlDataAdapter.SelectCommand = sqlCommand;

        sqlDataAdapter.Fill(dataTable);
    }
    catch (Exception ex)
    {
        throw ex;
    }
    finally
    {
        sqlCommand.Dispose();
        connection.Close();
    }

    return dataTable;
}

The problem that I am facing is that the datatable is not returning me any rows due to which there are no records on Excel sheet, but when I tested the stored procedure using exec on SQL Server, it is returning around 200 rows.

Can anyone please help me out on this as I am not sure how to proceed.

Tiger901
  • 11
  • 2
  • Side note: you should **not** use the `sp_` prefix for your stored procedures. Microsoft has [reserved that prefix for its own use (see *Naming Stored Procedures*)](http://msdn.microsoft.com/en-us/library/ms190669%28v=sql.105%29.aspx), and you do run the risk of a name clash sometime in the future. [It's also bad for your stored procedure performance](http://www.sqlperformance.com/2012/10/t-sql-queries/sp_prefix). It's best to just simply avoid `sp_` and use something else as a prefix - or no prefix at all! – marc_s May 19 '22 at 10:37
  • Aside... there seem to be a lot of `String` parameters on `BindVoid` that should be other data types to match the stored procedure parameters. After fixing that also consider other overloads of the `SqlParameter` constructor that allow you to be specific with data types, e.g.: `sqlCommand.Parameters.Add(new SqlParameter("@ LandlordId", SqlDbType.Int)).Value = landlord;` or even `sqlCommand.Parameters.Add("@ LandlordId", SqlDbType.Int).Value = landlord;` – AlwaysLearning May 19 '22 at 11:02
  • Aside... what's the point of `catch (Exception ex) { throw ex; }`? This pattern obscures the stack trace of the original exception. Don't catch it if you're not doing anything with it (such as logging or reporting), or alternatively just use `throw;` instead of `throw ex;` There's even a code quality rule about this, [CA2200: Rethrow to preserve stack details](https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca2200). – AlwaysLearning May 19 '22 at 11:06
  • Without even seeing your query we can't begin to help you. Please show us the SQL query. There does appear to be quite a few data type mismatches, which may be affecting things. What happens if you correct those? – Charlieface May 19 '22 at 11:12
  • Another side point: Dispose your connection, command and adapter with `using`. – Charlieface May 19 '22 at 11:13
  • 1
    Try adding "set nocount on;" as the first statement in your procedure. – SMor May 19 '22 at 11:42
  • @Charlieface I have added the sql query now. – Tiger901 May 19 '22 at 14:31

1 Answers1

0

Here is the code try this out, I Just edited it into my style of code

    public DataSet BindVoidData(int partnerId, long userId, string taType, string Borough, string landlord, string propertyRef, string propertyType, string propertyAddress, string propertyStatus)
    {
        SqlDataAdapter da = new SqlDataAdapter();
        DataSet Ds = new DataSet();
        try
        {
            var constring = System.Configuration.ConfigurationManager.ConnectionStrings["HousingRegister"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constring))
            {

                if (con.State == ConnectionState.Closed)
                {
                    con.Open();
                }

                using (SqlCommand cmd = new SqlCommand("sp_TADashboardVoid", con))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    //Add the DataType Based on Your StoredProcedure
                    cmd.Parameters.Add("@PartnerId", SqlDbType.Int, 20);

                    //After Adding All Data Type Define Value
                    cmd.Parameters["@PartnerId"].Value = partnerId;
                    da = new SqlDataAdapter(cmd);
                    da.Fill(Ds);

                    cmd.Dispose();
                }
                con.Close();
            }

            //Return Data Set
            return Ds;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

Here is the SQL Server Data Types and Their .NET Framework Equivalents for Add DataType in SqlCommand Link

Karthikcbe
  • 295
  • 2
  • 12