Click here to Skip to main content
16,015,583 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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?

C#
 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();
    }
}
Posted
Updated 4-Sep-11 0:47am
v2

1 solution

If you're getting an error for the table indexed 0, the fillgrid 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).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900