Har fortfarande problem att visa mina kategorier på ett snyggt sätt. Har skrivit om mitt php-skript till C#. När jag kör php-skriptet så fungerar det jättebra, men inte när jag kör C#-skriptet. Antagligen så retunerar metoden "objDb2.Query(sqlCategories2);" null. Det trodde jag med till en början men enligt databasen så ska det returneras en rad. du har glömt siffran 2 på hasrows.....: Ja så kan det vara när man har så pass lika variabler hehe.Object reference not set to an instance of an object
Så här ser min kod ut: protected void categoriesLabel_Load(object sender, EventArgs e)
{
int prevID = 0;
int firstElement = 0;
int lastID = 0;
int firstID = 0;
object[][] foo = new object[15][];
string returnString = "";
ArrayList cPath = new ArrayList();
int index = 0;
osAuction.Categories categories = new osAuction.Categories();
if (Request.QueryString["cat"] != null)
{
string[] cat = categories.SplitcPath(Request.QueryString["cat"]);
for (int k = 0; k < cat.Length; k++)
{
cPath.Add(cat[k]);
}
}
osAuction.Database.Database objDb = new osAuction.Database.Database();
OleDbDataReader objReader;
string sqlCategories = "SELECT categories.categories_id, categories.parent_id," +
"categories_description.categories_name FROM (categories INNER JOIN " +
"categories_description ON categories.categories_id = categories_description.categories_id) " +
"WHERE categories.parent_id = 0 ORDER BY categories.sort_order, categories_description.categories_name";
objReader = objDb.Query(sqlCategories);
while (objReader.Read())
{
foo[index] = new object[5] {objReader.GetString(2), objReader.GetInt32(1),
0, objReader.GetInt32(0), 0}; //[0]categories_name, [1]parent_id, [2]level,
//[3]categories_id, [4]nextID
/* Vad gör denna?
if (prevID != 0)
{
foo[prevID][4] = objReader.GetInt32(0);
}
prevID = objReader.GetInt32(0);
*/
// Vad gör denna?
if (firstElement == 0)
{
firstElement = objReader.GetInt32(0);
}
//
index++;
}
if (cPath.Count > 0)
{
osAuction.Database.Database objDb2 = new osAuction.Database.Database();
OleDbDataReader objReader2;
string newPath = "";
for (int j = 0; j < cPath.Count; j++)
{
prevID = 0;
firstID = 0;
int value = Convert.ToInt32(cPath[j]);
string sqlCategories2 = "SELECT categories.categories_id, categories.parent_id," +
"categories_description.categories_name FROM (categories INNER JOIN " +
"categories_description ON categories.categories_id = categories_description.categories_id) " +
"WHERE categories.parent_id = " + value + " ORDER BY categories.sort_order, categories_description.categories_name";
objReader2 = objDb2.Query(sqlCategories2);
if (objReader.HasRows)
{
newPath += cPath[j];
while (objReader2.Read())
{
foo[index] = new object[5] {objReader2.GetString(2) ,
objReader2.GetInt32(1), j+1, newPath + "_" + objReader2.GetInt32(0),
0}; //[0]categories_name, [1]parent_id, [2]level, [3]path, [4]nextID
/* Vad gör denna?
if (prevID != 0)
{
foo[prevID][4] = objReader2.GetInt32(0);
}
prevID = objReader2.GetInt32(0);
*/
/* Vad gör denna?
if (firstID == 0)
{
firstID = objReader2.GetInt32(0);
}
//
//Vad gör denna?
lastID = objReader2.GetInt32(0);
*/
}
/* Vad gör denna?
foo[lastID][4] = foo[firstID][4];
foo[value][4] = firstID;
*/
}
else
{
break;
}
}
}
categoriesLabel.Text = ShowCategory(firstElement, 0, foo, cPath, returnString);
for (int i = 0; i < cPath.Count; i++)
{
categoriesLabel.Text += "[" + i + "] " + cPath[i];
}
}
public string ShowCategory(int firstElement, int index, object[][] foo, ArrayList cPath, string returnString)
{
//string returnString;
string newcPath;
osAuction.Categories categories = new osAuction.Categories();
int level = Convert.ToInt32(foo[index][2]);
int parentID = Convert.ToInt32(foo[index][1]);
string categoriesName = Convert.ToString(foo[index][0]);
int nextID = Convert.ToInt32(foo[index][4]);
for (int i = 0; i < level; i++)
{
returnString += " ";
}
returnString += "";
string conFirstElement = Convert.ToString(firstElement);
for (int i = 0; i < cPath.Count; i++)
{
if (cPath[i] == conFirstElement)
{
returnString += "";
break;
}
}
returnString += categoriesName;
for (int i = 0; i < cPath.Count; i++)
{
if (cPath[i] == conFirstElement)
{
returnString += "";
break;
}
}
/*if (categories.HasSubCategories(firstElement))
{
returnString += "";
}*/
returnString += "";
returnString += "<br />";
if (nextID != 0)
{
index = index + 1;
ShowCategory(nextID, index, foo, cPath, returnString);
}
return returnString;
}
Kompileraren säger: "Object reference not set to an instance of an object" på rad 187:while (objReader2.Read())
Jag fattar nada. Någon som har ett hum vad det är som spökar?
Sv: Object reference not set to an instance of an object
Sv:Object reference not set to an instance of an object
Sv: Object reference not set to an instance of an object
if (objReader.HasRows)
{
newPath += cPath[j];
while (objReader2.Read())Sv:Object reference not set to an instance of an object