I am getting this error:
cannot implicitly convert type ‘string’ to ‘System.Data.SqlClient.Sqlconnection’
for this code:
SqlConnection con1 = ConfigurationManager.ConnectionStrings["connect"].ConnectionString;
How do I solve this problem? I am working with a Windows application.
This is what you need:
Note: this is better than the other answers because it includes another hint: use the using keyword to guarantee disposal of your connection object and therefore prevent connection pool problems. 🙂
The reason you were getting the error in the fist place is that you were trying to assign a string value (ConfigurationManager.ConnectionStrings[“connect”].ConnectionString) to a variable of type SqlConnection.
I suggest you learn more about variable typing, variable casting and type assignments in C#, it will make coding a much more pleasurable (less frustrating) experience.
Good Luck!