dev user
dev user

Reputation: 41

'SqlServerService' does not implement interface member (C#)

I am creating a system with C# and Windows Forms, and when I tried to implement an interface to generate the base model with the methods for any database that I implement in the system I get an error when trying to pass parameters to the child class of the interface.

I have the following interface:

It is responsible for generating the contract with all the classes that will inherit it.

public interface iDatabase
    {
        void createConnection();
        void CreateUser();
    }

and the class DbService:

which is responsible for containing the contract's fixation and allow me not to generate dependency on the project's dependencies, this way I can change the database at any time, I just need to send a new database class as parameter for it to work...

public class DbService
    {
    private iDatabase _database;

    public DbService(iDatabase databaseService)
    {
        this._database = databaseService;
    }
    
    public void connect()
    {
        this._database.createConnection();
    }
    
    public void insertUser()
    {
        this._database.CreateUser();
    }
}

and the dependency's class of SQL Server: this class is responsible for implementing SQL Server specific rules.

namespace TravelCompany.app.repository
{
    internal class SqlServerService : iDatabase
    {
        SqlConnection sqlCon = null;
        private string strCon = "Data Source=DESKTOP-UD6EQCL;Initial Catalog=immigration;Integrated Security=True";
        private string strSql = string.Empty;
        SqlDataAdapter adapt;
        SqlCommand command;

        public void createConnection()
        {
            try
            {
                sqlCon = new SqlConnection(strCon);
                sqlCon.Open();
                MessageBox.Show("Connection Open !");
            }
            catch (Exception e)
            {
                MessageBox.Show("Error to connect Database!", e.Message);
            }
        }

        public void CreateUser()
        {
            try
            {
                strSql = "INSERT INTO users (full_name, email, password, cpf, birthday, cep, street, neighborhood, city, uf, complement, passport_number, stack, xp, seniority) VALUES (@full_name, @email, @password, @cpf, @birthday, @cep, @street, @neighborhood, @city, @uf, @complement, @passport_number, @stack, @xp, @seniority)";
                command = new SqlCommand(strSql, sqlCon);
                command.Parameters.AddWithValue("@full_name", full_name);
                command.Parameters.AddWithValue("@email", email);
                command.Parameters.AddWithValue("@password", password);
                command.Parameters.AddWithValue("@cpf", cpf);
                command.Parameters.AddWithValue("@birthday", birthday);
                command.Parameters.AddWithValue("@cep", cep);
                command.Parameters.AddWithValue("@street", street);
                command.Parameters.AddWithValue("@neighborhood", neighborhood);
                command.Parameters.AddWithValue("@city", city);
                command.Parameters.AddWithValue("@uf", uf);
                command.Parameters.AddWithValue("@complement", complement);
                command.Parameters.AddWithValue("@passport_number", passport_number);
                command.Parameters.AddWithValue("@stack", stack);
                command.Parameters.AddWithValue("@xp", xp);
                command.Parameters.AddWithValue("@seniority", seniority);
                command.ExecuteNonQuery();
                MessageBox.Show("User created successfully!");
            }
            catch (Exception e)
            {
                MessageBox.Show("Error to create user!", e.Message);
            }

            throw new NotImplementedException();
        }

    }
} 


and every time that i try to pass values to the public void CreateUser() of public class SqlServerService i receive an error message:

ERROR: 'iDatabase.CreateUser']1 'SqlServerService' does not implement interface member

The method databaseService.connect(); is working!

the problem is with the 'create user' function of SqlServerService...

but the error appears only when I try to pass parameters to the SqlServerService class methods, and I don't know why this happens because I am implementing the interface method with the same name, can someone help?

I would like to solve this error problem and be able to use the create user function of the SqlServerService class with the implementation of the interface

Upvotes: 0

Views: 41

Answers (1)

dev user
dev user

Reputation: 41

solved the problem, I basically had to pass the same parameters to all the other methods, so the code looks like this:

class SqlServerService

namespace TravelCompany.app.repository
{
    internal class SqlServerService : iDatabase
    {
        SqlConnection sqlCon = null;
        private string strCon = "Data Source=DESKTOP-UD6EQCL;Initial Catalog=immigration;Integrated Security=True";
        private string strSql = string.Empty;
        SqlDataAdapter adapt;
        SqlCommand command;

        public void createConnection()
        {
            try
            {
                sqlCon = new SqlConnection(strCon);
                sqlCon.Open();
                MessageBox.Show("Connection Open !");
            }
            catch (Exception e)
            {
                MessageBox.Show("Error to connect Database!", e.Message);
            }
        }

        public void CreateUser(string userName, string userBirthday, string userCpf, string userCep, string userStreet, string userNeighborhood, string userCity, string userUf, string  userComplement, string userEmail, string userPassport, string stack, string xp, string seniority)
        {
            MessageBox.Show("User created!");
            //try
            //{
            //    strSql = "INSERT INTO users (full_name, email, password, cpf, birthday, cep, street, neighborhood, city, uf, complement, passport_number, stack, xp, seniority) VALUES (@full_name, @email, @password, @cpf, @birthday, @cep, @street, @neighborhood, @city, @uf, @complement, @passport_number, @stack, @xp, @seniority)";
            //    command = new SqlCommand(strSql, sqlCon);
            //     command.Parameters.AddWithValue("@full_name", userName);
            //     command.Parameters.AddWithValue("@email", userEmail);
            //     command.Parameters.AddWithValue("@password", userPassword);
            //     command.Parameters.AddWithValue("@cpf", userCpf);
            //     command.Parameters.AddWithValue("@birthday", userBirthday);
            //     command.Parameters.AddWithValue("@cep", userCep);
            //     command.Parameters.AddWithValue("@street", userStreet);
            //     command.Parameters.AddWithValue("@neighborhood", userNeighborhood);
            //     command.Parameters.AddWithValue("@city", userCity);
            //     command.Parameters.AddWithValue("@uf", userUf);
            //     command.Parameters.AddWithValue("@complement", userComplement);
            //     command.Parameters.AddWithValue("@passport_number", userPassport);
            //     command.Parameters.AddWithValue("@stack", userStack);
            //     command.Parameters.AddWithValue("@xp", userXp);
            //     command.Parameters.AddWithValue("@seniority", userSeniority);
            //     command.ExecuteNonQuery();
            //     MessageBox.Show("User created successfully!");
            // }
            // catch (Exception e)
            // {
            //    MessageBox.Show("Error to create user!", e.Message);
            // }

            //throw new NotImplementedException();
        }

    }
}

interface:

  public interface iDatabase
        {
            void createConnection();
            void CreateUser(string userName, string userBirthday, string userCpf, string userCep, string userStreet, string userNeighborhood, string userCity, string userUf, string userComplement, string userEmail, string userPassport, string stack, string xp,

 string seniority);
    }

and the class DbService:


 public void insertUser(string userName, string userBirthday, string userCpf, string userCep, string userStreet, string userNeighborhood, string userCity, string userUf, string userComplement, string userEmail, string userPassport, string stack, string xp, string seniority)
        {
            this._database.CreateUser(userName, userBirthday, userCpf, userCep, userStreet, userNeighborhood, userCity, userUf, userComplement, userEmail, userPassport, stack, xp, seniority);
        }

For a reason that i dont know, i have to pass the parameters to all the classes.

Upvotes: 1

Related Questions