Search code examples
c#executereader

Cannot implicity convert type error


Trying to fill Combobox with data from a database and I'm using ExecuteReader as below, but when I try to instantiate my connection it shows me the error

Cannot implicitly convert type error

My DBConnect class code:

public class DBConnect
{
        private SqlConnection connection;
        private string servername = "10.1.76.109,1433";
        private string database = "EngLib";
        private string dbuser;
        private string userpassword;


        public DBConnect()
        {

        }

        public void doDBConnect(string dbuserform, string userpasswordform)
        {
            dbuser = dbuserform;
            userpassword = userpasswordform;
        }


        public void Initialize()
        {

        }

        public bool openConnection()
        {
            string connectionString;
            connectionString = "Server=" + servername + ";Database=" + database + ";user id=" + dbuser + ";Password=" + userpassword;
            Console.WriteLine(connectionString);
            connection = new SqlConnection(connectionString);
            try
            {
                connection.Open();
                return true;
            }
            catch (SqlException ex)
            {
                switch (ex.Number)
                {
                    case 0:
                        MessageBox.Show("Não é possível contactar o servidor. Entre em contato com o administrador");
                        break;

                    case 18456:
                        MessageBox.Show("Usuário/Senha inválidos, tente novamente");
                        break;
                }
                return false;
            }

}

My form code:

{
    public Form2()
    {
        InitializeComponent();
    }

    public void Form2_Load(object sender, EventArgs e)
    {
        SqlDataReader rdr = null;
        DBConnect sqlConnection;
        sqlConnection = new DBConnect();
        sqlConnection.doDBConnect(dbuserform: "usertest", userpasswordform: "usertest");
         {
            try
            {

                {
                    sqlConnection.openConnection();
                    SqlCommand sqlCmd;
                    sqlCmd = new SqlCommand("SELECT Material FROM EngLib");
                    sqlCmd.Connection = sqlConnection;
                    SqlDataReader sqlReader = sqlCmd.ExecuteReader();

                    while (sqlReader.Read())
                    {
                        comboBox1.Items.Add(sqlReader["Material"].ToString());
                    }

                    sqlReader.Close();
                }
            }
            finally
            {
                // close the reader
                if (rdr != null)
                {
                    rdr.Close();
                }
            }
        }
    }
}

The user and password in the code is just temporary.


Solution

  • I think the problem is here:

    DBConnect sqlConnection;
    SqlCommand sqlCmd;
    sqlCmd.Connection = sqlConnection;
    

    SqlCommand expects that Connection is of type SqlConnection, but you are assigning your own class DBConnect.

    Possible fixes:

    Expose the connection property on DBConnect and use it.

    Inherit DBConnect from SqlConnection.

    Use SqlConnection directly.

    An additional note: you are not disposing the SqlConnection inside your DBConnect class, which can lead to StackOverflowException. You should implement IDisposable.