Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 与类别C ASP.NET关联的筛选产品_C#_Asp.net_Datalist - Fatal编程技术网

C# 与类别C ASP.NET关联的筛选产品

C# 与类别C ASP.NET关联的筛选产品,c#,asp.net,datalist,C#,Asp.net,Datalist,如果类别链接是导航,如何按与之关联的类别搜索产品 我已经创建了一个Category表=Category\u id,product\u Category 我也在products表中添加了product_类别。 我可以通过管理面板添加类别,如果你看我附带的屏幕截图,它会完美地显示出来。 但当我点击某些类别时,它并没有显示与该类别相关的产品,而是显示所有产品 例如,如果单击“启动器”,则仅启动器产品将显示,而不是所有产品 我使用datalist来显示产品。我已经试着从两个晚上中整理出来,做了很多研究,

如果类别链接是导航,如何按与之关联的类别搜索产品

我已经创建了一个Category表=Category\u id,product\u Category 我也在products表中添加了product_类别。 我可以通过管理面板添加类别,如果你看我附带的屏幕截图,它会完美地显示出来。 但当我点击某些类别时,它并没有显示与该类别相关的产品,而是显示所有产品

例如,如果单击“启动器”,则仅启动器产品将显示,而不是所有产品

我使用datalist来显示产品。我已经试着从两个晚上中整理出来,做了很多研究,但找不到我需要的具体内容

你能帮我做这个吗

我正在我的产品页面中使用此编码,但它不起作用

    con.Open();
        SqlCommand cmd = con.CreateCommand();
        cmd.CommandType = CommandType.Text;
         if (Request.QueryString["category"] ==null)

        {

            cmd.CommandText = "select * from Products";

        }
        else
        {
            cmd.CommandText = "select * from Products where product_category='" + Request.QueryString["category"].ToString() + "'";
        }

        cmd.CommandText = "select * from Products";
        cmd.ExecuteNonQuery();
        DataTable dtt = new DataTable();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(dtt);

        DataList1.DataBind();

        con.Close();
您有3个问题:

您没有使用SqlParameters。 在else块之后重置sql查询。这将返回所有未过滤的项目-每次。 您不会将数据列表绑定到数据表。也许您将数据源设置在其他地方,但我们看不到它。我只是把它列出来作为衡量标准 从产品中删除cmd.CommandText=select*;在你的else街区之后。 并设置数据源:

if (Request.QueryString["category"] ==null)
{
     cmd.CommandText = "select * from Products";
}
else
{
     //use parameters to prevent SQL injection
     cmd.CommandText = "select * from Products where product_category=@category";
     cmd.Parameters.Add(new SqlParameter("category", Request.QueryString["category"]));
}
//cmd.CommandText = "select * from Products";<--- delete this line
/*.. exeute quesry and load into datatable */
//bind datalist:
DataList1.DataSource = dtt;
DataList1.DataBind();
您有3个问题:

您没有使用SqlParameters。 在else块之后重置sql查询。这将返回所有未过滤的项目-每次。 您不会将数据列表绑定到数据表。也许您将数据源设置在其他地方,但我们看不到它。我只是把它列出来作为衡量标准 从产品中删除cmd.CommandText=select*;在你的else街区之后。 并设置数据源:

if (Request.QueryString["category"] ==null)
{
     cmd.CommandText = "select * from Products";
}
else
{
     //use parameters to prevent SQL injection
     cmd.CommandText = "select * from Products where product_category=@category";
     cmd.Parameters.Add(new SqlParameter("category", Request.QueryString["category"]));
}
//cmd.CommandText = "select * from Products";<--- delete this line
/*.. exeute quesry and load into datatable */
//bind datalist:
DataList1.DataSource = dtt;
DataList1.DataBind();

在if语句之后,您仍然将命令文本设置为选择所有产品,因此请注释cmd.CommandText=select*from products;在if语句之后,并在if语句之后重试,您仍然将命令文本设置为选择所有产品,因此请注释cmd.CommandText=select*from products;在if语句之后重试

我已经自己解决了这个问题。这是我的代码,对我来说很有用

           //This is for displaying PRODUCTS.
            #region
            con.Open();
            SqlCommand cmdp = con.CreateCommand();
            cmdp.CommandType = CommandType.Text;

            var categoryID = Request.QueryString["category"];
            int catId = string.IsNullOrEmpty(categoryID) ? 0 : int.Parse(categoryID);
            if (!string.IsNullOrEmpty(categoryID))
                cmdp.CommandText = " select * from products where [category_id] = " + catId;
            else
                cmdp.CommandText = "select * from products";
            cmdp.ExecuteNonQuery();
            DataTable dttp = new DataTable();
            SqlDataAdapter dap = new SqlDataAdapter(cmdp);
            dap.Fill(dttp);
            Datalist1.DataSource = dttp;
            Datalist1.DataBind();

            con.Close();
            #endregion

我自己解决了这个问题。这是我的代码,对我来说很有用

           //This is for displaying PRODUCTS.
            #region
            con.Open();
            SqlCommand cmdp = con.CreateCommand();
            cmdp.CommandType = CommandType.Text;

            var categoryID = Request.QueryString["category"];
            int catId = string.IsNullOrEmpty(categoryID) ? 0 : int.Parse(categoryID);
            if (!string.IsNullOrEmpty(categoryID))
                cmdp.CommandText = " select * from products where [category_id] = " + catId;
            else
                cmdp.CommandText = "select * from products";
            cmdp.ExecuteNonQuery();
            DataTable dttp = new DataTable();
            SqlDataAdapter dap = new SqlDataAdapter(cmdp);
            dap.Fill(dttp);
            Datalist1.DataSource = dttp;
            Datalist1.DataBind();

            con.Close();
            #endregion

请把答案拿出来阅读,然后回来编辑你的问题。这是目前无法解决的问题。我也附上了图片,我只是想做的是搜索产品,如果我单击其中一个类别,请拍摄并阅读,然后回来编辑您的问题。这是目前无法解决的问题。我也附上了图片,我只是想做的是搜索产品,如果我点击其中一个类别,你能澄清吗?cmd.CommandText=从产品中选择*;这就是我doing@NaveenZehra看看我的答案。你看,哪一行需要删除。你能澄清一下吗?cmd.CommandText=从产品中选择*;这就是我doing@NaveenZehra看看我的答案。你看,哪一行需要删除。不太清楚。对不起,我没有收到这封信。我在源代码课程中已经有了datasource,它给了我一个错误删除了一个定义我必须加载到datatable中的什么查询?我不理解这个问题。代码中只有一个查询。执行这个。将结果加载到数据表中。将数据表绑定到您的数据列表。不太清楚,我没有收到这个消息。对不起。我在源代码课程中已经有了datasource,它给了我一个错误删除了一个定义我必须加载到datatable中的什么查询?我不理解这个问题。代码中只有一个查询。执行这个。将结果加载到数据表中。将数据表绑定到数据列表。