0

在基于 Web 的 ASP.NET 建议框程序中,管理员将能够使用所有者的用户名查看 GridView 控件中列出的所有建议。在 GridView 的最后一列中,将在此处列出状态。当管理员单击其中一个建议的状态时,将出现一个新的弹出窗口 (asp.net ajax ModalPopUpExtender),其中列出所有可能的状态,例如:已执行、已批准...等。当管理员选择其中一种状态,建议的状态将在数据库中更新。一切正常。我现在要做的是,当用户更新任何建议的状态时,将向所有者发送一封电子邮件通知,告知其建议状态的更新。

我已经编写了 Mail 函数,但我不知道为什么它不发送任何电子邮件,并且在调试代码时出现此错误:

使用未分配的局部变量“描述”

我已经将它分配给数据库中 [Description] 列的值,但我不知道为什么会出现此错误。

谁能帮我解决这个问题?

我真的很难获得更新建议的用户名。仅供参考,我有以下数据库设计:

Employee Table: Username, Name... 
SafetySuggestionsLog: ID, Title, Description, Username, StatusID 
SafetySuggestionsStatus: ID, Status

ASP.NET 代码:

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
                        AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="ID" 
                        width="900px" CssClass="mGrid" 
                        DataSourceID="SqlDataSource1" 
                        OnRowDataBound="GridView1_RowDataBound">
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" CssClass="alt" />
            <HeaderStyle Font-Bold = "True" ForeColor="Black" Height="20px"/> 
            <Columns>
                <asp:BoundField DataField="ID" HeaderText="No." InsertVisible="False" 
                    ReadOnly="True" SortExpression="ID" />
                <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
                <asp:BoundField DataField="Description" HeaderText="Description" 
                    SortExpression="Description" />
                <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                <asp:BoundField DataField="Username" HeaderText="Username" 
                    SortExpression="Username" />
                <asp:BoundField DataField="DivisionShortcut" HeaderText="Division" 
                    SortExpression="DivisionShortcut" />
                <asp:BoundField DataField="Type" HeaderText="Type" SortExpression="Type" />

                <%-- This to make status be opened and edited through the Ajax ModalPopUp Window --%>
                <asp:TemplateField HeaderText="Status">
                    <ItemTemplate>
                        <asp:LinkButton runat="server" ID="lnkSuggestionStatus" Text='<%#Eval("Status")%>'
                                        OnClick="lnkSuggestionStatus_Click">
                        </asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>

                <%--<asp:HyperLinkField HeaderText="Status" 
                    SortExpression="Status" />--%>
            </Columns>
            <RowStyle HorizontalAlign="Center" />
        </asp:GridView>

        <asp:Button runat="server" ID="btnModalPopUp" style="display:none" />

        <AjaxToolkit:ModalPopUpExtender ID="modalPopUpExtender1"
                                        runat="server" 
                                        TargetControlID="btnModalPopUp" 
                                        PopupControlID="pnlPopUp" 
                                        BackgroundCssClass="popUpStyle"
                                        PopupDragHandleControlID="panelDragHandle" 
                                        OkControlID="OKButton">
        </AjaxToolkit:ModalPopUpExtender>

        <asp:HiddenField ID="HiddenField1" runat="server"/>

        <asp:Panel runat="server" ID="pnlPopUp" CssClass="popUpStyle">

                    <asp:RadioButtonList ID="StatusList" runat="server" RepeatColumns="1" RepeatDirection="Vertical"
                                            RepeatLayout="Table" TextAlign="Right" DataSourceID="SuggestionStatusDataSource"
                                            DataTextField="Status" DataValueField="ID">
                        <asp:ListItem id="option1" runat="server" Value="ACTIONED" />
                        <asp:ListItem id="option2" runat="server" Value="APPROVED" />
                        <asp:ListItem id="option3" runat="server" Value="PENDING" />
                        <asp:ListItem id="option4" runat="server" Value="TRANSFERRED" />
                    </asp:RadioButtonList>
                    <asp:SqlDataSource ID="SuggestionStatusDataSource" runat="server"
                                        ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
                                        SelectCommand="SELECT * FROM [SafetySuggestionsStatus]"></asp:SqlDataSource>

                    <asp:Button ID="confirmButton" runat="server" Text="Confirm" 
                                OnClientClick="javascript:return confirm('Are you sure you want to send an email notification about the safety suggestion to the owner?')" 
                                OnClick="btnSendStatus_Click" />

            <asp:Button ID="OKButton" runat="server" Text="Close" />
        </asp:Panel>
        </ContentTemplate>
        </asp:UpdatePanel>

代码隐藏:

protected void lnkSuggestionStatus_Click(object sender, EventArgs e)
    {
        LinkButton lnkSuggestionStatus = sender as LinkButton;

        //var safetySuggestionsId = 

        //get reference to the row selected 
        GridViewRow gvrow = (GridViewRow)lnkSuggestionStatus.NamingContainer;

        //set the selected index to the selected row so that the selected row will be highlighted
        GridView1.SelectedIndex = gvrow.RowIndex;

        //This HiddenField used to store the value of the ID
        HiddenField1.Value = GridView1.DataKeys[gvrow.RowIndex].Value.ToString();
        //ViewState["Username"] = gvrow.Cells[4].Text;

        //show the modalPopUp
        modalPopUpExtender1.Show();
    }

    public void btnSendStatus_Click(object sender, EventArgs e) {
        //get the ID of the selected suggestion/row
        var statusID = StatusList.SelectedValue;
        var safetySuggestionsID = HiddenField1.Value;

        string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=psspdbTest;Integrated Security=True";
        //For updating the status of the safety suggestion
        string updateCommand = "UPDATE SafetySuggestionsLog SET StatusID= @statusID where ID=@SafetySuggestionsID";
        using (SqlConnection conn = new SqlConnection(connString))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand(updateCommand, conn))
            {
                cmd.Parameters.AddWithValue("@statusID", Convert.ToInt32(statusID));
                cmd.Parameters.AddWithValue("@SafetySuggestionsID", Convert.ToInt32(HiddenField1.Value));
                cmd.ExecuteNonQuery();
            }
            //reset the value of hiddenfield
            HiddenField1.Value = "-1";
        }

        GridView1.DataBind();


        SendSuggestionStatusToUser(safetySuggestionsID);
    }

    protected void SendStatusByEmail(string toAddresses, string fromAddress, string MailSubject, string MessageBody, bool isBodyHtml)
    {
        SmtpClient sc = new SmtpClient("MAIL.Aramco.com");
        try
        {
            MailMessage msg = new MailMessage();
            msg.From = new MailAddress("pssp@aramco.com", "PMOD Safety Services Portal (PSSP)");

            // In case the mail system doesn't like no to recipients. This could be removed
            //msg.To.Add("pssp@aramco.com");

            msg.Bcc.Add(toAddresses);
            msg.Subject = MailSubject;
            msg.Body = MessageBody;
            msg.IsBodyHtml = isBodyHtml;
            sc.Send(msg);
        }
        catch (Exception ex)
        {
            throw ex;
        }

    }

    protected void SendSuggestionStatusToUser(string suggestionID)
    {
        string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=psspdbTest;Integrated Security=True";

        string safetySuggestionID = suggestionID.ToString();
        //string username = ViewState["Username"].ToString();

        //The following connection is to get the username of the suggestion Owner
        //and append (@aramco.com) to it.
        using (SqlConnection conn = new SqlConnection(connString))
        {
            var sbEmailAddresses = new System.Text.StringBuilder(2000);

            //initiate the varibles that will be retreived from the database
            string username;
            string description;
            string status;

            // Open DB connection.
            conn.Open();

            string cmdText2 = @"SELECT Username, Description, Status FROM dbo.SafetySuggestionsLog SSL INNER JOIN SafetySuggestionsStatus SSS
                                ON (SSL.StatusID = SSS.ID) WHERE (SSL.ID = @safetySuggestionID)";
            using (SqlCommand cmd = new SqlCommand(cmdText2, conn))
            {
                cmd.Parameters.AddWithValue("@SafetySuggestionID", Convert.ToInt32(HiddenField1.Value));
                SqlDataReader reader = cmd.ExecuteReader();
                if (reader != null)
                {
                    if (reader.Read())
                    {
                        username = reader["Username"].ToString();
                        description = reader["Description"].ToString();
                        status = reader["Status"].ToString();
                        sbEmailAddresses.Append(username).Append("aramco.com");
                    }
                }

                var sEMailAddresses = sbEmailAddresses.ToString();
                string body = @"Good day, &lt;br /&gt;&lt;br /&gt;
                                <b> We just would like to notify you that your following safety suggestion: </b>"
                                    + description +
                                    @"&lt;br /&gt;&lt;br /&gt;
                            has been.
                            &lt;br /&gt; &lt;br /&gt;&lt;br /&gt; &lt;br /&gt;
                            This email was generated using the &lt;a href='http://pmv/pssp/Default.aspx'&gt;PMOD Safety Services Portal (PSSP) &lt;/a&gt;. 
                            Please do not reply to this email.
                            ";
                SendStatusByEmail(sbEmailAddresses.ToString(), "", "Notification of Your Safety Suggestion", body, true);
                sbEmailAddresses.Clear();
                reader.Close();


            }

            conn.Close();
        }
    }

注意:我知道我不应该在这里发布冗长的代码,而是因为我想向您解释我的工作并获得您的帮助。

更新:

我修改了关于将变量分配给 NULL 的代码,当我调试代码时,我发现阅读器不起作用并且它没有读取以下内容:

if (reader != null)
                {
                    if (reader.Read())
                    {
                        username = reader["Username"].ToString();
                        description = reader["Description"].ToString();
                        status = reader["Status"].ToString();
                        sbEmailAddresses.Append(username).Append("@aramco.com");
                    }
                }

更新 2:

在该SendSuggestionStatusToUser(string suggestionID)方法中,我在以下行中添加了断点:

string safetySuggestionID = suggestionID.ToString();

我在以下行中添加了断点:

cmd.Parameters.AddWithValue("@safetySuggestionID", Convert.ToInt32(HiddenField1.Value));

我发现对于第一个,safetySuggestionID 从传递的值中获取数据。但是,对于第二个,值为(HiddenField1.Value)-1,我不知道为什么。此外,我为以下每一行添加了断点:

SqlDataReader reader = cmd.ExecuteReader();
                if (reader != null)
                {
                    if (reader.Read())
                    {
                        username = reader["Username"].ToString();
                        description = reader["Description"].ToString();
                        status = reader["Status"].ToString();
                        sbEmailAddresses.Append(username).Append("@aramco.com");
                    }
                }

并且调试器在调试它们时没有经过它们。它直接转到

var sEMailAddresses = sbEmailAddresses.ToString();

我不知道为什么。任何的想法?请你帮助我好吗?

4

2 回答 2

1

问题是变量username,descriptionstatusinSendSuggestionStatusToUser函数在您使用它们时并未明确分配。

也就是说,如果reader == null它们没有价值。

由于您稍后用于构造值,因此它可能description并且从未分配给)。这就是错误的含义。bodynull

一个简单的更改是明确地将它们分配给null空字符串或空字符串:

string username = null;
string description = null;
string status = string.Empty;

更新:

我现在注意到您是如何构建您的电子邮件地址的:

sbEmailAddresses.Append(username).Append("aramco.com");

除非以username结尾,@否则不会生成有效的电子邮件地址。电子邮件地址之间也没有分隔符,所以我不知道这将如何工作。

于 2012-07-07T11:16:53.167 回答
0

如果您的阅读器为空或不返回任何记录,您的用户名、描述、状态和 sbEmailAddress 将为空值。

于 2012-07-07T11:15:04.490 回答