Question How can I code so records filtered between two dates in DataGridView display in the Textboxs?

Godis

Member
Joined
Jul 31, 2019
Messages
19
Programming Experience
Beginner
I am working on a project where I want records filtered between two given dates in the datagridview display in the form textboxes accordingly.

I have tried all I could but it is not working. I have also tried to place a label control as alternative to total up the filtered records on the datagridview but I couldn't code to make it successful.

Please I appreciate if someone can help me out on these two problems.

I hereby attach my filter button code for correction.

Thanks as I look forward for correction
C#:
 private void btnSearch_Click(object sender, EventArgs e)
        {
            
            con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString);
            con.Open();

            SqlDataAdapter da = new SqlDataAdapter("Select * From FarmHistory Where Date Between '"+fromDateTimePicker.Value.ToString()+"' And '"+toDateTimePicker.Value.ToString()+"'", con);
            DataTable dt = new DataTable();
            da.Fill(dt);       
            farmHistoryDataGridView.DataSource = dt;                         
          }
 
You are back to where you started: You are instantiating cmd on line 42 and didn't set the connection on it.

Why are you even calling ExecuteScalar(), or for that matter why do you even instantiate an SqlCommand on line 42 when you have another SqlCommand on line 52?

Right now it looks like you are just randomly throwing code at the screen and trying to see what sticks. You need to step back and think about things. A SqlDataAdapter executes an SqlCommand so that it gets the data from the server into a local DataSet. That SqlCommand needs parameters set on it so that you can define the start and end dates.
 
Back
Top Bottom