在过去的两天里,我一直在折磨我,我非常希望有人可以提供一些指导。我真的不知道我哪里出错了,所以任何人都可以提供的帮助绝对是太棒了。
SQLDataSource1是一个View。它从tbl_Job的字段中获取PK并转换为名称。还要抓住钥匙。 SQLOrigin是tbl_origin
的数据源
我通过CommandField
启用了“编辑”按钮,以便用户可以编辑一行。
这些是我的SqlDataSources
:
<asp:SqlDataSource ID="SQLDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:HSEProjRegConnectionString1 %>"
SelectCommand="SELECT * FROM v_job_details WHERE Project_ID=@id" UpdateCommand="UPDATE [tbl_Job] SET [Job_Origin_ID]=@originID,[Job_Department_ID]=@departmentID,[Job_Priority_ID]=@priorityID,[Job_Status_ID]=@statusID WHERE [Job_ID]=@Job_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>
这就是我在Code Behind中所拥有的:
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();
}
}
当我点击Edit
中的GridView
时,我会更新所有字段并选择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
将Code Behind修改为以下内容。当我插入一个断点并完成它时,它会拾取它应该的值,但它仍然会给我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
可能无法访问值,即使代码确实存在找到他们?我希望这是有道理的。
答案 0 :(得分: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
这基本上意味着此更新会导致出现FOREIGN Key Violation。您的数据库存在一些限制,对于您正在处理项目的表中的每个项目,必须存在于另一个特定的表中。在这种情况下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.
我认为这与可空字段有关。你不能让某些字段为null,因此它显然得到一些什么都没有的值。我会在它们中添加一个断点并逐步执行它以查看更新时调用的内容。