I have created SQL Function for getting dates into my Gridview, So I am passing start date and end date to my Function but getting error like below.
The maximum recursion 100 has been exhausted before statement completion
If counts date greater than 100 means getting the above error, if below 100 dates means not getting any error. I have records of more than 100 days.
Below is my Data logic.
public DataTable GetDates(DateTime StartDate, DateTime EndDate)
{
DataTable dt = new DataTable();// order by dt desc
ConnectMethod();
cmd = new SqlCommand("select dt from dbo.ExplodeDates(@StartDate,@EndDate) order by dt desc", con);
try
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@StartDate", StartDate);
cmd.Parameters.AddWithValue("@EndDate", EndDate);
cmd.ExecuteScalar();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
return dt;
}
catch (Exception ex)
{ return dt; }
finally
{
con.Close();
con.Dispose();
cmd.Dispose();
}
}
I am getting this error on cmd.ExecuteScalar(); line. Below is my SQL Function.
CREATE FUNCTION [dbo].[ExplodeDates](
@startdate datetime
, @enddate datetime
)
RETURNS TABLE
AS
RETURN
WITH DATES AS(
SELECT @startdate AS dt
UNION ALL
SELECT DATEADD(D, 1, dt) FROM DATES WHERE DT<@enddate
)
SELECT * FROM DATES
GO
Please help to out this Issue looking result since morning.