简体   繁体   中英

Fill Datagrid with Sql Query using Date " 'Operand type clash: date is incompatible with int''

I am new to Programming and I started with C# (Visual Studio 2017 CE);
I am writing an application and using a DataGrid . I am trying to fill the DataGrid using a query to a C# service based DB (mdf file).
When I run the app and try the query statement I get this error:

Operand type clash: date is incompatible with int

at the SqlDataReader Line.
I have tested the SQL Select statement in the SQL Server and it works there. I have read multiple questions related to the error, but since I am a rookie to programming almost all answers are difficult to understand, Thanks in advance for your understanding

using (SqlConnection conn = Conexion.Conectado())
{
    string strsql = "SELECT dbo.Personas.Nombres, dbo.Personas.Apellidos, dbo.Prestamo.prestamo_id, dbo.Prestamo.fecha, dbo.Prestamo.Monto_prestamo, dbo.Prestamo.Ruta, dbo.Prestamo.Quotas, dbo.Prestamo.Balance, dbo.Registro_pagos.Monto_pago, dbo.Registro_pagos.Mora FROM dbo.Personas INNER JOIN dbo.Prestamo ON dbo.Personas.Persona_id = dbo.Prestamo.fk_Persona_id INNER JOIN dbo.Registro_pagos ON dbo.Prestamo.prestamo_id = dbo.Registro_pagos.fk_prestamo_id where dbo.Registro_pagos.fecha_pago = " + Dtp_fecha_cuadre.Text;


    SqlCommand cmd = new SqlCommand(strsql, conn);
    cmd.CommandType = CommandType.Text;
    SqlDataReader dr = cmd.ExecuteReader();
    while (dr.Read())
    {
        string Nombres = dr["Nombres"].ToString();
        string Apellidos = dr["Apellidos"].ToString();
        string num_prestamo = dr["prestamo_id"].ToString();
        DateTime fecha = Convert.ToDateTime(dr["fecha"].ToString());
        double Monto_prestamo = Convert.ToDouble(dr["Monto_prestamo"].ToString());
        string Codigo_ruta = dr["Ruta"].ToString();
        string Quotas = dr["Quotas"].ToString();
        double Balance = Convert.ToDouble(dr["Balance"].ToString());
        double Monto_pago = Convert.ToDouble(dr["Monto_pago"].ToString());
        double Mora = Convert.ToDouble(dr["Mora"].ToString());

        Dgv_cuadre_rutas.Rows.Add(Nombres, Apellidos, num_prestamo, fecha,Monto_prestamo , Codigo_ruta, Quotas, Balance, Monto_pago, Mora);
    }
    conn.Close();
}

Uses SQL DataAdapter instead which is much easier and will get rid of error

            using (SqlConnection conn = Conexion.Conectado())
            {
                string strsql = "SELECT dbo.Personas.Nombres, dbo.Personas.Apellidos, dbo.Prestamo.prestamo_id, dbo.Prestamo.fecha, dbo.Prestamo.Monto_prestamo, dbo.Prestamo.Ruta, dbo.Prestamo.Quotas, dbo.Prestamo.Balance, dbo.Registro_pagos.Monto_pago, dbo.Registro_pagos.Mora FROM dbo.Personas INNER JOIN dbo.Prestamo ON dbo.Personas.Persona_id = dbo.Prestamo.fk_Persona_id INNER JOIN dbo.Registro_pagos ON dbo.Prestamo.prestamo_id = dbo.Registro_pagos.fk_prestamo_id where dbo.Registro_pagos.fecha_pago = " + Dtp_fecha_cuadre.Text;


                SqlCommand cmd = new SqlCommand(strsql, conn);
                cmd.CommandType = CommandType.Text;
                SqlDataAdapter adapter = new SqlDataAdapter(strsql, conn);

                DataTable dt = new DataTable();
                adapter.Fill(dt);

                Dgv_cuadre_rutas.DataSource = dt;

                conn.Close();
            }

I was able to fix the error! the error occurs first because in my original query I was using " = " + Dtp_fecha_cuadre.Text;" the equal sends the data in an int format, so I had to change it to "--like '" + Dtp_fecha_cuadre.Value.ToString() +"'";,-- but at this point it was not filling up the datagrid, then I came up with the idea that, date string being send was not in the correct format, and adjusted the query to "like '" + Dtp_fecha_cuadre.Value.ToString("yyyy-MM-dd") +"'"; and this solve my problem, thanks all that helped me here – engel 1 min ago edit

 using (SqlConnection conn = Conexion.Conectado())
        {

            string strsql = "SELECT dbo.Personas.Nombres, dbo.Personas.Apellidos, dbo.Prestamo.prestamo_id, dbo.Prestamo.fecha, dbo.Prestamo.Monto_prestamo, dbo.Prestamo.Ruta, dbo.Prestamo.Quotas, dbo.Prestamo.Balance, dbo.Registro_pagos.Monto_pago, dbo.Registro_pagos.Mora FROM dbo.Personas INNER JOIN dbo.Prestamo ON dbo.Personas.Persona_id = dbo.Prestamo.fk_Persona_id INNER JOIN dbo.Registro_pagos ON dbo.Prestamo.prestamo_id = dbo.Registro_pagos.fk_prestamo_id where dbo.Registro_pagos.fecha_pago like '" + Dtp_fecha_cuadre.Value.ToString("yyyy-MM-dd") +"'";


            SqlCommand cmd = new SqlCommand(strsql, conn);
            cmd.CommandType = CommandType.Text;
            SqlDataReader dr = cmd.ExecuteReader();

            while (dr.Read())
            {

                string Nombres = dr["Nombres"].ToString();
                string Apellidos = dr["Apellidos"].ToString();
                string num_prestamo = dr["prestamo_id"].ToString();
                DateTime fecha = Convert.ToDateTime(dr["fecha"].ToString());
                double Monto_prestamo = Convert.ToDouble(dr["Monto_prestamo"].ToString());
                string Codigo_ruta = dr["Ruta"].ToString();
                string Quotas = dr["Quotas"].ToString();
                double Balance = Convert.ToDouble(dr["Balance"].ToString());
                double Monto_pago = Convert.ToDouble(dr["Monto_pago"].ToString());
                double Mora = Convert.ToDouble(dr["Mora"].ToString());


                Dgv_cuadre_rutas.Rows.Add(Nombres, Apellidos, num_prestamo, fecha, Monto_prestamo, Codigo_ruta, Quotas, Balance, Monto_pago, Mora);

            }
            conn.Close();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM