2013-06-26 74 views
2

這一直在折磨我過去兩天,我迫切希望有人能提供一些指導。我真的不知道自己出錯的地方,所以任何人都可以做的事情會非常棒。從GridView TemplateField更新SQL數據庫DropDownLists

SQLDataSource1是一個視圖。它從tbl_Job的字段中獲取PK並轉換爲名稱。也抓住鑰匙。 SQLOrigin是tbl_origin

SQL View tbl_Job tbl_origin

數據源我通過CommandField,使用戶可以編輯的行啓用編輯按鈕。

這些都是我SqlDataSources

<asp:SqlDataSource ID="SQLDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:HSEProjRegConnectionString1 %>" 
    SelectCommand="SELECT * FROM v_job_details WHERE [email protected]" UpdateCommand="UPDATE [tbl_Job] SET [Job_Origin_ID][email protected],[Job_Department_ID][email protected],[Job_Priority_ID][email protected],[Job_Status_ID][email protected] WHERE [Job_ID][email protected]_ID" 
    OnUpdated="SQLDataSource1_Updated"> 
    <SelectParameters> 
     <asp:QueryStringParameter Name="id" Type="Int32" QueryStringField="id" /> 
    </SelectParameters> 
    <UpdateParameters> 
     <asp:Parameter Name="originID" Type="Int32" /> 
     <asp:Parameter Name="departmentID" Type="Int32" /> 
     <asp:Parameter Name="priorityID" Type="Int32" /> 
     <asp:Parameter Name="statusID" Type="Int32" /> 
     <asp:Parameter Name="Job_ID" Type="Int32" /> 
    </UpdateParameters> 

<asp:SqlDataSource ID="SQLOrigin" runat="server" ConnectionString="<%$ ConnectionStrings:HSEProjRegConnectionString1 %>" 
    SelectCommand="SELECT * FROM [tbl_origin] WHERE Active = 1"></asp:SqlDataSource> 

我用TemplateFieldsItemTemplatesDropDownList顯示錶的內容。這是下面的代碼。請注意,我沒有添加所有的TemplateField來節省空間。除了現場參考,它們是相同的。

<asp:Label ID="Label1" runat="server" /> 
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="False" 
    DataSourceID="SQLDataSource1" AutoGenerateColumns="False" DataKeyNames="Job_ID"> 
    <Columns> 
     <asp:BoundField HeaderText="Job ID" DataField="Job_ID" InsertVisible="False" ReadOnly="True" /> 
     <asp:TemplateField HeaderText="Reference/Origin"> 
      <EditItemTemplate> 
       <asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="SQLOrigin" DataTextField="Name" 
        DataValueField="origin_ID" SelectedValue='<%# Bind("origin_ID") %>' Width="150px" 
        OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged"> 
       </asp:DropDownList> 
      </EditItemTemplate> 
      <ItemTemplate> 
       <asp:Label ID="Label1" runat="server" Text='<%# Bind("origin") %>'></asp:Label> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:CommandField ShowEditButton="true" /> 
    </Columns> 
</asp:GridView> 

這是我在我的代碼背後:

protected void Page_Load(object sender, EventArgs e) 
    { 

    } 

    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     DropDownList ddl = sender as DropDownList; 
     if (ddl == null) { return; } 
     GridViewRow gvr = ddl.NamingContainer as GridViewRow; 
     if (gvr == null) { return; } 

     SQLDataSource1.UpdateParameters["originID"].DefaultValue = GridView1.DataKeys[gvr.RowIndex]["Job_ID"].ToString(); 
    } 

    protected void SQLDataSource1_Updated(object sender, SqlDataSourceStatusEventArgs e) 
    { 
     if ((e.Exception == null) && e.AffectedRows.Equals(1)) 
     { 
      Label1.Text = "Data successfully updated!"; 

      GridView1.DataBind(); 
     } 
    } 

當我在GridViewEdit,我更新的所有字段,並選擇Update和得到這個錯誤:

The UPDATE statement conflicted with the FOREIGN KEY constraint "FK_tbl_Job_tbl_status".  The conflict occurred in database "HSE_proj", table "dbo.tbl_status", column 'Status_ID'. 
The statement has been terminated. 

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Data.SqlClient.SqlException: The UPDATE statement conflicted with the FOREIGN KEY constraint "FK_tbl_Job_tbl_status". The conflict occurred in database "HSE_proj", table "dbo.tbl_status", column 'Status_ID'. 
The statement has been terminated. 

如果我試圖只更新1字段,我會得到下面的錯誤。在下面的案例中,我試圖修改ORIGIN字段,並且DEPARTMENT字段緊挨着它。錯誤細節根據我要修改的列而改變,但錯誤本身是相同的。

Cannot insert the value NULL into column 'Job_Department_ID', table 'HSE_proj.dbo.tbl_Job'; column does not allow nulls. UPDATE fails. 
The statement has been terminated. 

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Data.SqlClient.SqlException: Cannot insert the value NULL into column 'Job_Department_ID', table 'HSE_proj.dbo.tbl_Job'; column does not allow nulls. UPDATE fails. 
The statement has been terminated. 

編輯當我加入AutoPostback="true"DropDownLists,我開始下面的錯誤。我只是不明白NULL值來自哪裏。我會插入一個斷點,看看我是否能看到錯誤。

Cannot insert the value NULL into column 'Job_Origin_ID', table 'HSE_proj.dbo.tbl_Job'; column does not allow nulls. UPDATE fails. 
The statement has been terminated. 

編輯2 修改了代碼到跟在後面。當我插入一個斷點並按照它來完成時,它正在拾取它應該的值,但它仍然給我一個NULL錯誤。

protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     DropDownList ddl = sender as DropDownList; 
     if (ddl == null) { return; } 
     GridViewRow gvr = ddl.NamingContainer as GridViewRow; 
     if (gvr == null) { return; } 

     DropDownList ddlOrigin = (DropDownList)sender; 
     GridViewRow row = (GridViewRow)ddlOrigin.NamingContainer; 

     Control control = row.FindControl("DropDownList2"); 

     if (ddlOrigin != null) 
     { 
      SQLDataSource1.UpdateParameters["originID"].DefaultValue = ddlOrigin.SelectedValue; 
      SQLDataSource1.UpdateParameters["updateID"].DefaultValue = GridView1.DataKeys[gvr.RowIndex]["Job_ID"].ToString(); 
     } 
    } 

編輯3 仍在試圖得到這個工作。考慮嘗試獲取Page_Load中的值,並在其中添加了一個using方法來添加包含值的參數,但我無法完成此工作。代碼沒有任何意義,所以我報廢了。我可以在網上找到的所有示例都指向使用OnSelectedIndexChangedAutoPostBack="true",當使用斷點時實際上是返回它應該的值,但由於某些原因,當我嘗試用這些值更新表時,它仍然表示爲NULL。

我可能沒有正確閱讀此內容。在Locals,如果我去到this導航到SQLDataSourceUpdateParametersNon-Public membersbase_collectionItems,我看到了我的參數在代碼中設置和originID具有tbl_origin正確的PK。我在調試過程中經驗不足,對我來說,我懷疑是否有問題,他們在Non-Public members這意味着即使代碼找到它們,UpdateCommand也可能無法訪問這些值?我希望這是有道理的。

+1

嘿,你的DropDownList沒有'AutoPostback = true'。如果你沒有這個'OnSelectedIndexChanged =「DropDownList2_SelectedIndexChanged」'不會觸發。 –

+0

謝謝,我做了修改! – Trido

回答

1
The UPDATE statement conflicted with the FOREIGN KEY constraint "FK_tbl_Job_tbl_status".  The conflict occurred in database "HSE_proj", table "dbo.tbl_status", column 'Status_ID'. 
The statement has been terminated 

這基本上意味着什麼,這個更新會導致有一個外國關鍵違規。您的數據庫有一些限制,對於表中您正在處理項目的每個項目,都必須存在於另一個特定的表中。在這種情況下StatusID

在你問題的第二部分:

Cannot insert the value NULL into column 'Job_Department_ID', table 'HSE_proj.dbo.tbl_Job'; column does not allow nulls. UPDATE fails. 
The statement has been terminated. 

我會承擔這個事做可空字段。你不能有某些字段爲空,所以它顯然會得到一些什麼都沒有的值。我會在他們的步驟中加入一個斷點,以便在更新時查看所調用的內容。

+0

我遇到的第一個問題是我的C#方法沒有返回它們應該的值。當我嘗試修改的行ID是28時,選定的值是'「1」'。我發現這種可怕的混淆。大聲笑 – Trido

+0

大聲笑!是的,你收到的SQL錯誤是告訴你這個故事。它沒有獲得完成SQL命令所需的所有數據。 – logixologist

+0

花了我一段時間,但我終於明白了。主要是。我稍微改變了我的做法,然後才能解決SQL問題。 – Trido