I am setting a Session variable in an HttpHandler, and then getting its value in the Page_load event of an ASPX page. I'm setting it using
public void ProcessRequest(HttpContext context)
{
HttpPostedFile file = context.Request.Files["Filedata"];
context.Session["WorkingImage"] = file.FileName;
}
(And before someone suggests that I check the validity of file.FileName, this same problem occurs if I hard-code a test string in there.) It's working just fine in IE, but in Firefox the Session Variable is not found, getting the "Object reference not set to an instance of an object" error in the following code:
protected void Page_Load(object sender, EventArgs e)
{
string loc = Session["WorkingImage"].ToString();
}
Has anyone encountered this problem - and hopefully come up with a means for passing the session variable?
This is for an HTTPHandler? If this by some chance has something to do with Flash, and Flash is making the request, you will be very interested in reading about the Flash Cookie Bug. Basically, Flash only forwards IE cookies.
The easist fix is to call correctCookie at Application_BeginRequest in your Global.asax and put the SessionId in the querystring of the Flash request.
Public Shared Sub correctCookie()
Try
Dim session_cookie_name As String = "ASP.NET_SESSIONID"
Dim session_value As String = HttpContext.Current.Request.QueryString("sid")
If session_value IsNot Nothing Then
UpdateCookie(session_cookie_name, session_value)
End If
Catch ex As Exception
End Try
End Sub
Private Shared Sub UpdateCookie(ByVal cookie_name As String, ByVal cookie_value As String)
Dim cookie As HttpCookie = HttpContext.Current.Request.Cookies.[Get](cookie_name)
If cookie Is Nothing Then
Dim cookie1 As New HttpCookie(cookie_name, cookie_value)
HttpContext.Current.Response.Cookies.Add(cookie1)
Else
cookie.Value = cookie_value
HttpContext.Current.Request.Cookies.[Set](cookie)
End If
End Sub
Related
I intend to open an outlook inbox page (see image) when the button is clicked. I use the code below but nothing happened. Hope to get some help
private void button6_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Outlook.Application outlookApplication = new Microsoft.Office.Interop.Outlook.Application(); ;
Microsoft.Office.Interop.Outlook.AppointmentItem appointmentItem = (Microsoft.Office.Interop.Outlook.AppointmentItem)outlookApplication.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olAppointmentItem);
}
I managed to solve my own question. I didn't state in my original question but outlook app is already downloaded inside my laptop.
private void button6_Click(object sender, EventArgs e)
{
Process.Start("outlook.exe");
}
Thanks to all of yall suggestions
I checked your code without an problem. So you need to track your error message of WindowsForm APP and confirm closed your Outlook. In general, you may getting the error about COM ID issue.
Please refer to the following links:
How to open Outlook new mail window c#
Code:
Outlook.Application oApp = new Outlook.Application ();
Outlook._MailItem oMailItem = (Outlook._MailItem)oApp.CreateItem ( Outlook.OlItemType.olMailItem );
oMailItem.To = address;
// body, bcc etc...
oMailItem.Display ( true );
Debug Error:
new Outlook.Application() thorws error if Outlook app is running
Try something like the following (off the top of my head):
Outlook.Application oApp = new Outlook.Application ();
Outlook.Namespace ns = oApp.GetNamespace("MAPI");
ns.Logon();
Outlook.MAPIFolder inbox = ns.GetDEfaultFolder(olFolderInbox);
if (oApp.Explorers.Count > 0)
{
Outlook.Explorer expl = oApp.Explorers[1];
expl.CurrentFolder = inbox;
}
else
{
inbox.Display();
}
I want to do some functionality when user moves a mail item (from a folder to another folder). So I want to capture the mail item move event with the outlook add-in.
I think this should be possible with following event handlers,
MAPIFolderEvents_12_BeforeItemMoveEventHandler
ItemsEvents_ItemRemoveEventHandler
I tried with both of the above event handles. But they didn’t work for me. Could someone provide an example. Here is the code for MAPIFolderEvents_12_BeforeItemMoveEventHandler.
Outlook.Folder fldr;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
fldr = (Outlook.Folder)Application.Session.GetDefaultFolder(OlDefaultFolders.olFolderTasks);
fldr.BeforeItemMove += new Microsoft.Office.Interop.Outlook.
MAPIFolderEvents_12_BeforeItemMoveEventHandler
(Folder_BeforeItemMove);
}
private void Folder_BeforeItemMove(object anItem, MAPIFolder aMoveToFolder, ref bool Cancel)
{
Outlook.MailItem mailItem = (anItem as Outlook.MailItem);
//Do other stuff
}
The object that raises the events (fldr) must be declared on the class level instead of local to avoid being released by the Garbage Collector.
Had a similar requirement for iterating through MailFolders.
So please try as below:
Outlook.MAPIFolder mapifldr;
Outlook.Folder fldr;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
mapifldr=Application.Session.GetDefaultFolder(OlDefaultFolders.olFolderTasks);
fldr = (Outlook.Folder) Application.GetNamespace("MAPI").GetFolderFromID(mapifldr.EntryID);
fldr.BeforeItemMove += new Microsoft.Office.Interop.Outlook.
MAPIFolderEvents_12_BeforeItemMoveEventHandler
(Folder_BeforeItemMove);
}
This is a very old post but hope it saves time for others.
So I've created a page called "Settings". Obviously in this page is where the settings are for the app. In the Settings page I've added 2 ToggleSwitches and 1 Listpicker. Using the Nokia Developer website on basics of Saving and reading Settings i managed to pull it off so it saves the states of the toggleswitches and listpicker.
The problem i'm having right now is that i need a way to read these saved setting values on the first page when the app starts so it can prepare the app accordingly. Soo far this is what i have in the Settings page:
Imports System.IO.IsolatedStorage
Partial Public Class Settings
Inherits PhoneApplicationPage
Private AppSettings As IsolatedStorageSettings
Public Sub New()
InitializeComponent()
AppSettings = IsolatedStorageSettings.ApplicationSettings
ListPicker1.Items.Add("Saved Notes")
ListPicker1.Items.Add("Important")
End Sub
Protected Overrides Sub OnNavigatedTo(e As NavigationEventArgs)
Try
Tg1.IsChecked = CBool(AppSettings("UseAccentColor"))
Tg2.IsChecked = CBool(AppSettings("GoBack"))
ListPicker1.SelectedIndex = CByte(AppSettings("StartListFalse"))
Catch ex As KeyNotFoundException
AppSettings.Add("UseAccentColor", False)
AppSettings.Add("GoBack", False)
AppSettings.Add("StartListFalse", False)
AppSettings.Save()
End Try
End Sub
Protected Overrides Sub OnNavigatedFrom(e As NavigationEventArgs)
System.Diagnostics.Debug.WriteLine("Exiting, so save now")
AppSettings("UseAccentColor") = Tg1.IsChecked
AppSettings("GoBack") = Tg2.IsChecked
AppSettings("StartListFalse") = ListPicker1.SelectedIndex
AppSettings.Save()
End Sub
End Class
So soo far it saves on exit but i need a way to load these from startup i.e. my MainPage. Like a way to refer to this page and according to these settings change whatever needs to be changed.
How can i do this?
Thanks!
You managed to save Settings to IsolatedStorage, and IsolatedStorage is accesssible from any page of your application. So in MainPage, just read those setting from IsolatedStorage instead of the Settings Page.
EDIT :
You can do it just like in OnNavigatedTo method in Settings Page
Private AppSettings As IsolatedStorageSettings = IsolatedStorageSettings.ApplicationSettings
'Tg1.IsChecked is analog with useAccentColor
Dim useAccentColor As Boolean = CBool(AppSettings("UseAccentColor"))
'Tg2.IsChecked = goBack
Dim goBack As Boolean = CBool(AppSettings("GoBack"))
'ListPicker1.SelectedIndex = startListFalse
Dim startListFalse As Byte = CByte(AppSettings("StartListFalse"))
In my app I'm using "HandleError" whereby if an error happens, my "Error.vbhtml" view renders. This is working great, except now I want to also log the error. I've built a custom HandleError Class, Inherited the HandleErrorAttribute, and Overridden the OnException method.
Now my error gets logged, but the Error.vbhtml view doesn't get rendered... what praytell am I messing?
Imports System.Web.Mvc
Namespace Mvc.Attributes
Public Class HandleError : Inherits System.Web.Mvc.HandleErrorAttribute
Private ExceptionService As Domain.IExceptionService
Public Sub New()
ExceptionService = New Domain.ExceptionService(New Domain.ExceptionRepository)
End Sub
Public Overrides Sub OnException(ByVal exceptionContext As ExceptionContext)
''# Log the exception if it has not been handled elsewhere
If Not exceptionContext.ExceptionHandled Then
ExceptionService.AddException(exceptionContext.Exception)
ExceptionService.SubmitChanges()
''# Signal to the system that we've handled the exception
exceptionContext.ExceptionHandled = True
End If
End Sub
End Class
End Namespace
I just took a look at the source code of the HandleError method at Codeplex. I scooped some of the code from there
Dim controllerName As String = DirectCast(filterContext.RouteData.Values("controller"), String)
Dim actionName As String = DirectCast(filterContext.RouteData.Values("action"), String)
Dim model As New HandleErrorInfo(filterContext.Exception, controllerName, actionName)
filterContext.Result = New ViewResult() With { _
.ViewName = View, _
.MasterName = Master, _
.ViewData = New ViewDataDictionary(Of HandleErrorInfo)(model), _
.TempData = filterContext.Controller.TempData _
}
filterContext.ExceptionHandled = True
filterContext.HttpContext.Response.Clear()
filterContext.HttpContext.Response.StatusCode = 500
''# Certain versions of IIS will sometimes use their own error page when
''# they detect a server error. Setting this property indicates that we
''# want it to try to render ASP.NET MVC's error page instead.
filterContext.HttpContext.Response.TrySkipIisCustomErrors = True
This appears to work
On the site we are building. We need to be able to redirect the user to a default page when his session has ended.
At first sight we used the Session_End with a Response.Redirect to do this job.
Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
Response.Redirect("~/global/exit.aspx")
End Sub
But it generates a crapload of Response is not available in this context errors. Naturally we don't want to spam our servers error logs.
What is the most efficient way to handle session ending with ASP.NET 2.0?
We added the following code to the global.asax.cs file:
private void IsAuthenticated()
{
string vFileName = Path.GetFileName(HttpContext.Current.Request.Path);
string vExt = Path.GetExtension(vFileName).ToLower();
if ((vFileName != "Login.aspx") && (vExt == ".aspx"))
{
if (HttpContext.Current.Session["LoggedIn"] == null)
{
HttpContext.Current.Response.Redirect("~/Login.aspx");
}
}
}
void Application_PostAcquireRequestState(object sender, EventArgs e)
{
IsAuthenticated();
}
NS: The first line in our Global .asax file is :
<%# Application Inherits="???.Global" Language="C#" %>
We handled it by checking if the session data existed in Application.Begin_Request for the pages that were user specific and if it didn't then redirecting the user to login or homepage.
You can use the session_end method, as it is not a user callable method, it is triggered by ASP.NET and response isn't available, as it is not part of the request.
The biggest way is to check and see if session is missing, somewhere in the load of your pages and redirect back to the main.
What I have done before is to put this checking logic inside a "RestrictedPage.master" master page that was used for all session specific pages, if session is lost, it redirects.