asp.net - C# Mail: why this function cannot send emails? -


in web-based asp.net suggestions box program, admin able see suggestions listed in gridview control username of owner. in last column of gridview, status listed there. when admin clicks on status of 1 of these suggestion, new pop-up window (asp.net ajax modalpopupextender) appeared listing possible status such as: actioned, approved... etc. , when admin selects 1 of these status, status of suggestion updated in database. works fine. want when user updates status of of suggestions, email notification sent owner regarding update of status of suggestion.

i wrote mail function don't know why not send email , getting error while debugging code:

use of unassigned local variable 'description'

i assigned value of [description] column in database don't know why getting error.

could me this?

i struggling getting username of updated suggestion. fyi, have following database design:

employee table: username, name... safetysuggestionslog: id, title, description, username, statusid safetysuggestionsstatus: id, status 

asp.net code:

<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" /> <%-- make status opened , edited through 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 * [safetysuggestionsstatus]"></asp:sqldatasource> <asp:button id="confirmbutton" runat="server" text="confirm" onclientclick="javascript:return confirm('are sure want send email notification safety suggestion owner?')" onclick="btnsendstatus_click" /> <asp:button id="okbutton" runat="server" text="close" /> </asp:panel> </contenttemplate> </asp:updatepanel> 

code-behind:

protected void lnksuggestionstatus_click(object sender, eventargs e) { linkbutton lnksuggestionstatus = sender linkbutton; //var safetysuggestionsid = //get reference row selected gridviewrow gvrow = (gridviewrow)lnksuggestionstatus.namingcontainer; //set selected index selected row selected row highlighted gridview1.selectedindex = gvrow.rowindex; //this hiddenfield used store value of id hiddenfield1.value = gridview1.datakeys[gvrow.rowindex].value.tostring(); //viewstate["username"] = gvrow.cells[4].text; //show modalpopup modalpopupextender1.show(); } public void btnsendstatus_click(object sender, eventargs e) { //get id of 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 status of safety suggestion string updatecommand = "update safetysuggestionslog set statusid= @statusid 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 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 mail system doesn't no recipients. 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 username of suggestion owner //and append (@aramco.com) it. using (sqlconnection conn = new sqlconnection(connstring)) { var sbemailaddresses = new system.text.stringbuilder(2000); //initiate varibles retreived database string username; string description; string status; // open db connection. conn.open(); string cmdtext2 = @"select username, description, status dbo.safetysuggestionslog ssl inner join safetysuggestionsstatus sss on (ssl.statusid = sss.id) (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> notify 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; email generated using &lt;a href='http://pmv/pssp/default.aspx'&gt;pmod safety services portal (pssp) &lt;/a&gt;. please not reply email. "; sendstatusbyemail(sbemailaddresses.tostring(), "", "notification of safety suggestion", body, true); sbemailaddresses.clear(); reader.close(); } conn.close(); } } 

note: know should not post lengthy code here, because want explain work , help.

update:

i modified code regarding assigning variables null , when debugged code, found reader doesn't work , did not read following:

if (reader != null) { if (reader.read()) { username = reader["username"].tostring(); description = reader["description"].tostring(); status = reader["status"].tostring(); sbemailaddresses.append(username).append("@aramco.com"); } } 

update 2:

in sendsuggestionstatustouser(string suggestionid) method, added break point following line:

string safetysuggestionid = suggestionid.tostring(); 

and added break point following line:

cmd.parameters.addwithvalue("@safetysuggestionid", convert.toint32(hiddenfield1.value)); 

and found first one, safetysuggestionid got data passed value. however, second 1 value of (hiddenfield1.value)is -1 , don't know why. also, added break points each 1 of following lines:

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"); } } 

and debugger did not go through them while debugging them. goes directly

var semailaddresses = sbemailaddresses.tostring(); 

and don't know why. idea? please me?

the problem variables username, description , status in sendsuggestionstatustouser function not definitely assigned @ point using them.

that is, if reader == null not have value.

since are using description construct value of body later on, could null (and never assigned to). that's meaning of error.

one simple change assign them - either null or empty string:

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

update:

i have notices how constructing email addresses:

sbemailaddresses.append(username).append("aramco.com"); 

unless username ends @, not produce valid email address. there no separator between email addresses, don't know how ever work.


Comments

Popular posts from this blog

javascript - backbone.js Collection.add() doesn't `construct` (`initialize`) an object -

c++ - Accessing inactive union member and undefined behavior? -

php - Get uncommon values from two or more arrays -