Insert records with checkbox C #

1

I am registering registrations in C # ASP.NET with SQL Server, the problem is that I do not know how to declare the checkboxes so that when one or more are selected they are registered in my table. This is what I have: The table sex and age are checkboxes.

 SqlConnection conn = new SqlConnection(@"Data Source=(local);Initial   Catalog=Sistemaa;User ID=sa;Password=344");
 SqlCommand cmd=new SqlCommand("insert into Empleados(nombre, trabajo, sexo, edad)values ("+this.txtnombre.text+",'"+this.txttrabajo.text+....(AQUI ES DÓNDE NO SÉ CÓMO AGREGAR LOS CHECKBOX))

This is my table

    
asked by KlonDTS 24.01.2017 в 19:31
source

3 answers

2

Complementing the response of Leandro would put the following validation:

string connstring = @"Data Source=(local);Initial   Catalog=Sistemaa;User ID=sa;Password=344";
using (SqlConnection conn = new SqlConnection(connstring)) 
{

    string sql = @"insert into Empleados (nombre, trabajo, sexo, edad) values (@nombre, @trabajo, @sexo, @edad)"; 

    SqlCommand cmd = new SqlCommand(sql, conn); 
    cmd.Parameters.AddWithValue("@nombre", txtnombre.text); 
    cmd.Parameters.AddWithValue("@trabajo", txttrabajo.text);
    cmd.Parameters.AddWithValue("@sexo", checkboxSexoMasculino.checked ? "M" : "F");
    cmd.Parameters.AddWithValue("@edad", textEdad.text);
    cmd.ExecuteNonQuery(); 

}

This means that checkboxSexoMasculino.checked will be evaluated, if the result is true will become "M" , if the result is false will be "F" , basically it is a IF on a single line.

    
answered by 24.01.2017 / 19:51
source
1

Let's start from the base that you should not concatenate the values in a string to assign the parameters, but you should pass them using Parameters of the command object

string connstring = @"Data Source=(local);Initial   Catalog=Sistemaa;User ID=sa;Password=344";
using (SqlConnection conn = new SqlConnection("connection string")) 
{

    string sql = @"insert into Empleados (nombre, trabajo, sexo, edad) values (@nombre, @trabajo, @sexo, @edad)"; 

    SqlCommand cmd = new SqlCommand(sql, conn); 
    cmd.Parameters.AddWithValue("@nombre", txtnombre.text); 
    cmd.Parameters.AddWithValue("@trabajo", txttrabajo.text);
    //resto parametros

    cmd.ExecuteNonQuery(); 

}

If the checks are values that you need to insert you can define fields of type 'bit' then assign the parameters

cmd.Parameters.AddWithValue("@parambit", checkbox1.checked);
    
answered by 24.01.2017 в 19:40
0

I did it how you told me @ - Leandro Tuttini I have another question, are several check as I mentioned, I have to add them all in the parameters? since in my table I only have one column so that the selected checks are up there, I do not know if you explain to me

        SqlCommand cmd = new SqlCommand("insert into Empleados(nombre,trabajo,sexo,edad,)values(@nombre,@trabajo,@sexo,@edad)", conn);
        cmd.Parameters.AddWithValue("@nombre", txtnombre.Text);
        cmd.Parameters.AddWihValue("@trabajo", txttrabajo.Text);
        cmd.Parameters.AddWithValue("@sexo",checkbox1....); //Aqui no me da la opción de checked
         cmd.Parameters.AddWithValue("@edad",checkbox2...);
    
answered by 24.01.2017 в 19:55