1
I’m starting in the c# language and I’m having a problem crossing parameters between layers. In my program I made 3 layers, the Dal, Model and the interface. When I try to compare a return value of the Dal layer on the interface layer, I can’t, but if I copy the dal layer code and play on the interface, it works normally. Follows the codes:
ps. This code is a test for a separate project, but I am testing in parts.
Interface layer:
private void Button1_Click(object sender, EventArgs e)
{
Dal da = new Dal();
Modelo mo = new Modelo();
int s = 1;
int r = 1;
mo.idade = textBox1.Text;
mo.sexo = textBox2.Text;
if (da.Verificar(r) == 1 && da.Verificar2(s) == 1)
{
MessageBox.Show("Ja possui um registro no sistema!");
}
else
{
MessageBox.Show("Não possui nenhum registro no sistema");
}
}
Layer Model:
private string nIdade;
private string nSexo;
public string idade
{
get { return nIdade; }
set { nIdade = value; }
}
public string sexo
{
get { return nSexo; }
set { nSexo = value; }
}
Dal layer:
private static string caminho = "server=localhost;port=3306;database=cadastro;User Id=root;password=";
private MySqlConnection conexao = new MySqlConnection(caminho);
private Modelo mo = new Modelo();
public int Verificar(int r)
{
//Comando de verificação de registro.
string verifica = "select * from pessoas where idade = '"+ mo.idade+"'";
MySqlCommand verificar = new MySqlCommand(verifica, conexao);
conexao.Open();
MySqlDataReader dr = verificar.ExecuteReader();
if (dr.HasRows)
{
r = 1;
}
else
{
r = 0;
}
conexao.Close();
return r;
}
public int Verificar2(int s)
{
//Comando de verificação de registro.
string verifica2 = "select * from pessoas where sexo = '" + mo.sexo + "'";
MySqlCommand verificar2 = new MySqlCommand(verifica2, conexao);
conexao.Open();
MySqlDataReader dr2 = verificar2.ExecuteReader();
if (dr2.HasRows)
{
s = 1;
}
else
{
s = 0;
}
conexao.Close();
return s;
}
What do you mean "I can’t"? It won’t be for lack of any
using
?– ramaral
The question is good, but comment on the code where it is giving error and show us which error messages. When asking take into account our perspective of the code, what for you is a part concatenated to a whole for us is just a fragment lacking more information.
– Augusto Vasques
your 'dal' does not work properly because you are passing sex and age in the model that is in the 'interface', in the 'dal' you are just instantiating model without passing any parameter
– Lucas Miranda