1

I want to connect to mysql server from C#. I found some code on the net but somewhere there is something wrong because i get

A first chance exception of type 'System.ArgumentException' occurred in System.Data.dll

error.

private void Initialise() { server = "dns to server"; database = "db_name"; uid = "root"; password = "password"; string connectionString; connectionString = "SERVER=" + server + ";" + "DATABASE=" + database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";"; OR connectionString = "Server=xxx.no-ip.org;Database=rdb;"+ "Uid=root;Pwd=wHt2%Zt;"; connection = new MySqlConnection(connectionString); if (this.OpenConnection() == true) Console.Out.Write("SUCCESS"); else Console.Out.Write("ERROR"); } private bool OpenConnection() { try { connection.Open(); return true; } catch (MySqlException ex){ switch (ex.Number) { case 0: MessageBox.Show("Cannot connect to server. Contact administrator"); break; case 1045: MessageBox.Show("Invalid username/password, please try again"); break; } return false; } } 

I don't get any message on the console. I added Mysql.Data as a reference to my project and i used using MySql.Data.MySqlClient;

I also tried connectig through gui but with no luck. Ideas ?

Edit 1 : with either connection string my program is still not working.

Edit 2 : OpenConnection method added.

Edit 3 : This is the error i get !

4
  • 1
    Can you show the code for OpenConnection() ? Also try changing "PASSWORD=" to "Pwd=" Commented Aug 29, 2012 at 19:47
  • question edited. my program does not get that far. i think that my problem is with connection = new MySqlConnection(connectionString) this part. Commented Aug 29, 2012 at 19:50
  • if you wrap the line that throws the exception in a try catch block you will have more info about why is throwing the exception. Also try the changing what i said in the first post. Commented Aug 29, 2012 at 19:54
  • the problem was my setup. first problem : my password contained a semi colon. second problem : my server did not accepted remote login. i haven't test my code yet but i will test it tonight. thanks a lot for your ideas. Commented Aug 30, 2012 at 9:39

3 Answers 3

1

p.s.

http://www.connectionstrings.com/mysql

probably you need to change your approach...use a webconfig or app config to setup and read your connectionstrings from the config...

I would also recommend you to read this...

http://www.codeproject.com/Articles/12300/An-ASP-NET-Application-Using-a-MySQL-Database

UPDATE

Based on your updated findings...there can be two problems, first double check your connectionstring, Second is to check if the user "root" has the required permissions.

Sign up to request clarification or add additional context in comments.

2 Comments

i don't think my program is that much sensitive. i want it to work, them make it beauty.
app.config/web.config is beauty? i think it's a standard approach to store your connectionstrings. lazy usage.
1

Look here: http://www.connectionstrings.com/mysql Have all.

Comments

0
Server=myServerAddress;Port=1234;Database=myDataBase;Uid=myUsername;Pwd=myPassword; 

I think the problem is your database server:

Server=xxx.no-ip.org 

It seems your app doesn't find the server. Check firewall.

You can debug and set an interruption point and check which exception it's throwing, not just the number

3 Comments

My problem was the password. It contained ";" among other signs. I change it and now i get "ERROR". I have to investigate this error now.
Might not be the case here but dbs are often configured not to allow remote root login in MySQL I seem to recall. Might want to double check that root is allowed to connect remotely.
i will check that and come back with an answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.