2014-06-18 63 views
0

我有一個web應用程序調用SQL查詢來獲取受項目影響的應用程序列表。我希望我的管理員能夠點擊編輯並將刪除的應用程序隨意添加到此列表中。目前,當他們點擊編輯時,即使在Gridview中他們只能看到受該特定項目影響的應用程序,他們仍會將任何受ANY項目影響的應用程序視爲列表框的一部分。當我爲項目的硬編碼值工作時,但我需要它作爲一個變量,取決於我的管理員點擊哪個項目進行編輯。爲什麼我的編輯功能顯示所有列條目?

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 [email protected]"> 
<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; 

    } 

} 

回答

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 [email protected] 

那麼您什麼時候創建您的SqlCommand添加一個參數,並從您的窗體中給它的價值:

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

好吧,我想我遵循你的建議。兩個問題。假設你的意思是我的表單的列名稱的參數Im?第二我在哪裏添加cmd.Parameters.AddWithValue(「@ projectId」,projectId); – OnceorTwice

+0

它不必是相同的名稱 - 並且在創建'SqlCommand'後添加參數。如果您發佈用於執行查詢的代碼,我可以更具體一些。 –

+0

嗨,我將代碼添加到原始帖子。非常感謝。 – OnceorTwice

相關問題