RESOLVED ..
This was allowed as I was making a stupid mistake. I did not initialize the String
Builder StringBuilder sb = new StringBuilder ();
I am using the following CTE request on my website to retrieve data using C #. But it gives an error in the if statement inside the loop
ERROR MESSAGE
The System.NullReferenceException was not handled by the user code Message = The object reference was not installed in the object instance.
String strSqlCTE;
strSqlCTE= " DECLARE @PageId int ";
strSqlCTE += " SET @PageId =" + pageid + "; ";
strSqlCTE += " WITH RecursiveTable (PageId, PageName, PageInternalLink, PageInternalLinkUrl, PageInheritance, Level) ";
strSqlCTE += " AS(SELECT tt.PageId, tt.PageName, tt.PageInternalLink, tt.PageInternalLinkUrl, tt.PageInheritance, 0 AS Level ";
strSqlCTE += " FROM pg_Pages AS tt WHERE PageId = @PageId ";
strSqlCTE += " UNION ALL ";
strSqlCTE += " SELECT tt.PageId, tt.PageName, tt.PageInternalLink, tt.PageInternalLinkUrl, tt.PageInheritance, Level + 1 ";
strSqlCTE += " FROM pg_Pages AS tt ";
strSqlCTE += " INNER JOIN RecursiveTable rt ON rt.PageInheritance = tt.PageId ) ";
strSqlCTE += " SELECT * FROM RecursiveTable ORDER BY Level DESC ";
DataSet dsBC = new DataSet();
dsBC = DataProvider.Connect_Select(strSqlCTE);
if (dsBC.Tables[0].Rows.Count > 0)
{
int RowCount = dsBC.Tables[0].Rows.Count;
StringBuilder sb = null;
for ( int i = 0; i <= RowCount; i++)
{
if (i == RowCount)
{
sb.Append("<a href='" + dsBC.Tables[0].Rows[0]["PageInternalLinkUrl"].ToString() + "'>" + dsBC.Tables[0].Rows[0]["PageName"].ToString().ToUpper() + "</a>");
}
else
{
sb.Append("<a href='" + dsBC.Tables[0].Rows[0]["PageInternalLinkUrl"].ToString() + "'></a> ● ");
}
}
ltrBreadCrumb.Text = sb.ToString();
}
My CTE request works, and below is the actual request
DECLARE @PageId int
SET @PageId = 31;
WITH RecursiveTable (PageId, PageName, PageInternalLink, PageInternalLinkUrl, PageInheritance, Level)
AS(
SELECT tt.PageId, tt.PageName, tt.PageInternalLink, tt.PageInternalLinkUrl, tt.PageInheritance, 0 AS Level
FROM pg_Pages AS tt
WHERE PageId = @PageId
UNION ALL
SELECT tt.PageId, tt.PageName , tt.PageInternalLink, tt.PageInternalLinkUrl, tt.PageInheritance, Level + 1
FROM pg_Pages AS tt
INNER JOIN RecursiveTable rt ON rt.PageInheritance = tt.PageId
)
SELECT * FROM RecursiveTable ORDER BY Level DESC
This works well when run in a management studio, but after executing the instruction, generate an error
sb.Append("<a href='" + dsBC.Tables[0].Rows[0]["PageInternalLinkUrl"].ToString() + "'></a> ● ");
dsBC.Tables [0]. Rows.Count . , , , , CTE.
- , , .