为什么我的编辑功能显示所有列条目?

时间:2014-06-18 19:31:49

标签: c# sql sql-server-2008

我有一个webapp,它调用SQL查询以获取受项目影响的应用程序列表。我希望我的管理员能够点击编辑并随意将删除应用程序添加到此列表中。目前,当他们单击编辑时,他们会看到受任何项目影响的每个应用程序都作为列表框的一部分,即使在Gridview中他们只看到受该特定项目影响的应用程序。当我对其工作的项目的值进行硬编码时,我需要将其作为变量,具体取决于我的管理员点击要编辑的项目。

select 
    Applications.ApplicationName, 
    Projects.ProjectName 
from ImpactedApplications 
inner join  Applications 
    on ImpactedApplications.AppId=Applications.AppId
inner join Projects 
    on ImpactedApplications.ProjectId=Projects.ProjectId
Where Projects.ProjectId=57 <--This is Hardcoded.  I need this to be dynamic.

添加代码

 <asp:TemplateField HeaderText="ImpactedApplications">
 <EditItemTemplate>
 <asp:ListBox ID="ListBox1" runat="server" DataSourceID="ImpactedApps" 
  DataTextField="ApplicationName" DataValueField="ProjectName" Width="250px"></asp:ListBox>
  &nbsp;<asp:Button ID="Button1" runat="server" Text="Add" />
 <asp:Button ID="Button2" runat="server" Text="Remove" />
 <asp:ListBox ID="ListBox3" runat="server" DataSourceID="Applications" 
  DataTextField="ApplicationName" DataValueField="AppId" Width="250px">
 </asp:ListBox>
 <br />
 <asp:SqlDataSource ID="ImpactedApps" runat="server" 


 ConnectionString="<%$ ConnectionStrings:LandscapeServicesConnectionString %>" 
 SelectCommand="select Applications.ApplicationName, Projects.ProjectName 
from ImpactedApplications 
inner join  Applications 
on ImpactedApplications.AppId=Applications.AppId
inner join Projects 
on ImpactedApplications.ProjectId=Projects.ProjectId
where Projects.ProjectName=@Projectname">
<SelectParameters>
<asp:FormParameter DefaultValue="PO Consolidation (EDW)" 
FormField="Gridview1 - Column[2] - Project Name" Name="Project Name" />
</SelectParameters>
</asp:SqlDataSource>

添加背景代码

public partial class Update : System.Web.UI.Page
{
    public string query { get; set; }
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["LandscapeServicesConnectionString"].ConnectionString);
    DataSet dt = new DataSet();
    protected void Page_Load(object sender, EventArgs e)
    {
        con.Open();

    }


    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;

    }

}

1 个答案:

答案 0 :(得分:0)

创建参数化查询:

select 
    Applications.ApplicationName, 
    Projects.ProjectName 
from ImpactedApplications 
inner join  Applications 
    on ImpactedApplications.AppId=Applications.AppId
inner join Projects 
    on ImpactedApplications.ProjectId=Projects.ProjectId
where Projects.ProjectId=@projectId

然后,当您创建SqlCommand时,添加一个参数并从表单中提供值:

cmd.Parameters.AddWithValue("@projectId",projectId);