First problem is the
Date
is a reserved word in SQL (and most other databases, so you either need to change the column name, or enclose it is square brackets.
The second is that your quotes do not match up!
The third is that
txtodate
is (probably) a text box, so unless you add the ".Text" propery to it you will end with a an SQL statement like this:
SELECT * from table where [Date] between '2013-01-01' and 'System.Windows.Forms.TextBox, Text: 2013-02-18'
But, please, don't do it like that!
Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
using (SqlConnection con = new SqlConnection(strConnect))
{
con.Open();
using (SqlCommand com = new SqlCommand("SELECT * from table where [Date] between @SD and @ED", con))
{
com.Parameters.AddWithValue("@SD", txtfromDate.Text);
com.Parameters.AddWithValue("@ED", txttodate.Text);
using (SqlDataReader reader = com.ExecuteReader())
{
while (reader.Read())
{
int id = (int) reader["iD"];
string desc = (string) reader["description"];
Console.WriteLine("ID: {0}\n {1}", iD, desc);
}
}
}
}