Show more current data in a datagridview

0

At the moment of clicking the radiobutton "Current" I want the datagridview only to show me the most current data of the line that I select in the combobox. When I run the program it gives me the following error: Incorrect syntax near '11 -28-16 '. Initially I tried to take the datetimepicker date but it gave me the same error. How can I solve this?

Code:

    private void btnFiltrar_Click(object sender, EventArgs e)
    {
        if (rbTodos.Checked) {

            conexion.Open();
            da = new SqlDataAdapter("select * from CWS where Family = '"+cbLine.Text+"' and Date between '" + dateTimePicker1.Value.ToString("MM/dd/yyyy") + "'and'" + dateTimePicker3.Value.ToString("MM/dd/yyyy") + "'", conexion);
            DataSet DS = new DataSet();
            da.Fill(DS, "CWS");
            dataCWSreport.DataSource = DS.Tables["CWS"];
            conexion.Close();
        }

        if (rbActual.Checked){

            String fecha;
            int fecha1 = lblShowDate.Text();
            fecha = lblShowDate.ToString();
            conexion.Open();
            da = new SqlDataAdapter("select * from CWS where Family = '" + cbLine.Text + "' and Date '" + lblShowDate.Text + "'", conexion);
            DataSet DS = new DataSet();
            da.Fill(DS, "CWS");
            dataCWSreport.DataSource = DS.Tables["CWS"];
            conexion.Close();
        }

    }

PS: the data type of the Date column is 'date'.

    
asked by Eduardo Parra 28.11.2016 в 21:50
source

4 answers

0

Clicking the RadioButton actual orders the query in a descendant way:

da = new SqlDataAdapter("SELECT * FROM CWS WHERE Family = '" + cbLine.Text + "' and Date = '" + lblShowDate.Text + "' ORDER BY [Date] DESC", conexion);
    
answered by 28.11.2016 / 22:08
source
0

What kind of friend, e your query da = new SqlDataAdapter("select * from CWS where Family = '"+cbLine.Text+"' and Date between '" + dateTimePicker1.Value.ToString("MM/dd/yyyy") + "'and'" + dateTimePicker3.Value.ToString("MM/dd/yyyy") + "'", conexion); I see that the format of your date is the following: MM / dd / yyyy And in your Datagridview is: 23-Nov-15 Check in your database how the Date is saved, it may be a question of date format. Greetings

    
answered by 28.11.2016 в 23:18
0

Use the format without dates for dates "yyyyMMdd" in the ToString of your DateTimePicker and this will not mark you error.

    
answered by 29.11.2016 в 13:46
0

Another feasible way is with a TOP in your query, this serves to limit the rows and not overload data your grid, also ordering the dates from highest to lowest, see:

da = new SqlDataAdapter("SELECT TOP 5 FROM CWS where Family = '" + cbLine.Text + "' ORDER BY [Date]DESC",conexion);

This can work for you, too, although I would not place a TOP .

    
answered by 30.11.2016 в 20:59