2011-08-12 25 views
1

這是我的數據庫菜單出了錯

ID   parentitemid  text    Url 
    1   NULL   folder1   /folder1 
    2   folder1  WebForm1.aspx /folder1/WebForm1.aspx 
    3   folder1  WebForm2.aspx /folder1/WebForm2.aspx 
    6    null   folder3  /folder3 
    7   folder3  WebForm1.aspx /folder3/WebForm1.aspx 
    8   folder3  WebForm2.aspx /folder3/WebForm2.aspx 
    9   folder3  WebForm3.aspx /folder3/WebFomr3.aspx 

我試圖建立一個菜單出這個......

因此,它應該是這個樣子的folder1(WebFrom1.aspx, WebForm2.aspx)等等。但是,它又繼續完全錯誤和打印文件夾1(文件夾1),folder3(folder3),文件夾1(文件夾1)....

  • 文件夾1(文件夾1)說,文件夾1是在括號中的菜單和文件夾1的子菜單。

這是我在代碼隱藏邏輯

if (!IsPostBack) 
{ 
    DataSet dst = GetMenuData(); 
    foreach (DataRow masterRow in dst.Tables["Menu"].Rows) 
    { 
     if ((string)masterRow["parentitemid"] != "NULL" || 
      (string)masterRow["parentitemid"] != "") 
     { 
      MenuItem masterItem = new MenuItem((string)masterRow["parentitemid"]); 
      Menu1.Items.Add(masterItem); 
      foreach (DataRow childRow in masterRow.GetChildRows("Menu")) 
      { 
       MenuItem childItem = new MenuItem((string)childRow["text"]); 
       masterItem.ChildItems.Add(childItem); 
      } 
     } 
    } 
} 
    DataSet GetMenuData() 
    { 
     string connectionString = "Data Source=NISHANTH-PC\\SQLEXPRESS;Initial  
    Catalog=roletesting;Integrated Security=True"; 
     SqlConnection con = new SqlConnection(connectionString); 
     SqlDataAdapter parent = new SqlDataAdapter("Select DISTINCT parentitemid from 
    Menu", con); 
     SqlDataAdapter child = new SqlDataAdapter("SELECT * FROM Menu", con); 
     DataSet dst = new DataSet(); 
     parent.Fill(dst, "Menu"); 
     child.Fill(dst, "Menu"); 
     dst.Relations.Add("Children", 
      dst.Tables["Menu"].Columns["parentitemid"], 
     dst.Tables["Menu"].Columns["text"],false 
    ); 
     return dst; 
    } 

u能請幫助我在正確填寫菜單..

+0

如果你有數據庫的控制,我會建議,而不是文本列具有父列指向ID, 。這將允許您更改文件夾名稱,而無需更新所有parentitemid單元格。 – Tyrsius

+0

請不要將「C#」添加到您的標題中,以便將您的問題放入類別中。我們在[SO]上使用標籤。 –

+0

@John:當然...... – user838359

回答

0

它看起來像你的數據檢索是你的第一個問題。它看起來像你會檢索你的父數據,然後立即用子數據覆蓋菜單表。然後,這看起來是以一對一的方式將您的一張表與自己關聯起來。

試着改變你的填充不同的表名,並從那裏工作......

 parent.Fill(dst, "Menu"); 
     child.Fill(dst, "SubMenu"); 
+0

所以,我需要創建另一個表,並在菜單和子菜單 – user838359

+0

之間存在關係。父和子是獲取數據的兩個單獨命令,但都試圖將這些數據放入同一個數據集中的同一個表名中,導致它被覆蓋。 –

+0

k..Let me try that ...謝謝.. – user838359