在过去的两天里,这一直在折磨我,我非常希望有人可以提供一些指导。我真的不知道我哪里出错了,所以任何人都可以做的任何事情来提供帮助都非常棒。
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 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
andItemTemplates
来显示一个表格的内容DropDownList
。这是下面的代码。请注意,我没有添加所有 TemplateFields 以节省空间。除了字段引用之外,它们是相同的。
<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();
}
}
当我点击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
将后面的代码修改为以下内容。当我插入一个断点并执行它时,它会获取它应该得到的值,但它仍然给我一个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
具有正确的 PK tbl_origin
。我在调试方面没有过多的经验,对我来说,我想知道问题是否可能是它们的Non-Public members
意思是UpdateCommand
即使代码确实找到了它们也可能无法访问这些值?我希望这是有道理的。