本文介绍了在asp.net中使用数据库绑定菜单控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我有3个表,这些是产品表,catagory表和子目录表。使用这3个表我需要将菜单控件与sql server数据库绑定。在这个当我点击产品名称时,我必须显示catagoies和子目录,我必须在点击这些子目录时显示catagories和子目录的内容。



i我正在使用这个代码



使用System;

使用System.Data;

使用System.Configuration;

使用System.Collections;

使用System.Web;

使用System.Web.Security;

使用System .Web.UI;

使用System.Web.UI.WebControls;

使用System.Web.UI.WebControls.WebParts;

使用System.Web.UI.HtmlControls;

使用System.Data.SqlClient;

使用System.Data.SqlTypes;

使用System.IO ;



public partial class construction_materials:System.Web.UI.Page

{

string str = System .Configuration.ConfigurationManager.AppSettings [ConnectionString]。ToString();



protected void Page_Load(object s ender,EventArgs e)

{

if(!IsPostBack)

{



PopulateMenu();



}

}

DataSet GetMenuData()

$

SqlConnection con = new SqlConnection(str);

SqlDataAdapter dadCats = new SqlDataAdapter(SELECT catid,catname,productid FROM tbl_productcatagory where productid = 1,con );

SqlDataAdapter dadProducts = new SqlDataAdapter(SELECT subcatid,catid,subcatagoryname FROM tbl_productsubcatagory,con);

DataSet dst = new DataSet();

dadCats.Fill(dst,tbl_productcatagory);

dadProducts.Fill(dst,tbl_productsubcatagory);

dst.Relations.Add(Children ,

dst.Tables [tbl_productcatagory]。列[catid],

dst.Tables [tbl_productsubcatagory]。列[catid]) ;

返回dst;

}





public void PopulateMenu()

{

DataSet dst = GetMenuData();

foreach(dst.Tables中的DataRow masterRow [tbl_productcatagory]。行)

{

MenuItem masterItem = new MenuItem((string)masterRow [catname]);

Menu1.Items.Add(masterItem);

foreach( MasterRow中的DataRow childRow.GetChildRows(Children))

{

MenuItem childItem = new MenuItem((string)childRow [subcatagoryname],, ,(〜/ default.aspx?catid =+(masterRow [catid]。ToString())+& subcatid =+(childRow [subcatid]。ToString())),_ parent );

masterItem.ChildItems.Add(childItem);

}

}



}

}



但是我收到了这个错误。



Th因为并非所有值都具有相应的父值,所以无法启用约束。



你可以帮助我....

Hi i have 3 tables these are products table, catagory table, and sub catagory table. using these 3 tables i need to bind menu control with sql server database. in this when i click product name i have to display the catagoies and sub catagories and i have to display the content of catagories and sub catagories while clicking those sub catagories.

i am using this code

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.IO;

public partial class construction_materials : System.Web.UI.Page
{
string str = System.Configuration.ConfigurationManager.AppSettings["ConnectionString"].ToString();

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{

PopulateMenu();

}
}
DataSet GetMenuData()
{
SqlConnection con = new SqlConnection(str);
SqlDataAdapter dadCats = new SqlDataAdapter("SELECT catid, catname,productid FROM tbl_productcatagory where productid=1", con);
SqlDataAdapter dadProducts = new SqlDataAdapter("SELECT subcatid, catid, subcatagoryname FROM tbl_productsubcatagory ", con);
DataSet dst = new DataSet();
dadCats.Fill(dst, "tbl_productcatagory");
dadProducts.Fill(dst, "tbl_productsubcatagory");
dst.Relations.Add("Children",
dst.Tables["tbl_productcatagory"].Columns["catid"],
dst.Tables["tbl_productsubcatagory"].Columns["catid"]);
return dst;
}


public void PopulateMenu()
{
DataSet dst = GetMenuData();
foreach (DataRow masterRow in dst.Tables["tbl_productcatagory"].Rows)
{
MenuItem masterItem = new MenuItem((string)masterRow["catname"]);
Menu1.Items.Add(masterItem);
foreach (DataRow childRow in masterRow.GetChildRows("Children"))
{
MenuItem childItem = new MenuItem((string)childRow["subcatagoryname"], "", "", ("~/default.aspx?catid=" + (masterRow["catid"].ToString()) + "&subcatid=" + (childRow["subcatid"].ToString())), "_parent");
masterItem.ChildItems.Add(childItem);
}
}

}
}

But i am getting this error.

This constraint cannot be enabled as not all values have corresponding parent values.

can u please help me....

推荐答案

dst.Relations.Add("Children",
dst.Tables["tbl_productcatagory"].Columns["catid"],
dst.Tables["tbl_productsubcatagory"].Columns["catid"], false);





默认情况下,当你创建一个关系时,它会强制执行外键约束,通过设置为false,你告诉它你不要想强制执行这种关系。



By default, when you create a relationship, it enforces foreign key constraints, by setting to false, you are telling it that you dont want to enforce the relationship.


这篇关于在asp.net中使用数据库绑定菜单控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 22:09