Alright...
The schema of Categories is:
ID int PK
Name varchar(50)
Left int
Right int
On a side note, I am facing more issues now... I keep getting the error "Object reference not set to an instance of an object" when trying to add a new node using the following function:
//Insert a new node after a certain node
public bool AddNodeAfter(string nodeName, string newNodeName)
{
IQueryable<Category> categories = null;
try
{
Category node = db.Categories.FirstOrDefault(c => c.Name == nodeName);
categories = db.Categories.Where(c => c.Right > node.Right && c.Left > node.Right);
if (categories != null)
{
foreach (Category categ in categories) //when debugging I noticed that categories is what is throwing the exception
{
categ.Left += 2;
categ.Right += 2;
}
Category newNode = new Category
{
ID = 11,
Name = newNodeName,
Left = node.Right + 1,
Right = node.Right + 2
};
db.AddToCategories(newNode);
if (db.SaveChanges() > 0)
return true;
}
else
{
MessageBox.Show("Categories variable is set to null", "Error!");
}
}
catch (NullReferenceException e)
{
MessageBox.Show(e.Message + e.InnerException, "Error!");
return false;
}
return false;
}
But I do not understand why this is happening. I already checked if "categories" is null, which is not the case. But this exception is still being thrown for some reason.
Any thoughts?