按钮单击后不显示ASP.NET GridView

时间:2017-01-22 16:45:44

标签: c# sql asp.net .net gridview

我有一个名为ddlSvcEngr的下拉列表从数据源获取值。我希望gridview根据ddlSvcEngr的选择显示数据库的结果。但每次我点击查看按钮时都没有出现网格视图。这是代码:

HTML:

<asp:DropDownList ID="ddlSvcEngr" runat="server" DataSourceID="SqlDataSource1" DataTextField="rep_svcengr" DataValueField="rep_svcengr" CssClass="btn btn-default dropdown-toggle" Width="300px" AppendDataBoundItems="true">    
<asp:Button ID="btnView" runat="server" Text="View" CssClass="btn btn-primary" Width="300px" OnClick="btnView_Click" />
<asp:GridView ID="gvResults" runat="server" AutoGenerateColumns="False" ShowFooter="True">
            <Columns>
                <asp:BoundField ItemStyle-Width="150px" DataField="rep_svcengr" HeaderText="Service Engineer" >
                    <ItemStyle Width="150px"></ItemStyle>
            </Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:jasonsvcConnectionString %>" SelectCommand="SELECT [rep_svcengr], [rep_company], [rep_vessel], [rep_wo], [rep_brand], [rep_model], [rep_eqpt], [rep_sn] FROM [reports]"></asp:SqlDataSource>

代码背后:

private void bgSvcEngr()
    {
        string conString = ConfigurationManager.ConnectionStrings["jasonsvcConnectionString"].ConnectionString;
        string query = "SELECT rep_svcengr,rep_company,rep_vessel,rep_wo,rep_brand,rep_model,rep_eqpt,rep_sn FROM reports WHERE rep_svcengr='@svcengr'";
        SqlCommand cmd = new SqlCommand(query);
        cmd.Parameters.AddWithValue("@svcengr", ddlSvcEngr.SelectedItem.Value);
        using (SqlConnection con = new SqlConnection(conString))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.Connection = con;
                sda.SelectCommand = cmd;
                using (DataSet ds = new DataSet())
                {
                    sda.Fill(ds);
                    gvResults.DataSource = ds;
                    gvResults.DataBind();
                }
            }
        }
    }

protected void btnView_Click(object sender, EventArgs e)
    {            
            bgSvcEngr();
    }

注意:每次我从命令查询中删除行WHERE rep_svcengr='" + ddlSvcEngr.SelectedIndex.ToString() + "'。点击View按钮后会出现gridview。

我不知道为什么每次将WHERE命令放入查询时都不会出现gridview的问题。

1 个答案:

答案 0 :(得分:0)

摆脱单引号:

替换:

WHERE rep_svcengr='@svcengr'

使用:

WHERE rep_svcengr=@svcengr

enter image description here

public partial class WebForm1 : System.Web.UI.Page
{
    protected void Button1_Click(object sender, EventArgs e)
    {
        SelectByColor();
    }
    private void SelectByColor()
    {
        string conString = ConfigurationManager.ConnectionStrings["AdventureWorks2014ConnectionString"].ConnectionString;
        string query = "SELECT productid, name, color FROM production.product WHERE color=@pcolor";
        SqlCommand cmd = new SqlCommand(query);
        cmd.Parameters.AddWithValue("@pcolor", DropDownList1.SelectedItem.Value);
        using (SqlConnection con = new SqlConnection(conString))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.Connection = con;
                sda.SelectCommand = cmd;
                using (DataSet ds = new DataSet())
                {
                    sda.Fill(ds);
                    GridView1.DataSource = ds;
                    GridView1.DataBind();
                }
            }
        }
    }
}