The below code is throwing exception at run time but does not throw exception while debugging in Visual Studio. This is really causing difficulty for me to detect the cause of exception. Below I have also placed the exception image for reference.
namespace CheckforContractorLogin.VisualWebPart1
{
public partial class VisualWebPart1UserControl : UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string loginName = string.Empty;
string coc_url = string.Empty;
SPQuery spQuery = new SPQuery();
spQuery.Query = "<Where><Eq><FieldRef Name='LoginName' /><Value Type='Text'>" + currentUser + "</Value></Eq></Where>";
Guid _spSiteID = SPContext.Current.Site.ID;
Guid _spWebID = SPContext.Current.Site.OpenWeb().ID;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite _spSite = new SPSite(_spSiteID))
{
using (SPWeb _spWeb = _spSite.OpenWeb(_spWebID))
{
//if user has already accepted the COC
SPList getSPList = _spWeb.Lists["RedirectUrl"];
SPListItemCollection getspItemColl = getSPList.Items;
foreach (SPListItem item in getspItemColl)
{
if (Convert.ToString(item["Title"]) == "Policy Acceptance")
{
coc_url = Convert.ToString(item["Url"]);
}
}
SPList spList = _spWeb.Lists["Policy Acceptance Status"];
SPListItemCollection spItemColl = spList.GetItems(spQuery);
bool result = getADUserInfo();
if ((spItemColl.Count == 0) && (result))
{
Response.Redirect(coc_url);
}
}
}
});
}
}
protected string currentUser
{
get
{
string currentUser1 = HttpContext.Current.User.Identity.ToString();
int index = currentUser1.IndexOf("\\") + 1;
string currentLoginUser = currentUser1.Substring(index);
return currentLoginUser;
}
}
protected bool getADUserInfo()
{
DirectoryEntry dentry = null;
DirectorySearcher dsearcher = null;
string ldap = string.Empty;
string empID = string.Empty;
string _empID = string.Empty;
try
{
Guid spSiteGUID = SPContext.Current.Site.ID;
Guid spWebGUID = SPContext.Current.Site.OpenWeb().ID;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite elevatedSiteColl = new SPSite(spSiteGUID))
{
using (SPWeb elevatedWeb = elevatedSiteColl.OpenWeb(spWebGUID))
{
SPList spList = elevatedWeb.Lists["LDAP_Paths"];
SPQuery spQuery = new SPQuery();
spQuery.Query = "<Where><Eq><FieldRef Name='OU'/>"+ "<Value Type='Text'>QD</Value></Eq></Where>";
SPListItem spItem = spList.GetItemById(1);
ldap = spItem["Path"].ToString();
}
}
dentry = new DirectoryEntry();
dentry.Path = ldap;
dentry.Username = "******\\sp_admin";
dentry.Password = "******";
dsearcher = new DirectorySearcher(dentry);
dsearcher.Filter = String.Format("(&(ObjectCategory=Person)(sAMAccountName=" + currentUser + "))");
SearchResult searchResult = dsearcher.FindOne();
dentry = searchResult.GetDirectoryEntry();
if (searchResult != null)
{
if (dentry.Properties.Contains("physicalDeliveryOfficeName"))
{
empID = dentry.Properties["physicalDeliveryOfficeName"][0].ToString();
}
}
});
if (empID.Contains("QA-"))
{
return true;
}
else
return false;
}
catch (Exception e)
{
throw e;
}
finally
{
dentry.Close();
dentry.Dispose();
dsearcher.Dispose();
}
}
}
}