在Gridview中获取不正确的数据

时间:2018-04-03 16:54:44

标签: c# sql asp.net

我有这个应用程序,它假设在sql数据库中搜索发票号。我只使用文本框,gridview和按钮。当我使用查询构建器时,我的SQL查询为我提供了正确的信息,但是当我运行应用程序时,我在gridview中获得了400多条记录。我是初学者,我正在寻找建议并帮助我们获得正确的结果。 我的asp代码

<asp:TextBox ID="txtInvoice" runat="server"></asp:TextBox>

                <AlternatingRowStyle BackColor="White" ForeColor="#284775" />

                <Columns>
                    <asp:BoundField DataField="SiteID" HeaderText="Site ID" SortExpression="SiteID">
                        <ItemStyle HorizontalAlign="Center" width="100" />
                    </asp:BoundField>
                    <asp:BoundField DataField="SiteDescription" HeaderText="Site Description" SortExpression="SiteDescription">
                        <ItemStyle HorizontalAlign="Left" Wrap="False" width="100"/>
                    </asp:BoundField>
                    <asp:BoundField DataField="InvoiceDate" HeaderText="Invoice Date" SortExpression="InvoiceDate">
                        <ItemStyle HorizontalAlign="Center" Wrap="False" />
                    </asp:BoundField>
                    <asp:BoundField DataField="InvoiceNumber" HeaderText="Invoice Number" SortExpression="InvoiceNumber">
                        <HeaderStyle Wrap="True" />
                        <ItemStyle HorizontalAlign="Center" Width="75" />
                    </asp:BoundField>
                    <asp:BoundField DataField="VendorNumber" HeaderText="Vendor Number" SortExpression="VendorNumber">
                        <HeaderStyle Wrap="True" />
                        <ItemStyle HorizontalAlign="Center" Width="100" />
                    </asp:BoundField>
                    <asp:BoundField DataField="VendorName" HeaderText="Vendor Name" SortExpression="VendorName">
                        <ItemStyle HorizontalAlign="Left" />
                    </asp:BoundField>
                    <asp:BoundField DataField ="ReceivedDate" HeaderText="Received Date" SortExpression="ReceivedDate">
                        <ItemStyle HorizontalAlign ="Center" />
                     </asp:BoundField>

                </Columns>
                <EditRowStyle BackColor="#999999" />
                <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                <SortedAscendingCellStyle BackColor="#E9E7E2" />
                <SortedAscendingHeaderStyle BackColor="#506C8C" />
                <SortedDescendingCellStyle BackColor="#FFFDF8" />
                <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
            </asp:GridView>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:OneSourceConnectionString %>" SelectCommand="SELECT DISTINCT a.SiteID, a.SiteDescription, po.InvoiceDate, po.InvoiceNumber, v.VendorNumber, v.VendorName, po.ReceivedDate FROM AdmSites AS a INNER JOIN PreTranslations AS pt ON a.SiteID = pt.SiteID INNER JOIN InvAdjustments AS i ON a.AdmSiteID = i.AdmSiteID INNER JOIN PurSitePurchaseOrderHeader AS po ON a.AdmSiteID = po.AdmSiteID INNER JOIN InvVendors AS v ON po.InvVendorID = v.InvVendorID WHERE (po.InvoiceNumber LIKE @InvoiceNumber + N'%')">

                <SelectParameters>
                    <asp:Parameter Name="InvoiceNumber" Type="String" />
                </SelectParameters>
            </asp:SqlDataSource>

代码背后:

protected void btnSearch_Click(object sender, EventArgs e)
    {
        // Get connection to the database
        string strConn = ConfigurationManager.ConnectionStrings["OneSourceConnectionString"].ToString();
        SqlConnection conn = new SqlConnection(strConn);
        conn.Open();
        // Query OneSource Database to grab invoice number
        SqlCommand cmd = new SqlCommand("SELECT DISTINCT a.SiteID, a.SiteDescription, po.InvoiceDate, po.InvoiceNumber, v.VendorNumber, v.VendorName, po.ReceivedDate"
                                        + " FROM AdmSites a, InvVendors v, PurSitePurchaseOrderHeader po, InvAdjustments i, PreTranslations pt"
                                        + " WHERE a.SiteID = pt.SiteID"
                                        + " AND a.AdmSiteID = i.AdmSiteID"
                                        + " AND a.AdmSiteID = po.AdmSiteID"
                                        + " AND v.InvVendorID = po.InvVendorID"
                                        + " AND i.InvoiceNumber = @txtInvoice", conn);

          cmd.Parameters.AddWithValue("@txtInvoice", txtInvoice.Text);

        try
        {

            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();

            da.Fill(dt);

            if (dt.Rows.Count > 0)
            {

                gvSearch.DataSource = dt;
                gvSearch.DataBind();
            }
            conn.Close();


        }
        catch (Exception ex)
        {
            Response.Write(ex);
        }
        finally
        {
            conn.Close();
        }
    }

1 个答案:

答案 0 :(得分:0)

在你的代码中 你正在使用这两件事 1)SQL DATASOURCE &#34; SelectCommand =&#34; SELECT DISTINCT a.SiteID,a.SiteDescription,po.InvoiceDate,po.InvoiceNumber,v.VendorNumber,v.VendorName,po.ReceivedDate FROM AdmSites AS INNER JOIN PreTranslations AS pt on a.SiteID = pt .SiteID INNER JOIN InvAdjustments AS i ON a.AdmSiteID = i.AdmSiteID INNER JOIN PurSitePurchaseOrderHeader AS po ON a.AdmSiteID = po.AdmSiteID INNER JOIN InvVendors AS v ON po.InvVendorID = v.InvVendorID WHERE(po.InvoiceNumber LIKE @InvoiceNumber + N'#39;%&#39;)&#34;&GT;

2)的SqlCommand  SqlCommand cmd = new SqlCommand(&#34; SELECT DISTINCT a.SiteID,a.SiteDescription,po.InvoiceDate,po.InvoiceNumber,v.VendorNumber,v.VendorName,po.ReceivedDate&#34;                                         +&#34;来自AdmSites a,InvVendors v,PurSitePurchaseOrderHeader po,InvAdjustments i,PreTranslations pt&#34;                                         +&#34; WHERE a.SiteID = pt.SiteID&#34;                                         +&#34; AND a.AdmSiteID = i.AdmSiteID&#34;                                         +&#34; AND a.AdmSiteID = po.AdmSiteID&#34;                                         +&#34; AND v.InvVendorID = po.InvVendorID&#34;                                         +&#34;和i.InvoiceNumber = @ txtInvoice&#34;,conn);

不要同时使用这两件事 只使用一个