1

I have a windows application that I am making for a friend, but for some reason, when I try to connect, the app crashes. a message box pops up and says "DBTest hastopped working, windows is checking for a solution to the problem..." after 5 seconds, it closes and the app doesn't launch.

However, when I comment out the info from the app, it works again, but only the app launches, and I can't connect anymore??

I checked the connection on MySQL workbench, and it lets me connect to the website's database, and I can connect to the sites DB remotely, but it will not allow me to do so in the application.

Heres the code that im testing, and the app keeps crashing.

I'm at a loss.

    public partial class Form1 : Form
{
    public MySqlConnection connection;
    public Form1()
    {

        InitializeComponent();

        DBInfo db = new DBInfo();

        string server;
        string database;
        string uid;
        string password;

        server = "XXX";
        database = "XXX";
        uid = "XXX";
        password = "XXX";
        string connectionString;
        connectionString = "Server=" + server + ";" + "Database=" + database + ";"
                + "Uid=" + uid + ";" + "Password" + password + ";";

        connection = new MySqlConnection(connectionString);

        try
        {
            connection.Open();
        }
        catch (MySqlException ex)
        {
            switch (ex.Number)
            {
                case 0:
                    MessageBox.Show("Cannot connect to Server. Contact Admin.");
                    break;

                case 1045:
                    MessageBox.Show("Invalid Username/Password, please try again.");
                    break;
            }
        }
    }

UPDATE:

System.ArgumentException: Format of the initialization string does not conform to specification starting at index 54. at System.Data.Common.DBConnectionOptions.GetKeyValuePair(String connectionString, Int32 currentPosition, StringBuilder buffer, Boolean useOdbcRules, String& keyname, String& keyvalue) at System.Data.Common.DbConnectionOptions.ParseInternal(Hashtable parsetable, String connectionString, Boolean buildChain, Hashtable synonyms, Boolean firstkey) at System.Data.Common.DbConnectionOptions..ctor(String connectionString, Hashtable synonyms, Boolean use OdbcRules) at System.Data.Common.DbConnectionStringBuilder.set_ConnectionString(String value) at MySql.Data.MySqlClient.MySqlConnectionStringBuilder..ctor(string connStr) at MySql.Data.MySqlClient.MySqlConnection.set_ConnectionString(String value) at MySql.Data.MySqlClient.MySqlConnection..ctor(String connectionString) at DBtest.Form1..ctor() in C:\Users\AlexMoreno\Dcouments\Visual Studio\2008\Projects\DBtest\Form1.cs:line 38

Alex Moreno
  • 138
  • 1
  • 11
  • Changing the code you posted to now uncomment `connection.Open();` and add try/catch logic that was not there originally...changes the question quite a bit. ;) – J0e3gan May 12 '13 at 02:21
  • _However, when I comment out the info from the app, it works again, but only the app launches, and I can't connect anymore??_ So does this mean the `connection.Open();` statement that you originally had commented out in your code, or does it mean other code that you have not shown commented out? It is tough to hit a target that is shifting and unclear. – J0e3gan May 12 '13 at 02:24
  • 1
    sorry, Im just trying things as I go. To answer your second statement, its both the "connection.Open()", and "connection = new MySQLConnection(connectionstring);" if I comment both out, it works again. – Alex Moreno May 12 '13 at 02:25
  • Booyah! The exception message and stack trace that you added as an update is exactly what we need to see to help. – J0e3gan May 12 '13 at 02:56
  • See my update to my answer. I think you are simply missing an `=` after `Password` in your connection string. – J0e3gan May 12 '13 at 03:00
  • -1 => +1 for the effort you made to clear up your question. Sorry that I didn't simply see the missing `=` sooner. :) – J0e3gan May 12 '13 at 03:02
  • omg... wow.... thank you for helping me for so long, even though it was only a simple syntax error on my part. i really appreciate it. – Alex Moreno May 12 '13 at 03:03
  • No problem. (Although I'll take a +1 on the answer as a reward.) ;) Sometimes a second pair of eyes is key in the fight against the law of parsimony. – J0e3gan May 12 '13 at 03:08
  • 1
    I would, but I need +15 on the site before I can start "+1" on comments, sorry :( – Alex Moreno May 12 '13 at 03:12
  • Truly a gut-busting LOL on my end. :) LOL I meant on the answer, but you might have the same problem - can't recall the required reputation. I would move the extended comments to chat, but you are short on reputation to do that too. Soon you'll have enough for both. – J0e3gan May 12 '13 at 03:13
  • the problems of being a noobie >< lol. it happens, the sad part is mysql isnt compilers syntax, so the messagebox is an overextended syntax message. haha. I really do appreciate the help you've given me. you have no idea how relieved I feel at the moment. – Alex Moreno May 12 '13 at 03:16

1 Answers1

2

Wrap a try/catch around the statement to open the connection, and get the details of the exception that is likely being thrown and unhandled:

public partial class Form1 : Form
{
    public MySqlConnection connection;
    public Form1()
    {

        InitializeComponent();

        //DBInfo db = new DBInfo(); // would comment this out since you're not using it

        string server;
        string database;
        string uid;
        string password;

        server = "XXX";
        database = "XXX";
        uid = "XXX";
        password = "XXX";
        string connectionString;
        connectionString = "Server=" + server + ";" + "Database=" + database + ";"
                + "Uid=" + uid + ";" + "Password" + password + ";";

        //connection = new MySqlConnection(connectionString); // not here - for troubleshooting at least

        try
        {
            connection = new MySqlConnection(connectionString); // relocated from above for troubleshooting
            connection.Open();
        }
        (Exception ex) // Yes, Exception - until you know more about what is happening.
        {
            MessageBox.Show(ex.ToString());
        }
    }
}

You could output the details of the error another way - e.g. log them, output them to Debug.Console if you are going to debug the app in Visual Studio; take your pick; you just need to reality check that an exception is unhandled and get its details to proceed.

Also, try commenting out DBInfo db = new DBInfo();. You don't seem to be using it anyway.

UPDATE:

You are missing an = in your connection string - right after Password. So...

connectionString = "Server=" + server + ";" + "Database=" + database + ";"
            + "Uid=" + uid + ";" + "Password" + password + ";";

...should be:

connectionString = "Server=" + server + ";" + "Database=" + database + ";"
            + "Uid=" + uid + ";" + "Password=" + password + ";";
J0e3gan
  • 8,287
  • 9
  • 48
  • 76
  • Hi, i tried both ways, the one I have above, and your way, and the app still crashes. I have no idea why. – Alex Moreno May 12 '13 at 02:15
  • 1
    Hey, I think im closer to solving the problem? i got a really loooong message box... wasnt too friendly. http://tinypic.com/r/30j4ldv/5 trying to look this up somewhere. – Alex Moreno May 12 '13 at 02:42
  • Add the "really loooong" message to your question as an update so that we can dig into it. I haven't used message boxes this way in a while - can't remember whether you can simply copy & paste the text displayed (vs. taking a screenshot and adding the output to your question as an image); but you could always output the lengthy string to the debug console or log it to make copying the text possible if you can't copy it from the message box. – J0e3gan May 12 '13 at 02:48
  • I would +1 again for tinypic.com alone if I could. I haven't used that site before and am glad that you made me aware of it. – J0e3gan May 12 '13 at 03:06