這一直在折磨我過去兩天,我迫切希望有人能提供一些指導。我真的不知道自己出錯的地方,所以任何人都可以做的事情會非常棒。從GridView TemplateField更新SQL數據庫DropDownLists
SQLDataSource1是一個視圖。它從tbl_Job的字段中獲取PK並轉換爲名稱。也抓住鑰匙。 SQLOrigin是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>
我用TemplateFields
和ItemTemplates
在DropDownList
顯示錶的內容。這是下面的代碼。請注意,我沒有添加所有的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();
}
}
當我在GridView
打Edit
,我更新的所有字段,並選擇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
方法來添加包含值的參數,但我無法完成此工作。代碼沒有任何意義,所以我報廢了。我可以在網上找到的所有示例都指向使用OnSelectedIndexChanged
和AutoPostBack="true"
,當使用斷點時實際上是返回它應該的值,但由於某些原因,當我嘗試用這些值更新表時,它仍然表示爲NULL。
我可能沒有正確閱讀此內容。在Locals
,如果我去到this
導航到SQLDataSource
,UpdateParameters
,Non-Public members
,base
,_collectionItems
,我看到了我的參數在代碼中設置和originID
具有tbl_origin
正確的PK。我在調試過程中經驗不足,對我來說,我懷疑是否有問題,他們在Non-Public members
這意味着即使代碼找到它們,UpdateCommand
也可能無法訪問這些值?我希望這是有道理的。
嘿,你的DropDownList沒有'AutoPostback = true'。如果你沒有這個'OnSelectedIndexChanged =「DropDownList2_SelectedIndexChanged」'不會觸發。 –
謝謝,我做了修改! – Trido