Click here to Skip to main content
15,890,741 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi.

I have a question about SQL statement.
I have a MonthCalendar where I select a date. The date then shows up in a textbox.
On the form I also have a datagridview.

I have tried alot of diffrent converters.
C#
DateTime condition = Convert.ToDateTime(textBox1.Text);
            sda = new SqlDataAdapter("SELECT * FROM tbl_Tidrapport WHERE Datum LIKE "+ condition +"", cn);
            dt = new DataTable();
            sda.Fill(dt);
            dataGridView1.DataSource = dt;

Always get error.

Please help....
How can I get the date in the SQL query?
Posted
Updated 27-Apr-15 23:44pm
v2

LIKE won't work, it's for strings: and the chances are that it doesn't match because the Datum value doesn't exactly match to the microsecond - which is what an equality check will expect.
So try this:
C#
DateTime condition = Convert.ToDateTime(textBox1.Text).Date;
sda = new SqlDataAdapter("SELECT * FROM tbl_Tidrapport WHERE CONVERT(date, Datum) = @DT", cn);
sda.SelectCommand.Properties.AddWithValue("@DT", condition);
dt = new DataTable();
sda.Fill(dt);
dataGridView1.DataSource = dt;
 
Share this answer
 
Comments
Andy Lanng 28-Apr-15 6:03am    
I like this solution because it uses parameters (i forgot how to use parameters without creating a command ^_^) AND it answers the 'Like' issue I mentioned - 5*
thaagaard elofsson 28-Apr-15 6:25am    
Thank you so much. I got it to work.
sda.SelectCommand.Parameters.AddWithValue("@DT", condition);
OriginalGriff 28-Apr-15 6:32am    
You're welcome!
First of all - Do not insert into your query directly. Instead use SqlParameters. This helps avoid SQL Injection dangers.

Your query has a second issue: the "condition" would be used as a field name as it has no single quotes within the query. Parameters take care of that too.

Try this:
C#
DateTime condition = Convert.ToDateTime(textBox1.Text);
dt = new DataTable();
using (SqlCommand cmd = cn.CreateCommand())
{
    cmd.CommandText = "SELECT * FROM tbl_Tidrapport WHERE Datum LIKE @condition";
    cmd.CommandType = CommandType.Text;
    cmd.Parameters.Add(new SqlParameter("@condition", SqlDbType.DateTime) {Value = condition});
    using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
    {
        cn.Open();
        sda.Fill(dt);
        cn.Close();
    }
}



Also, using "Like" with a date type doesn't really make any sense. I have no idea what you are trying to achieve with that, but I have left it in my solution.

If you want help with the "Like" issue then let me know in a comment

Good luck :)
 
Share this answer
 
Syntax error for Like search
refer this SQL Like query [^]

do something like this
C#
string condition = "";
        var   sda = new SqlDataAdapter("SELECT * FROM tbl_Tidrapport WHERE Datum LIKE '%" + condition + "%'", cn);


Note: Be aware of SQL Injection [^]
SQL Injection Attacks[^]
 
Share this answer
 
v2
Comments
Andy Lanng 28-Apr-15 6:05am    
Try to avoid answers that do not address SQL Injection. The Date issue appears to be a whole problem in itself.
Technically correct though. You did answer his question as asked ;)
Karthik_Mahalingam 28-Apr-15 6:17am    
I agree,
do you think, if i bring the topic SQL Injection, he will be aware of that ??
Andy Lanng 28-Apr-15 6:26am    
The point is that he is most likely unaware of it, so we should make him aware, IMHO anyway ^_^
Karthik_Mahalingam 28-Apr-15 7:38am    
updated the solution :)
Andy Lanng 28-Apr-15 7:39am    
upvoted :D

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