How to solve " A first chance exception of type" exception when - sql

I am using the following code to connect to an Microsoft SQl Server database but I always get an error
var conn = #"Server=(local)\localhost;Database=Training;";
using (SqlConnection objSqlConnection = new SqlConnection(conn))
{
try
{
objSqlConnection.Open();
Console.Write("Connection is successfull");
}
catch (System.Data.SqlClient.SqlException sqlException)
{
Console.WriteLine(sqlException.Message);
}
}
and the error I get is:
A first chance exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
The thread 'vshost.RunParkingWindow' (0xd28) has exited with code 0 (0x0).
The thread '<No Name>' (0x1550) has exited with code 0 (0x0).
The program '[6440] DataAccess.vshost.exe: Managed (v4.0.30319)' has exited with code 0 (0x0).

Make sure your System.Data.dll is correctly referenced in your code.
First chance exception messages most often do not mean there is a problem in the code. For applications / components which handle exceptions gracefully, first chance exception messages let the developer know that an exceptional situation was encountered and was handled.
For code without exception handling, the debugger will receive a second chance exception notification and will stop with a unhandled exception.

Related

.NET Core 3.1 - application stops for no reason - System.Threading.Tasks.TaskCanceledException

My .NET Core API main loop just stops after a while for no reason
I get an exception
Exception thrown: 'System.Threading.Tasks.TaskCanceledException' in System.Private.CoreLib.dll
but I cannot find anything relevant in google
how can I figure what makes my API crash ?
why is it not caught in my try {} catch ?
try
{
Log.Information("Started application");
host.Run(); <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<runs for 10-20' then gets to next line
Log.Information("Stopped application");
}
catch (Exception exception)
{
Log.Fatal(exception, "Application terminated unexpectedly");
}
finally
{
Log.CloseAndFlush();
}
thanks for helping me on this
Your Cancelation token throws that exception so somewhere you reach the timeout.
For example when I debug my tests it always was throwing such exception as on the beginning in the async function I put new CancellationTokenSource(1000 <-timeout defined).Token There is no problem to catch TaskCanceledException.

asp.net mvc exception in controller not beign handled at custom ErroHandler

I have a global ErrorHanlder (Derive from HandleErrorAttribute) that works fine and catches all errors that occur. But I have a situation in which, home controller can throw exception. I inspected and saw that error thrown from controller is not handled in my custom ErrorHandler and asp.net gives this:
Exception of type 'Exception' was thrown.
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.
Would you help me please ?
Code:
public HomeController()
{
_ServiceAsync = new ServiceAsyncProvider();
}
class ServiceAsyncProvider
{
public ServiceAsyncProvider()
{
throw new Exception();
}
}
While your code snippet is a little unclear on this point, it looks like your _ServiceAsync is being initialized in your controller's constructor. Exceptions thrown during controller construction are not handled by the HandleError filter.
See this related question for more information: Handling Exceptions that happen in a asp.net MVC Controller Constructor
You are throwing an exception in the constructor. These exceptions are not handled by the filter.

Catch exceptions from native API call

If exceptions occurred when windows is processing the message, how can I catch it?
The code below crashes when I thrown an exception in the key down event handler.
try{
WindowsNative.SendMessage(form.Handle, WM_KEYDOWN, KeyValue, 0);
}catch{}

Why the method InitializeComponent throw an error?

I have created a new SplitPage inside VS2012 and I load this page with:
this.Frame.Navigate(typeof(SplitPage), "AllGroups");
An error is throw from :
this.InitializeComponent();
Visual Studio show me this exception :
An exception of type 'Windows.UI.Xaml.Markup.XamlParseException' occurred
in IC2.exe but was not handled in user code
WinRT information: Cannot find a Resource with the Name/Key SubtitleTextStyle
[Line: 88 Position: 104]
If I catch the error I found the message :
Unspecified error
How can I avoid this error ?
I think the problem lies in the xaml where you don't have the style in the resources that is being call "SubtitleTextStyle". You may want to check on that or remove that line away in your xaml.

Throwing an exception in a WCF service throws a CommunicationException

Whenever I throw an exception in my service, another exception is thrown right after it:
System.ServiceModel.CommunicationException: There was an error reading from the
pipe: Unrecognized error 109 (0x6d). ---> System.IO.PipeException: There was an
error reading from the pipe: Unrecognized error 109 (0x6d).
I'm implementing IErrorHandler so I can log (using log4net) all unhandled exceptions:
bool IErrorHandler.HandleError(Exception error)
{
if (!(error is FaultException))
{
logger.Fatal("Unhandled Exception", error);
}
return false;
}
Any idea why is that?
Problem was client calling Abort on the channel whenever I returned a fault exception.