Saving information in the same database attribute using two textbox

Asked

Viewed 53 times

0

I was wondering if there’s any way I could save two different pieces of information into the same attribute in the database. Like I have two textbox, one Cpf and one cnpj. I wanted to save Cpf and cnpj together. Only I created two textbox, one for each one.

Model code

public int idClientefisico { get; set; } 
public string nome { get; set; } 
public string rg { get; set; } 
public string cpf { get; set; } 
public string email { get; set; } 
public string endereco { get; set; } 
public string nr { get; set; } 
public string cep { get; set; } 
public string estado { get; set; } 
public string telefone { get; set; } 
public string cidade { get; set; } 

public void Insert(Model.cliente_fisico clientefisico) 
{ 
    SqlConnection conexao = new SqlConnection(strCon);
    string sql = @"Insert into cliente_fisico values 
                  (@nome,@rg,@cpf,@email,@endereco,@nr,@cep,@estado,@telefone,@cidade)";

    SqlCommand cmd = new SqlCommand(sql, conexao);     
    cmd.Parameters.AddWithValue("@nome", clientefisico.nome); 
    cmd.Parameters.AddWithValue("@rg", clientefisico.rg); 
    cmd.Parameters.AddWithValue("@cpf", clientefisico.cpf); 
    cmd.Parameters.AddWithValue("@email", clientefisico.email); 
    cmd.Parameters.AddWithValue("@endereco", clientefisico.endereco); 
    cmd.Parameters.AddWithValue("@nr", clientefisico.nr); 
    cmd.Parameters.AddWithValue("@cep", clientefisico.cep); 
    cmd.Parameters.AddWithValue("@estado", clientefisico.estado); 
    cmd.Parameters.AddWithValue("@telefone", clientefisico.telefone); 
    cmd.Parameters.AddWithValue("@cidade", clientefisico.cidade); 

    conexao.Open(); 

    try 
    { 
         cmd.ExecuteNonQuery(); 
    }
    catch
    {
        Console.WriteLine("Deu erro na inserção de Cliente do tipo fisico ... ");
    }
    finally
    {
        conexao.Close();
    }
}
  • I couldn’t understand anything, really. You can [Dit] your publication and try to make it clearer.

  • became clearer?

  • There’s a way to do that. That’s what I wanted to know?

  • Yes, I would like to know the way it is because then I would create another table to store only two different types of data.

1 answer

0

Yes, you can do this with simple techniques. See this example using only a Viewmodel class:

public class CadastroViewModel
{
    // outras propriedades
    public string CPF { get; set; }
    public string CNPJ { get; set; }
    public string Documento
    {
        get { return CPF ?? CNPJ; }
        set 
        { 
            if (ID.Length < 14)
                CPF = Documento;
            else
                CNPJ = Documento;
        }
    }
}

Thus, you assign values of Textboxes in CPF and CNPJ, but always save and recover from the database the Document property.

  • My code is just above, I couldn’t put it here.

  • Cool, now you added. Just vc replace the fields cpf and cnpj on your table for just one call documento. And save the property Documento of my example.

  • @Tmbruhth You should not put your code in the publication of Thiago. You need to keep it in the body of your question.

  • In case that ID would be the document my friend?

  • @Tmbruhth, yes, I’ve edited the example, it should be clearer now.

  • the code of my save button, then it will look like this cliente_fisico.document = txtCPF.Text; cliente_fisico.document = txtCNPJ.Text;

  • @Tmbruhth, no, the opposite. It’s in my answer: "you assign values of Textboxes in CPF and CNPJ, but always save and recover from the database the property Document."

Show 2 more comments

Browser other questions tagged

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