Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I am working on pos system and all things are working fine even I have added MySQL workbench database in visual studio in server explorer but when I try to connect my software with database connection so am getting error The system failed to establish a connection please help see my code and please point out me where I am wrong and what is the best way to resolve this problem thanks

What I have tried:

C#
private string TstServerMySQL;
    private string TstPortMySQL;
    private string TstUserNameMySQL;
    private string TstPwdMySQL;
    private string TstDBNameMySQL;

    private void cmdTest_Click(object sender, EventArgs e)
    {
        //Test database connection

        TstServerMySQL = txtServerHost.Text;
        TstPortMySQL = txtPort.Text;
        TstUserNameMySQL = txtUserName.Text;
        TstPwdMySQL = txtPassword.Text;
        TstDBNameMySQL = txtDatabase.Text;

        try
        {
            SQLConn.conn.ConnectionString = "Server = '" + TstServerMySQL + "';  " + "Port = '" + TstPortMySQL + "';" + "user id = '" + TstUserNameMySQL + "';   " + "password = '" + TstPwdMySQL + "'; " + "Database = '" + TstDBNameMySQL + "'";
            SQLConn.conn.Open();
            Interaction.MsgBox("Test connection successful", MsgBoxStyle.Information, "Database Settings");

        }
        catch
        { 
            Interaction.MsgBox("The system failed to establish a connection", MsgBoxStyle.Information, "Database Settings");
        }
        SQLConn.DisconnMy();
    }
Posted
Updated 14-Jul-19 9:27am
v2

Quote:
please help see my code and please point out me where I am wrong and what is the best way to resolve this problem thanks

What kind of help do you expect ?
The connection string depend on user input, how can we guess what is the connection string ?

Already 128 question and you still don't know how to make a question answerable.
 
Share this answer
 
That code you wrote to assemble a connection string is absolute garbage.

Besides not doing any input validation at all, you're using string concatenation to build the connection string, which makes your code pretty much unreadable and a pain in the ass to debug. Use string interpolation instead and it gets much easier. (You also don't need all the damn spaces and the single quotes!)
C#
string connStr = $"Server={TstServerMySQL};Port={TstPortMySQL};Usser Id={TstUserNameMySQL};Password={TstPwdMySQL};Database={TstDBNameMySQL}";
SQLConn.conn.ConnectionString = connStr;

Also, breaking the building of the connection string out into it's own variable makes it easier to see the content of the connection string in the debugger before assigning it to the connection object.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900