人。我正在撞墙,寻求救济!
我让用户在数据库中创建一个新项目。用户输入故障排除程序(任务列表)和程序的简短摘要(10个字左右)。该过程被添加到数据库中没有问题。除了将其添加到数据库之外,我的代码还需要通过电子邮件向管理员发送审核警报。
警报电子邮件需要包含创建它的人的姓名(没有问题)和摘要。摘要是我被挂断的地方。我试图从" ItemInserted"中的dataSource.InsertParameters中获取它。事件,它即将出现。
以下是DetailsView的相关部分:
<asp:DetailsView ID="DvProcedures" runat="server" Height="50px" Width="586px"
AutoGenerateRows="False" DataKeyNames="ProcID" DataSourceID="dataDvProcedures"
OnItemUpdated="DvProcedures_ItemUpdated" OnItemInserted="DvProcedures_ItemInserted" OnItemInserting="DvProcedures_ItemInserting">
<Fields>
<!-- other fields are removed from this example to help keep things tiday -->
<asp:TemplateField HeaderText="Summary" SortExpression="Summary">
<InsertItemTemplate>
<asp:TextBox ID="txtProcedureSummary" runat="server" Text='<%# Bind("Summary") %>' Width="586px" CausesValidation="True"></asp:TextBox>
</InsertItemTemplate>
</Fields>
</asp:DetailsView>
以下是DetailView数据源的相关部分:
<asp:SqlDataSource ID="dataDvProcedures" runat="server" ConnectionString="<%$ ConnectionStrings:kbelConnectionString %>"
InsertCommand="INSERT INTO [Procedures]
([PolicyID], [CategoryID], [Summary], [Content], [CreatedBy], [CreatedOn])
VALUES (@PolicyID, @CategoryID, @Summary, @Procedure, @CreatedBy, GETDATE())"
<!-- the remainder of the dataview is removed, so as not to muddy these waters -->
</asp:SqlDataSource>
这是&#34; ItemInserted&#34;的代码隐藏和&#34; ItemInserting&#34;事件:
protected void DvProcedures_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
{
// check for database exception and such. then re-bind the gridview
if (e.Exception != null)
{
lblErrorMessage.Text = "A database error has occurred. Message: " + e.Exception.Message;
e.ExceptionHandled = true;
}
else if (e.AffectedRows == 0)
lblErrorMessage.Text = "No error was detected, but procedure could not be added to the database.";
else
{
GvProcedures.DataBind();
}
// send email to admin
this.SendEmailMessage(
AdminEmail,
SmtpServer,
dataDvProcedures.InsertParameters["CreatedBy"].DefaultValue, // works fine, maybe related to manually setting it in DvProcedures_ItemInserting()?
dataDvProcedures.InsertParameters["Summary"].DefaultValue, // comes up null, although it's good in DB
"create");
}
protected void DvProcedures_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
// set "CreatedBy" attribute for dataview's "insert" command
// TODO: get username programitically
string userName = "dvader";
dataDvProcedures.InsertParameters["CreatedBy"].DefaultValue = userName;
}
我不知道如何继续,所以欢迎任何建议。
谢谢。
注意:我搜索了SO这个问题,发现了两种可能,这些都没有帮助我:
1)Getting Value from DataView C#
建议: string name = dataView [index] [&#34; Name&#34;] as string; 结果: 得到了&#34;名称&#39;索引&#39;在当前背景下不存在&#34;错误
2)How to get a value from a column in a DataView?
建议: 如果您有DataView而不是DataTable,那么它与DataRowView一样 string val =(string)view [rowIndex] [&#34; GrossPerPop&#34;];
结果: 我不知道如何制作或使用DataRowView