0

Im trying to connect mysql database through Xamarin android. My codes are like this below:

 con = new MySqlConnection("xxxxxxxxxxxx");
 con.Open();//gives error at this line

It gives me this error below:

Unhandled Exception: System.TypeInitializationException: The type initializer for 'MySql.Data.MySqlClient.Replication.ReplicationManager' threw an exception. occurred

When I check C# Interactive, it shows this;

            con.Open();

(1,17): error CS0103: 'con' does not exist in the current context

Cruwiel
  • 25
  • 1
  • 1
  • 8
  • did you debug to see the value of `con`? – Juan Carlos Oropeza May 08 '18 at 18:55
  • @JuanCarlosOropeza well, this is the screenshot of the value of con: https://image.ibb.co/nkVSKS/Ekran_Al_nt_s.png – Cruwiel May 08 '18 at 19:04
  • Did you enabled SQL debugging? https://learn.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2008/09x4a6at(v=vs.90) – Eru May 08 '18 at 19:16
  • You should check your string connection. The second error is because you havent define `con` on the C# interactive and that is a totally diferent thing – Juan Carlos Oropeza May 08 '18 at 19:28
  • @Eru I read it from somewhere else but I couldnt enable it. Because there is no "Debug" option under project properties of xamarin projects. How can I enable it in xamarin project? – Cruwiel May 08 '18 at 19:28
  • Exception message for using MySql.Data with Xamarin on Android is a duplicate of this question: https://stackoverflow.com/questions/50137205/androidapp-and-mysqlconnection-didnt-work-connection-open – Bradley Grainger May 10 '18 at 14:35

1 Answers1

0

I think first of all, you should check your connection string, in c# interactive you first need to initialize variable to use them then. You can't use variables from your code, cause c# interactive doesn't know them. You should try this in c# interactive:

 con = new MySqlConnection(<your Connection string>);
 con.Open()
Kacper
  • 451
  • 6
  • 17
  • My connection string is working on UWP without error. When I type these into c# interactive, it gives more errors like "CS0246: The type or namespace name 'MySqlConnection' could not be found" – Cruwiel May 08 '18 at 19:34
  • in c# interactive you also has to put "using". use alt+enter or your key shortcut to do this – Kacper May 08 '18 at 19:35
  • MySQL.Data is installed. I installed it via nuGet packages – Cruwiel May 08 '18 at 19:43
  • Sorry I'm new at these, i think its because c# interactive cant see my references, when i type "using MySql.Data.MySqlClient;" it gives same error. I dont know how to add references there :( – Cruwiel May 08 '18 at 19:43
  • I found there is guide how to use your code from project, but it doesn't use classes from nugets https://stackoverflow.com/a/11135787/9217084 – Kacper May 08 '18 at 19:46