Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi I have one column called date in database and my database datecolumn contain like this 2013-02-22,but in front end iam using 2 ajaxdate pickers and searching for a record
i am writing the query like this,i want to know whether it is correct or not
SQL
SELECT * from table where Date between '" + txtfromdate.Text +" "and" "+txttodate +"'";
Posted
Updated 4-Mar-13 8:10am
v2

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:
SQL
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.
C#
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);
                }
            }
        }
    }
 
Share this answer
 
if you are using inline queries then try this


C#
string SQL = "SELECT * from table where Date between '" + txtfromdate.Text + "' and '" + txttodate.Text + "'";
 
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