List by DNI - Asp.net mvc 4 db Access

1

I am programming in ncapas, asp.net mvc 4 bd Access and what I need is to list a data by your ID that I sent to you. If you arrive but do not filter. I still list all the info. This is my code.

public List<TAsistenciaDTO> ObtenerAsistenciaxDNI(string dni)
        {
            List<TAsistenciaDTO> asistenciaList = new List<TAsistenciaDTO>();

            DataSet ds = new DataSet();

            using (OleDbConnection conn = ConexionDAL.ConexionACCESS())
            {
                var commandText = "Select * from TAsistencia";

                OleDbCommand comando = new OleDbCommand(commandText, conn);
                comando.Parameters.Add("@PerMarcacion", OleDbType.VarChar).Value = dni;

                OleDbDataAdapter da = new OleDbDataAdapter(comando);
                da.Fill(ds);
                conn.Close();
                DataTable dt = ds.Tables[0];
                foreach (DataRow rows in dt.Rows)
                {
                    TAsistenciaDTO product = new TAsistenciaDTO();
                    product.AsiFec = Convert.ToDateTime(rows["AsiFec"].ToString());
                    product.PerMarcacion = rows["PerMarcacion"].ToString();
                    asistenciaList.Add(product);
                }
                return asistenciaList;
            }
        }

What I do not understand well is this line of code comando.Parameters.Add("@PerMarcacion", OleDbType.VarChar).Value = dni; .

I also did it by nesting the bone value: var commandText = "Select * from TAsistencia where PerMarcacion='"+ dni +"'"; but I get the following error The data types do not match in the expression of criteria

    
asked by Luis Vega 19.01.2018 в 00:08
source

1 answer

3

OK ... there are two problems at a glance ...

or the query is missing the where clause (which is what you see at this moment) or when you arm the query with the parameter, the DNI is not a string (probably a number) ...

var commandText = "Select * from TAsistencia WHERE PerMarcacion = ?";
OleDbCommand comando = new OleDbCommand(commandText, conn);
comando.Parameters.Add("@PerMarcacion", OleDbType.VarChar).Value = dni;

The parameter add will have to revise it. It sure is not a varchar.

Otherwise, it eliminates passing the parameter in that way (I do not recommend it) and concatenates it in another way

var commandText = "Select * from TAsistencia WHERE PerMarcacion = " + dni;
    
answered by 19.01.2018 / 00:17
source