folks. I am currently banging my head against a wall, and seeking relief!
I have the user creating a new item in the database. User enters a trouble-shooting procedure (a list of tasks) and a very brief summary of the procedure (10 words or so). The procedure is added to the database with no problem. In addition to adding it to the database, my code needs to email admin a moderation alert.
The alert email needs to contain the name of the person who created it (that's no problem) and the summary. The summary is where I'm getting hung up. I'm attempting to grab it from dataSource.InsertParameters inside the "ItemInserted" event, and it's coming up null.
Here is the relevant portion of the 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>
Here is the relevant portion of the DetailView's data source:
<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>
And here is the code-behind for the "ItemInserted" and "ItemInserting" events:
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;
}
I have no idea how to proceed, so any advice would be welcome.
Thank you.
Note: I searched SO for this issue, and found two possibles, neither of which helped me:
1) Getting Value from DataView C#
recommendation: string name = dataView[index]["Name"] as string; result: got "the name 'index' does not exist in current context" error
2) How to get a value from a column in a DataView?
recommendation: If you have a DataView rather than a DataTable, then the same works with a DataRowView string val = (string)view[rowIndex]["GrossPerPop"];
result: I don't know how to make or use a DataRowView