在SQL查询中获取错误.用于基于价格过滤数据 [英] getting error in sql query. for filtering data on the bases of price

查看:94
本文介绍了在SQL查询中获取错误.用于基于价格过滤数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据价格对产品进行过滤...

我在一页上有一个链接控件..并且我将页面重定向到ProductCatalog.aspx,以在网格中显示产品

i am trying to filter the products on the base of price...

i have a link control on one page.. and i am redirctring the page to ProductCatalog.aspx to show the products in the grid

<td align="center">
                               <asp:LinkButton ID="LinkButton4" PostBackUrl='<%# "~/ProductCatalog.aspx?PriceID="+Eval("UnitCost") %>' runat="server"><5000</asp:LinkButton>
                           </td>



在Products.Aspx页面上



on Products.Aspx page

string query = "Select * from Products where '"+int.TryParse(Request.QueryString["PriceID"]<5000)+"'";



我收到错误消息



i am getting the error

Error 1	Operator '<' cannot be applied to operands of type 'string' and 'int'	




已改进
在产品Catalog.aspx页上




IMproved
On Product Catalog.aspx page

private void LoadGridView() // just calling it on page load
   {

       string query = "Select * from Products where UnitCost= " + Request.QueryString["ID"] + "<5000"; ;
       //DataSet ds = new DataSet();
       DataSet ds=obj.fillgrid(query);
       GridView1.DataSource = ds.Tables[0];
       GridView1.DataBind(); ;
       //conn.Close();
   }



并在我的母版页上嵌入



and on my master page i embedd

<table class="style1" width="20%">
            <tr>
                <td bgcolor="#666" align="center" class="style2" >
                    Search Products By Price Range</td>
                        </tr>
                        <tr bgcolor="#F7F7DE" >
                            <td align="center">
                                <asp:LinkButton ID="LinkButton4" PostBackUrl='<%# "~/ProductCatalog.aspx?ID="+Eval("UnitCost") %>' runat="server"><5000</asp:LinkButton>
                            </td>
                        </tr>
                        <tr bgcolor="#F7F7DE">
                            <td align="center">
                                <asp:LinkButton ID="LinkButton5" runat="server">>=5000 and <=10000</asp:LinkButton>
                            </td>
                        </tr>
                        <tr bgcolor="#F7F7DE">
                            <td align="center">
                                <asp:LinkButton ID="LinkButton6" runat="server">>=10000 and <=20000</asp:LinkButton>
                            </td>
                        </tr >
                        <tr bgcolor="#F7F7DE">
                            <td align="center">
                                <asp:LinkButton ID="LinkButton7" runat="server">>20000</asp:LinkButton>
                            </td>
                        </tr>
                    </table>

推荐答案

string query = "Select * from Products where " + Request.QueryString["PriceID"].ToString() + "<5000";


使用参数化查询
或者,您可以在DataSetDataTable
中使用filter
编辑



其实Eval("UnitCost")的值是多少?因为查询中缺少字段名称(上述代码).您的代码应如下所示.


Use parametrized query
Or you can use filter in your DataSet or DataTable

EDIT



Actually whats the value of Eval("UnitCost")? Because the field name missing in your query(above code). You code should like below.

string query = "Select * from Products where PriceID<5000";

的产品中选择*,此处必须动态发送值5000.如果有任何问题请纠正我

EDIT2



因此,您想按范围选择产品吗?
您的代码应类似于(条件ProductIdUnitCost的位置应有两列)

Here you must send the value 5000 dynamically. Correct me if anything wrong

EDIT2



So you want to select products by range?
Your code should be like(There should be two columns in where condition ProductId & UnitCost)

string query = "Select * from Products where ProductID= '" + Request.QueryString["ID"].ToString() + "' AND UnitCost < 5000";


顺便说一句,我建议您使用参数化查询.


BTW again I suggest you to use parametrized query.


这篇关于在SQL查询中获取错误.用于基于价格过滤数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆