efaisalzia
efaisalzia

Reputation: 45

How can I resolve in my project Format of the initialization string does not conform to specification starting at index 17'

Intialize connection

System.ArgumentException:'Format of the initialization string does not conform to specification starting at index 17'

The above error message shows on this line

    private void InitializeConnection()
    {
        server = "localhost";
        database = "table";
        uid = "root";
        password = "";
        string connectionString = "SERVER=" + server + ";" + "DATABASE" + database + ";" + "UID" + uid + ";" + "PASSWORD" + password + ";";
        ObjConnection = new MySqlConnection(connectionString); // error happens here
    }

enter image description here

Upvotes: 0

Views: 165

Answers (2)

Bradley Grainger
Bradley Grainger

Reputation: 28162

Use the MySqlConnectionStringBuilder class to format the connection string for you. It will handle concatenating the options and escaping special characters correctly.

private void InitializeConnection()
{
    var builder = new MySqlConnectionStringBuilder
    {
        Server = "localhost",
        Database = "table",
        UserID = "root",
        Password = "",
    }

    string connectionString = builder.ConnectionString;
    ObjConnection = new MySqlConnection(connectionString);

    // OR: ObjConnection = new MySqlConnection(builder.ConnectionString);
}

Upvotes: 0

User12345
User12345

Reputation: 5480

In your code near DATABASE should be like DATABASE=, same way for UID and PASSWORD. All you were missing way = near these variables

private void InitializeConnection()
{
    server = "localhost";
    database = "table";
    uid = "root";
    password = "";
    string connectionString = "SERVER=" + server + ";" + "DATABASE=" + database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
    ObjConnection = new MySqlConnection(connectionString);
}

Upvotes: 0

Related Questions