Click here to Skip to main content
15,899,475 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
protected void Button7_Click(object sender, EventArgs e)
{           
    using (MySqlConnection con = new MySqlConnection(constr))
    {                                            
        using (MySqlCommand cmd = new MySqlCommand("SELECT * FROM completeup WHERE Date_Time_Down BETWEEN @From AND @To", con))
        {
            using (MySqlDataAdapter da = new MySqlDataAdapter(cmd))
            {
              cmd.Parameters.AddWithValue("@From", Convert.ToDateTime(this.TextBox7.Text));
                cmd.Parameters.AddWithValue("@To", Convert.ToDateTime(this.TextBox8.Text));
                DataSet ds = new DataSet();
                da.Fill(ds);
                GridView2.DataSource = ds;
                GridView2.EmptyDataText = "No Record(s) Found";
                GridView2.DataBind();
            }
        }
    }
}
Posted
Updated 21-Dec-15 0:09am
v2
Comments
deepankarbhatnagar 21-Dec-15 7:54am    
What are the values of your TextBox7.Text & TextBox8.Text ?

Start with the debugger, and look at exactly what values you have in the two text boxes - remember that BETWEEN assumes that the first date is the earliest.

If your textboxes are the "wrong way round", you won't get any values:
SQL
SELECT * FROM MyTable
WHERE EnterDate BETWEEN '2015-11-01' AND '2015-11-05'
Returns 20 values, while
SQL
SELECT * FROM MyTable
WHERE EnterDate BETWEEN '2015-11-05' AND '2015-11-01'
Returns none.

This is a good example of why you should never use Visual Studio default names for anything - you may remember that "TextBox6" is the mobile number today, but when you have to modify it in three weeks time, will you then? Use descriptive names - "tbMobileNo" for example - and your code becomes easier to read, more self documenting, easier to maintain - and surprisingly quicker to code because Intellisense can get to to "tbMobile" in three keystrokes, where "TextBox8" takes thinking about and 8 keystrokes...
 
Share this answer
 
v2
Comments
Nigol_Learner 21-Dec-15 17:53pm    
cmd.Parameters.AddWithValue("@From", Convert.ToDateTime(this.frdatettxt.Text));
cmd.Parameters.AddWithValue("@To", Convert.ToDateTime(this.todatetxt.Text));

Bro, now I already rename the textbox for more easy to understand which is from date to date, but how ever I already try both way of date, the textbox value content date like this 12/20/2015 to 12/21/2015 and I try other way round like this 12/21/2015 to 12/20/2015, but the result I get same only, No Record(s) Found.
Nigol_Learner 21-Dec-15 18:22pm    
cmd.Parameters.AddWithValue("@From", (this.frdatettxt.Text));
cmd.Parameters.AddWithValue("@To", (this.todatetxt.Text));

I Have to remove this code Convert.ToDateTime, then it's works bro, but now having other issue, when filter 12/20/2015 to 12/22/2015, it's only shows the record between 12/20/2015 to 12/21/2015, its didn't show the last to date 12/22/2015, if want to see record of date 12/22/2015, I have to filter like this > 12/20/2015 to 12/23/2015. Please help bro.
SELECT * FROM completeup

WHERE ((Date_Time_Down between @From and dateadd(dd,1,@ToDate)) or (@FromDate=@ToDate))
 
Share this answer
 
Comments
Nigol_Learner 23-Dec-15 2:02am    
didn't work bro
Nigol_Learner 7-Jan-16 2:05am    
Fill: SelectCommand.Connection property has not been initialized. I get this error bro.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900