i have mail.aspx page. which have to a compose mail facility? also, user can select from user account to user, cc and bccs. along with them when update panel update method calls all previous values from text box getting empty. for avoiding them I create one method that takes previous values and renders with them for a specific condition.
here is my method :
private void SetViewStateData(bool sTitle, bool sFromuser, bool sTouser, bool sCc, bool sBcc, bool sBody)
{
if (sTitle.Equals(true))
{
ViewState["sTitle"] = Request.Form.Get("txt_ComName1");
}
else
{
ViewState["sTitle"] = string.Empty;
}
if (sFromuser.Equals(true))
{
ViewState["sFromuser"] = Request.Form.Get("txt_ComName2");
}
else
{
ViewState["sFromuser"] = string.Empty;
}
if (sTouser.Equals(true))
{
ViewState["sTouser"] = Request.Form.Get("txt_ComName3");
}
else
{
ViewState["sTouser"] = string.Empty;
}
if (sCc.Equals(true))
{
ViewState["sCc"] = Request.Form.Get("txt_ComName4");
}
else
{
ViewState["sCc"] = string.Empty;
}
if (sBcc.Equals(true))
{
ViewState["sBcc"] = Request.Form.Get("txt_ComName5");
}
else
{
ViewState["sBcc"] = string.Empty;
}
if (sBody.Equals(true))
{
ViewState["sBody"] = Request.Form.Get("CKEditor1");
}
else
{
ViewState["sBody"] = string.Empty;
}
txt_ComName1.Text = sTitle.Equals(false) ? string.Empty : ViewState["sTitle"].ToString();
txt_ComName2.Text = sFromuser.Equals(false) ? string.Empty : ViewState["sFromuser"].ToString();
txt_ComName3.Text = sTouser.Equals(false) ? string.Empty : ViewState["sTouser"].ToString();
txt_ComName4.Text = sCc.Equals(false) ? string.Empty : ViewState["sCc"].ToString();
txt_ComName5.Text = sBcc.Equals(false) ? string.Empty : ViewState["sBcc"].ToString();
CKEditor1.Text = sBody.Equals(false) ? string.Empty : ViewState["sBody"].ToString();
}
this raise error when txt_ComName2.Text sets from view state value if bool status true or string.empty. however, this works fine with a first text box. when second it's given me an error like :
Server Error in '/EASYMAIL_METRO' Application. Object reference not set to an instance of an object. 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.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 240: }
Line 241: txt_ComName1.Text = sTitle.Equals(false) ? string.Empty : ViewState["sTitle"].ToString();
Line 242: txt_ComName2.Text = sFromuser.Equals(false) ? string.Empty : ViewState["sFromuser"].ToString();
Line 243: txt_ComName3.Text = sTouser.Equals(false) ? string.Empty : ViewState["sTouser"].ToString();
Line 244: txt_ComName4.Text = sCc.Equals(false) ? string.Empty : ViewState["sCc"].ToString(); Source File: c:\shalin\EASYMAIL_METRO\mail.aspx.cs Line: 242
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
mail.SetViewStateData(Boolean sTitle, Boolean sFromuser, Boolean sTouser, Boolean sCc, Boolean sBcc, Boolean sBody) in c:\shalin\EASYMAIL_METRO\mail.aspx.cs:242
mail.btn_add_Click(Object sender, EventArgs e) in c:\shalin\EASYMAIL_METRO\mail.aspx.cs:1823
mail.Page_Load(Object sender, EventArgs e) in c:\shalin\EASYMAIL_METRO\mail.aspx.cs:71
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +51
System.Web.UI.Control.OnLoad(EventArgs e) +95
System.Web.UI.Control.LoadRecursive() +59
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +678
what's going wrong with this...please help me, guys...