Click here to Skip to main content
15,898,374 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi I am using Visual Studio 2005 for a project and I want to get data between a start date & end date in an ASP.NET form I am using SQL Server 2008 as backend & C#.

C#
con.Open();
SqlCommand cmd = new SqlCommand("select company_name,body_model from pur_body where date between '" + DateTime.Parse(TextBox1.Text).ToLongDateString() + "' and '" + DateTime.Parse(TextBox2.Text).ToLongDateString()+ "'", con);

      GridView1.DataSource = cmd.ExecuteReader();
        GridView1.DataBind();
        con.Close();


this code display message than "string was not recognized as a valid DateTime"

Please help me out ....
Posted
Updated 15-Jul-12 9:20am
v4
Comments
[no name] 15-Jul-12 10:23am    
So make TextBox1.Text and TextBox2.Text valid strings for converting to DateTime. And stop screaming.

use this code i think it will solve your problem

C#
con.Open();

	DateTime dt1, dt2;
        DateTime.TryParse(TextBox1.Text, out    dt1);
        DateTime.TryParse(TextBox2.Text, out    dt2);

SqlCommand cmd = new SqlCommand("select company_name,body_model from pur_body where date between '" + dt1 + "' and '" + dt2 + "'", con);
 
      GridView1.DataSource = cmd.ExecuteReader();
        GridView1.DataBind();
        con.Close();
 
Share this answer
 
Comments
sandeep nagabhairava 16-Jul-12 3:52am    
hi prosan catch my 5!
Okay, the pertinant question here seems to be: What is the format of the output string?
An example date copied from my mySql database is: "2012-06-11 00:00:00"

Perhaps this[^] page will help.
 
Share this answer
 
C#
con.Open();
 
	DateTime dt1, dt2;
        DateTime.TryParse(TextBox1.Text, out    dt1);
        DateTime.TryParse(TextBox2.Text, out    dt2);
 
SqlCommand cmd = new SqlCommand("select company_name,body_model from pur_body where date >= '" + dt1 + "' and 
<= '" + dt2 + "'", con);
 
      GridView1.DataSource = cmd.ExecuteReader();
        GridView1.DataBind();
        con.Close();
 
Share this answer
 

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