0

I have a C# application with MYSQL database. I was work without any problem but suddenly a problem appear:

A first chance exception of type 'MySql.Data.MySqlClient.MySqlException' occurred in MySql.Data.dll

I didn't change any thing in my program !!, also i checked the connection from PHP page and it was work without problem, the problem just appear in my c# application. In catch part, i changed it to: ee.HResult instead of "ee.message" and i got this number: -2147467259 .. !! Here the code:

public static string conString = "";
    public MySqlConnection objConn;
    string server = "www.******.com";
    string database = "******";
    string uid = "******";
    string password = "******";
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        try
        {
            conString = "SERVER=" + server + ";" + "DATABASE=" + database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + "; CharSet=utf8;";
            objConn = new MySqlConnection(conString);
            objConn.Open();
            MySqlCommand cmd = new MySqlCommand("select * from sett", objConn);
            MySqlDataReader dataReader = cmd.ExecuteReader();
            dataReader.Read();
            int x = int.Parse(dataReader.GetDecimal(0).ToString());
            MessageBox.Show(x.ToString());
        }
        catch (Exception ee)
        {
            MessageBox.Show(ee.Message);
        }
    }
3
  • have you considered refactoring the code and not using dataReader.GetDecimal(0).ToString() what if the field is not a decimal in position 0 so why not change it to be the actual database field name like this (string)dataReader["yourfieldname"] try stepping thru the code also Commented Feb 19, 2015 at 16:00
  • The problem not in code, i'm sure..because i didn't change any thing ! also, in other tried in another old application, that connected to the same database, Same problem. It appear in: objConn.Open(); Commented Feb 19, 2015 at 16:06
  • I think that you are missing my point.. it's not what you changed..but what if the structure of the table changed.. that is why it's better in my opinion to assign values from datareader by accessing it by it's field name not it's indexed position.. sounds like you are experiencing a datatype conversion issue.. try changing it and debugging it an see if that makes a difference.. what will changing it hurt ... Commented Feb 19, 2015 at 16:15

1 Answer 1

1

The problem is because the "Remote MySQL" in the domain. I added % mark to allow all remote connection to the database.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.