0

I've found many suggestions to fix this unprocessed exception, and I have tried every one I could find (and understand). It's been hours, but still no results... I've created a windows forms application that asks for a client's information and I'm trying to Insert this info into a DB I have created in MySQL. This is the code that seems to be working for everyone but me:

public partial class Form1 : Form
{

    private MySqlConnection mConn;
    public Form1()
    {
        InitializeComponent();
        mConn = new MySqlConnection("Persist Security Info=False; server=localhost; database=xxxxxxx;uid=yyyyyyy;password=zzzzzzzz");
    }

    private void button1_Click(object sender, EventArgs e)
    {
        mConn.Open();
        if (mConn.State == ConnectionState.Open)
        {
            MySqlCommand command = new MySqlCommand("INSERT INTO client VALUES (" + textBox1.Text + ",' " + textBox2.Text + "', '" + textBox3.Text + "'," + textBox4.Text + ",' " + textBox5.Text + "')", mConn);
            command.ExecuteNonQuery();
            mConn.Close();
        }
        else
            MessageBox.Show("Connection Failed!");
    }
}

This is the exception text:

MySql.Data.MySqlClient.MySqlException (0x80004005): Unable to connect to any of the specified MySQL hosts. ---> System.Net.Sockets.SocketException (0x80004005): The requested adress is not valid in this context [::1]:3306 in ...

If you can help me I'll be very grateful. It's my first year programming and so far it has passed quite well, but now I have been "left behind" in my class because of this recurring error. Thank you in advance :)

r_laezza
  • 119
  • 1
  • 2
  • 14
  • 2
    You have a SQL injection vulnerability. – SLaks May 03 '13 at 18:17
  • Are you able to bring up MySQL through the command line utility? If not, follow the error messages you get there. It could be as simple as the MySQL service not running (if you're on Windows). If you can connect through the command line, check out [this StackOverflow posting](http://stackoverflow.com/questions/4655669/unable-to-connect-to-any-of-the-specified-mysql-hosts), which seems similar to your problem. Especially note the comment below the accepted solution that shows how to determine which port your MySQL is listening on. – Ed Gibbs May 03 '13 at 18:19
  • Yes I can bring up MySQL through the command line utility. And I already tried to include the port number into the connection string, in fact I tried many different connection strings. Through the MySQL command line I was able to confirm the port number and I verified that an exception was created in my firewall for that port. – r_laezza May 09 '13 at 12:59
  • How can I fix a SQL injection vulnerability? – r_laezza May 09 '13 at 13:01

2 Answers2

4

My Professor was able to find the problem. For some reason my computer can't recognize the term localhost as the IP address 127.0.0.1, so when I was writing server = localhost in the connection string, an error occurred. When I tried writing server = 127.0.0.1 everything worked smoothly. I hope this is helpful.

Working connection string:

"Persist Security Info=False;server=127.0.0.1;database=xx;uid=yy;password=zz"
r_laezza
  • 119
  • 1
  • 2
  • 14
0

The Problem with establishing connection to the remote site will be solved by providing public ip of that db instead of private one.

it will help u. Try it.

kunal
  • 11
  • 1