gridview中显示的特定记录 [英] Specific record showing in gridview

查看:56
本文介绍了gridview中显示的特定记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



感谢先前的解决方案,它帮助了我。



现在我正在开展一个项目,我必须添加针对特定客户的产品,并希望在网格视图中显示那些特定客户的记录,而不是全部来自数据库



此产生的错误说明附近的语法不正确。



期待一个好的解决方案。



问候。

ABK



我尝试过:



Hi,
thanks for the Previous Solution it helped me.

now i am working on a project where i have to add Products for Specific Customers and want to show those specific customers record in grid-view not all from the database

this produce error says incorrect syntax near where.

looking forward for a good solution.

regards.
ABK

What I have tried:

<asp:GridView ID="grdProductType" runat="server" CssClass="table table-striped table-bordered table-condensed" AutoGenerateColumns="False" AllowPaging="True" PageSize="15" OnPageIndexChanging="grdProductType_PageIndexChanging" OnRowDataBound="grdProductType_RowDataBound">
                       <Columns>
                           <asp:TemplateField HeaderText="Sno">
                               <EditItemTemplate>
                                   <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                               </EditItemTemplate>
                               <ItemTemplate>
                                   <asp:Label ID="lblSno" runat="server"></asp:Label>
                               </ItemTemplate>
                           </asp:TemplateField>
                           <asp:BoundField HeaderText="Trader Company Name" DataField="CompanyName" />
                           <asp:BoundField HeaderText="Product Type" DataField="ProductType" />
                           <asp:BoundField HeaderText="Product " DataField="ProductName" />
                       </Columns>
                       <FooterStyle BorderStyle="None" Wrap="False" />
                       <HeaderStyle BackColor="#4989C1" BorderColor="#4989C1" BorderStyle="Solid" BorderWidth="0.75pt" ForeColor="White" HorizontalAlign="Center" VerticalAlign="Middle" />
                       <PagerStyle BorderStyle="Solid" HorizontalAlign="Center" VerticalAlign="Middle" />
                   </asp:GridView>





和后端编码为





Page_Load







and Back end coding is as


Page_Load


protected void Page_Load(object sender, EventArgs e)
       {
           if (Session["TraderID"] != null)
               GetCompnayInfo(hdID.Value = Session["TraderID"].ToString());

              Fillgrid();
       }







和FillGrid功能为






and FillGrid Function as

protected void Fillgrid()
      {
          DataTable dt = new DataTable();
          dt = obj.GetData(@"SELECT t.TraderId,t.CompanyName,zpt.ProductTypeID,zpt.ProductType,zp.ProductID,zp.ProductName,zp.ProductCode
          FROM TraderCompanyProfile t
          INNER JOIN TraderCompnayProducts tcs ON t.TraderId=tcs.TradeID
          INNER JOIN zProductType zpt ON tcs.ProductTypeID=zpt.ProductTypeID
          INNER JOIN zProduct zp ON tcs.ProductID=zp.ProductID ORDER BY t.CompanyName,zpt.ProductType,zp.ProductName where t.TraderId='" + Session["TraderID"].Tostring() + "'");
          grdProductType.DataSource = dt;
          grdProductType.DataBind();

      }

推荐答案

ORDER BY子句应该在WHERE子句之后。

您还应该使用参数化查询 [ ^ ]而不是在创建SQL查询时连接的字符串。这不仅有助于避免SQL注入攻击,还可以通过避免单引号等问题来帮助构建查询。

例如。 (注意 - 这尚未经过测试,我没有修复参数问题)
The ORDER BY clause should come after the WHERE clause.
You should also use Parameterized Queries[^] instead of concatenated strings when creating SQL queries. Not only does this help to avoid SQL Injection attacks it can help with the construction of the query by avoiding issues with single-quotes etc.
E.g. (Note - this has not been tested and I have not fixed the parameter issue)
dt = obj.GetData(@"SELECT t.TraderId,t.CompanyName,zpt.ProductTypeID,zpt.ProductType,zp.ProductID,zp.ProductName,zp.ProductCode
          FROM TraderCompanyProfile t
          INNER JOIN TraderCompnayProducts tcs ON t.TraderId=tcs.TradeID
          INNER JOIN zProductType zpt ON tcs.ProductTypeID=zpt.ProductTypeID
          INNER JOIN zProduct zp ON tcs.ProductID=zp.ProductID where t.TraderId=@sesh Session["TraderID"].Tostring() + "' ORDER BY t.CompanyName,zpt.ProductType,zp.ProductName ");


这篇关于gridview中显示的特定记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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