L. Porfido
L. Porfido

Reputation: 27

I can't run a correct query on visual studio

I think the problem is in the format. I have this code in the class:

public DataTable CheckDate(DateTime day)
    {
        MySqlCommand command = new MySqlCommand("SELECT * FROM `appuntamenti` WHERE `date`='@date'", conn.getConnection());
        command.Parameters.Add("@date", MySqlDbType.Date).Value = date;
        MySqlDataAdapter adapter = new MySqlDataAdapter();    
        DataTable table = new DataTable();
        adapter.SelectCommand = command;
        adapter.Fill(table);
        return table;
    }

e in the form c#, i use:

private void button2_Click(object sender, EventArgs e)
    {          
        DateTime day = dateTimePicker2.Value;
        dataGridView1.DataSource = appuntamento.CheckDate(day);

    }

When i click the button2, it is generate the query and the empty filtered table are generated. The format in database is YYYY-MM-GG but in visual studio the format is GG-MM-YYYY. I change the format in YYYY-MM-GG but it create an unusual bug (i click on datatimepicker 24/04/2020 but the output datatimepicker is 2020-00-24). When i use my query on localhost/phpmyadmin, it works. Can you help me?

Upvotes: 0

Views: 92

Answers (2)

Marlon Assef
Marlon Assef

Reputation: 1551

Remove all quotes in the query.

Like this:

"SELECT * FROM appuntamenti WHERE date = @date"

Upvotes: 0

Nieminen
Nieminen

Reputation: 1284

Try removing the quotes from around @date.

The PDO statement should handle the rest

Upvotes: 2

Related Questions