在下拉列表的基础上填充girdview [英] filling girdview on the bases of dropdownlist
本文介绍了在下拉列表的基础上填充girdview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个下拉列表,用于从数据库的类别表中获取数据.
ESK_Categories表
CateogoryID,类别名称.
ESK_产品表
ProductID,CategoryID(fk),ProductName,UnitCost
现在,我想在网格视图中显示产品表中的产品.我写了代码,但给出错误,找不到表0.
请检查休假代码中绑定功能的查询..是否正确?
i have a dropdownlist getting data from the categories table in the database.
ESK_Categories Table
CateogoryID,Category name.
ESK_Products Table
ProductID,CategoryID(fk),ProductName,UnitCost
now i want to display products form the products table in the grid view. i wrote the code but its giving an error can not find table 0.
pls check the query in bind fuction in the fallowing code.. is it correct?
dbcon obj = new dbcon();
protected void Page_Load(object sender, EventArgs e)
{
//dbcon obj = new dbcon();
if (!IsPostBack)
{
string querydr = "select * from ESK_Categories";
SqlDataReader dr = obj.fillcomb(querydr);
DropDownList1.DataSource = dr;
DropDownList1.DataValueField = "CategoryName";
DropDownList1.DataBind();
}
if (!IsPostBack)
{
bind();
}
}
public void bind()
{
string query = "SELECT ESK_Products.ProductID, ESKProducts.ProductName,ESK_Products.UnitCost ,ESK_Cateogries.CategoryName FROM ESK_Products LEFT JOIN ESK_Categories ON ESK_Products.ProductsID=ESK_Categories.CategoryID Where CategoryName='" + DropDownList1.Text + "'";
DataSet ds = obj.fillgrid(query);
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}
}
推荐答案
如果索引为0的表出现错误,则fillgrid
方法不太可能创建/填充该表到您的数据集中.使用调试器查看方法内部发生的情况.
关于查询,如果您要获取所有产品,并且某个产品具有类别,则也要获取类别名称,那么看起来就很正确.
但是,请尝试使用 SqlParameter [
If you''re getting an error for the table indexed 0, thefillgrid
method isn''t most likely creating/ filling the table into your dataset. Use the debugger to see what''s happening inside the method.
About the query, if you''re trying to fetch all products and if a product has a category then fetch the categoryname also, then it looks quite right.
However, try using SqlParameter[^] (if this is Sql Server) instead of concatenating literals into the statement (DropDownList1.Text
).
这篇关于在下拉列表的基础上填充girdview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文