System.InvalidCastException: ‘Conversion from string “employee” to type ‘Boolean’ is not valid.’

The form I created pops up but when I click on the open button this error occurs. The error directs me to sqlConn.ConnectionString , So any insights with regards to the problem is really helpful.

     Private Sub updateTable()

    sqlConn.ConnectionString = "server=" + server + ";" + "user id =" = username + ";" _
    + "password= " + password + ";" + "database" = database

    sqlConn.Open()

        sqlCmd.CommandText = "SELECT * From employee.employee"

        sqlRd = sqlCmd.ExecuteReader
        sqlDt.Load(sqlRd)
        sqlRd.Close()
        sqlConn.Close()
        DataGridView1.DataSource = sqlDt

End Sub

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You just didn’t actually read your code properly, seeing what you expected to see rather than what’s actually there. This:

sqlConn.ConnectionString = "server=" + server + ";" + "user id =" = username + ";" _
+ "password= " + password + ";" + "database" = database

is wrong for multiple reasons. What you actually meant to write was this:

sqlConn.ConnectionString = "server=" + server + ";" + "user id =" + username + ";" _
+ "password= " + password + ";" + "database=" + database

Notice that there were three mistakes in your code. You need to be much more careful and especially when you know specifically that that is the line in error.

That said, there are multiple ways that that code could be improved. Firstly, if you want to concatenate Strings then use the string concatenation operator, not the addition operator. Also, NEVER concatenate two literals:

sqlConn.ConnectionString = "server=" & server & ";user id=" & username & ";password=" & password & ";database=" & database

The thing is, the reason you made this mistake in the first place is that concatenating lots of substrings that way makes your code hard to read and, therefore, error-prone. I would generally only use one concatenation operator or maybe two at the most. If you would need any more, use a different option. From .NET 1.0 to fairly recently, that option would be String.Format:

sqlConn.ConnectionString = String.Format("server={0};user id={1};password={2};database={3}",
                                         server,
                                         username,
                                         password,
                                         database)

Much more readable and therefore not prone to the sort of errors you made. In more recent versions, you also have the option of string interpolation:

sqlConn.ConnectionString = $"server={server};user id={username};password={password};database={database}"

You shouldn’t use any of those options though. As you’re building a connection string, use the appropriate connection string builder. All ADO.NET providers have such a class and Connector/Net is no exception. I’m not sure what the specific properties would be called but it would look something like this:

Dim builder As New MySqlConnectionStringBuilder With {.Server = server,
                                                      .UserID = username,
                                                      .Password = password,
                                                      .Database = database}

sqlConn.ConnectionString = builder.ConnectionString

That is less error-prone still.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x