1

I'm trying to run this line of code for connecting to mysql using csharp and dotnet core 2.1.300 (sdk).

 string server = "localhost";
 string database = "db";
 string uid = "root";
 string password = "";
 string connectionString;
 connectionString = "SERVER=" + server + ";" + "DATABASE=" + database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
connection = new MySqlConnection(connectionString);
connection.Open();

But last line will raise an exception that I cannot understand what goes wrong to resolve it. running dll of project using dotnet shows me this:

dotnet : 
At line:1 char:1
+ dotnet C:\Users\foo\bar\project ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError


Unhandled Exception: 


Cannot print exception string because Exception.ToString() failed.

I tried recommendations by posts like this similar question and added System.Runtime.Serialization.Primitives assembly to my references in VS 2017 but it did not help.

I'm modifying this csharp project actually, so that it write its output to a database. https://github.com/vcsjones/AuthenticodeLint

Changing the MySqlException to Exception will result in this output:

Unhandled Exception: 
System.IO.FileNotFoundException: Could not load file or assembly 'System.Drawing.Common, Version=0.0.0.0, Culture=neutral, 
PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified.
   at System.ModuleHandle.ResolveType(RuntimeModule module, Int32 typeToken, IntPtr* typeInstArgs, Int32 typeInstCount, IntPtr* 
methodInstArgs, Int32 methodInstCount, ObjectHandleOnStack type)
   at System.ModuleHandle.ResolveTypeHandleInternal(RuntimeModule module, Int32 typeToken, RuntimeTypeHandle[] 
typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)
   at System.Reflection.RuntimeModule.ResolveType(Int32 metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
   at System.Reflection.CustomAttribute.FilterCustomAttributeRecord(CustomAttributeRecord caRecord, MetadataImport scope, Assembly& 
lastAptcaOkAssembly, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, Boolean 
mustBeInheritable, Object[] attributes, IList derivedAttributes, RuntimeType& attributeType, IRuntimeMethodInfo& ctor, Boolean& 
ctorHasParameters, Boolean& isVarArg)
   at System.Reflection.CustomAttribute.IsCustomAttributeDefined(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, 
RuntimeType attributeFilterType, Int32 attributeCtorToken, Boolean mustBeInheritable)
   at System.Reflection.CustomAttribute.IsDefined(RuntimeType type, RuntimeType caType, Boolean inherit)
   at System.Diagnostics.StackTrace.ShowInStackTrace(MethodBase mb)
   at System.Diagnostics.StackTrace.ToString(TraceFormat traceFormat)
....
Peggy
  • 639
  • 9
  • 28
  • Is it on the `connection.Open();` line? Can we get a full stack trace please? – Trevor Oct 01 '19 at 20:43
  • @Çöđěxěŕ: I cannot print stack trace.(Or maybe I don't know how to do it! From my understanding, although I'm catching the exception raised by that last line, printing the stack trace or exception message raises another exception and fails! ) – Peggy Oct 01 '19 at 20:53
  • I'm handling exception like this: try { connection.Open(); } catch (MySqlException ex) { verboseWriter.LogMessage(ex.StackTrace); } Even if I comment out this line verboseWriter.LogMessage(ex.StackTrace); I still get that "Unhandled Exception" error. – Peggy Oct 01 '19 at 20:54
  • Change your exception to `Exception` not `MySqlException`. What do you get then when you put a breakpoint there and then check `Message`... If that doesn't work have you tried at least version 2.2; if I remember correctly this was an issue in 2.1x... – Trevor Oct 01 '19 at 20:57
  • I updated my post as you asked (changed to Exception). – Peggy Oct 01 '19 at 21:02
  • well at least you have something useful now! Can you open up Package Manager and then run `Install-Package System.Drawing.Common -Version 4.6.0`... Once done, can you see if you still have the issue? I think by default this wasn't included in core. – Trevor Oct 01 '19 at 21:06
  • It worked!!!!! Thanks a lot. I'm new in using VS and dotnet and were struggling with this problem since morning :\\\\ – Peggy Oct 01 '19 at 21:10

1 Answers1

1

After changing your exception type, we have something to go after.

Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'System.Drawing.Common, Version=0.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified. at System.ModuleHandle.ResolveType(RuntimeModule module, Int32 typeToken, IntPtr* typeInstArgs, Int32 typeInstCount, IntPtr* methodInstArgs, Int32 methodInstCount, ObjectHandleOnStack type) at System.ModuleHandle.ResolveTypeHandleInternal(RuntimeModule module, Int32 typeToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext) at System.Reflection.RuntimeModule.ResolveType(Int32 metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments) at System.Reflection.CustomAttribute.FilterCustomAttributeRecord(CustomAttributeRecord caRecord, MetadataImport scope, Assembly& lastAptcaOkAssembly, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, Object[] attributes, IList derivedAttributes, RuntimeType& attributeType, IRuntimeMethodInfo& ctor, Boolean& ctorHasParameters, Boolean& isVarArg) at System.Reflection.CustomAttribute.IsCustomAttributeDefined(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, RuntimeType attributeFilterType, Int32 attributeCtorToken, Boolean mustBeInheritable) at System.Reflection.CustomAttribute.IsDefined(RuntimeType type, RuntimeType caType, Boolean inherit) at System.Diagnostics.StackTrace.ShowInStackTrace(MethodBase mb) at System.Diagnostics.StackTrace.ToString(TraceFormat traceFormat) ....

This exception is caused by not having System.Drawing.Common library. Open up Package Manager and then run Install-Package System.Drawing.Common -Version 4.6.0

In this version you have, the library doesn't have the dependencies you need, System.Drawing.Common, they should have updated this to include it.

Trevor
  • 7,777
  • 6
  • 31
  • 50