Insert using methods and classes in the C#database

Asked

Viewed 183 times

0

Hello, good afternoon, sir! My problem is the following: I am wanting to run an Insert event, which in the database will add the (name, user, sector, position, shift and extension), but now I am training to run this function in a method of a class rather than directly in the click event. Only I also created a Connection class to SELECT the data and send to the Repeater structure...
The case is: In the event the insert button I have

protected void btnInserir_Click(object sender, EventArgs e)
{
    InserirF insF = new InserirF();
    ConexaoF conF = new ConexaoF();

    conF.Nome = txtNome.Text;
    conF.Usuario = txtUsuario.Text;
    conF.Setor = txtSetor.Text;
    conF.Cargo = txtCargo.Text;
    conF.Turno = txtTurno.Text;
    conF.Ramal = txtRamal.Text;
    insF.Inserir();
}

Now see that in my Connection class I have the following

public class ConexaoF {


string id;
string nome; string usuario; string setor; string cargo; string turno; string ramal;

public string ID
{
    get { return id; }
    set { id = value; }
}
public string Nome
{
    get { return nome; }
    set { nome = value; }
}
public string Usuario
{
    get { return usuario; }
    set { usuario = value; }
}
public string Setor
{
    get { return setor; }
    set { setor = value; }
}
public string Cargo
{
    get { return cargo; }
    set { cargo = value; }
}
public string Turno
{
    get { return turno; }
    set { turno = value; }
}
public string Ramal
{
    get { return ramal; }
    set { ramal = value; }
}

SqlConnection sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["Conexao"].ConnectionString);

public List<ConexaoF> listar()
{
    List<ConexaoF> listaresultado = new List<ConexaoF>();

    try
    {
        SqlCommand sqlSeleciona = new SqlCommand("SELECT id, nome, usuario, setor, cargo, turno, ramal FROM spc_funcionario_aprendiz ORDER BY id ASC", sqlConnection);

        sqlConnection.Open();

        SqlDataReader dr = sqlSeleciona.ExecuteReader();

        while (dr.Read())
        {
            ConexaoF conn = new ConexaoF();

            conn.id = dr["id"].ToString();
            conn.nome = dr["nome"].ToString();
            conn.usuario = dr["usuario"].ToString();
            conn.setor = dr["setor"].ToString();
            conn.cargo = dr["cargo"].ToString();
            conn.turno = dr["turno"].ToString();
            conn.ramal = dr["ramal"].ToString();

            listaresultado.Add(conn);
        }
    }
    finally
    {
        sqlConnection.Close();
    }
    return listaresultado;
}}

Ignore the keys because I had trouble entering the code.
Anyway, here in the Connected class I have the data of Name, User, Sector, Position, Shift and Extension, which respectively I will use both in the event of click Insert as much as in the other class Inserirf that comes the method that makes the Insert. Follows the code of the Insertf class

public class InserirF {


SqlConnection sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["Conexao"].ConnectionString);

public void Inserir()
{
    ConexaoF conF = new ConexaoF();
    try
    {
        SqlCommand sqlInserir = new SqlCommand("INSERT INTO spc_funcionario_aprendiz (nome, usuario, setor, cargo, turno, ramal) VALUES ('@nome', '@usuario', '@setor', '@cargo', '@turno', '@ramal')", sqlConnection);
        sqlInserir.Parameters.Add("nome", SqlDbType.VarChar).Value = conF.Nome.ToString();
        sqlInserir.Parameters.Add("usuario", SqlDbType.VarChar).Value = conF.Usuario.ToString();
        sqlInserir.Parameters.Add("setor", SqlDbType.VarChar).Value = conF.Setor.ToString();
        sqlInserir.Parameters.Add("cargo", SqlDbType.VarChar).Value = conF.Cargo.ToString();
        sqlInserir.Parameters.Add("turno", SqlDbType.VarChar).Value = conF.Turno.ToString();
        sqlInserir.Parameters.Add("ramal", SqlDbType.Int).Value = conF.Ramal.ToString();

        sqlConnection.Open();
        sqlInserir.ExecuteNonQuery();
        sqlConnection.Close();
    }
    finally
    {
        sqlConnection.Close();
    }
}}

Only, note that by clicking the button, the events of
conF.Nome = txtNome.Text; conF.Usuario = txtUsuario.Text;
And then, they receive the value that is in the Textbox, but after that, when the method enters insF.Insert() He ends up "restarting" all the information of Conexaof conf, as he ends up creating another at the beginning of the method.. And so respectively leaving the data null, giving error in INSERT. How to save these values so I can use them in Sqlcommand INSERT?

  • It would be good to reduce everything to a [mcve] problem, so that the question serves a wide audience. Studying the post available on this link can make a very positive difference in your use of the site: Stack Overflow Survival Guide in English

1 answer

-1


//It is generating new values because you are giving new Connection in --> //"Conexaof conf = new Conexaof();"

protected void btnInserir_Click(Object Sender, Eventargs and){

InserirF insF = new InserirF();
ConexaoF conF = new ConexaoF();

conF.Nome = txtNome.Text;
conF.Usuario = txtUsuario.Text;
conF.Setor = txtSetor.Text;
conF.Cargo = txtCargo.Text;
conF.Turno = txtTurno.Text;
conF.Ramal = txtRamal.Text;

// Pass the conf by reference

insF.Inserir(conf);

}

//and here, you get this reference. (EXCEPT FOR THE CONNECTF conf = new Connectf())

public void Insert(Conexaof conf) {

try
{
    SqlCommand sqlInserir = new SqlCommand("INSERT INTO spc_funcionario_aprendiz (nome, usuario, setor, cargo, turno, ramal) VALUES ('@nome', '@usuario', '@setor', '@cargo', '@turno', '@ramal')", sqlConnection);
    sqlInserir.Parameters.Add("nome", SqlDbType.VarChar).Value = conF.Nome.ToString();
    sqlInserir.Parameters.Add("usuario", SqlDbType.VarChar).Value = conF.Usuario.ToString();
    sqlInserir.Parameters.Add("setor", SqlDbType.VarChar).Value = conF.Setor.ToString();
    sqlInserir.Parameters.Add("cargo", SqlDbType.VarChar).Value = conF.Cargo.ToString();
    sqlInserir.Parameters.Add("turno", SqlDbType.VarChar).Value = conF.Turno.ToString();
    sqlInserir.Parameters.Add("ramal", SqlDbType.Int).Value = conF.Ramal.ToString();

    sqlConnection.Open();
    sqlInserir.ExecuteNonQuery();
    sqlConnection.Close();
}
finally
{
    sqlConnection.Close();
}

}

  • Opa, gave it right! Thank you very much, helped a lot.. It was simpler than I expected

  • Oops, for nothing :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.