hi,
i work on windows application and i have this code:

SqlCeDataReader rrs;
            SqlCeCommand comd = new SqlCeCommand("select CodePersonne from Table1 where DateR ='" + DateTime.Now.Date    + "';", conne);
            rrs = comd.ExecuteReader();

while running, i get this error:
The data type is not valid for the boolean operation. [ Data type (if known) = datetime,Data type (if known) = nvarchar ]
i can't find where is the mistake and i wish you can help me.

Recommended Answers

All 8 Replies

SqlCeDataReader rrs;

SqlCeCommand comd = new SqlCeCommand("select CodePersonne from Table1 where DateR='" + DateTime.Now + "'", conne);

rrs = comd.ExecuteReader();

thank for reply but it doesn't work

Please post "Exception trace" or "Error messages" but do not say "It doesn't work".

Sorry but really the same error message appear

>Sorry but really the same error message appear

No problem. Let me know the field type of DateR column and if possible, post ~complete~ code here.

You can also use parameterized query.

SqlCeConnection cn = new SqlCeConnection(@"Data Source=c:\test\compactdb\Database1.sdf");
            //SqlCeCommand cmd = new SqlCeCommand("select * from table1 where dater='" + DateTime.Now.ToShortDateString() + "'", cn);

            // OR  

            SqlCeCommand cmd = new SqlCeCommand("select * from table1 where dater=@p1", cn);
            cmd.Parameters.AddWithValue("@p1", DateTime.Now.ToShortDateString());

            cn.Open();
            SqlCeDataReader dr = cmd.ExecuteReader();

            while (dr.Read())
            {
                Console.WriteLine(dr[0] + " " + dr[1]);

            }
            dr.Close();
            cn.Close();

thank you. the type field of DateR is datetime and this is the full code:

private void rehabForm_Load(object sender, EventArgs e)
        {

            SqlCeConnection conne = new SqlCeConnection();
            conne.ConnectionString = @" Data source= |DataDirectory|\Database1.sdf";
            conne.Open();
            SqlCeDataAdapter da;
            DataTable dtresullt = new DataTable();
             SqlCeDataReader rrs;
            SqlCeCommand comd = new SqlCeCommand("select CodePersonne from  Table1 where DateR ='" + DateTime.Now.ToShortDateString() + "';", conne);
            rrs = comd.ExecuteReader();
            
             while (rrs.Read())
                {
                    SqlCeDataReader rs;
                    SqlCeCommand cmd = new SqlCeCommand("SELECT [Nom],[Prenom],[DateNaissance]  FROM  Table2 where CodePersonne ='" + Convert.ToInt32(rrs["CodePersonne"].ToString()) + "';", conne);
                    rs = cmd.ExecuteReader();
                    da = new SqlCeDataAdapter(cmd);
                    da.Fill(dtresullt);

                }
                dataGridView1.DataSource = dtresullt;
           

        }

thanks again

try this

DateTime.Now.toString("yyyyMMdd");

try this
C# Syntax (Toggle Plain Text)

1.
DateTime.Now.toString("yyyyMMdd");

thank you very much, it is solved

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.