Click here to Skip to main content
15,900,511 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have a field(dt) in sql database with datatype "date" and in my c# form there is a date in a label( like 07-11-2011).i wanna write a while condition to compare with a date in database...something like........
C#
string query = "select name from vacseat where dt='"+lblDate.Text+"'";

its not working ,plz help
thanx in advance.
Posted
Updated 7-Nov-11 7:58am
v2

either cast the date to a varchar or cast the string to a date. Right now you are trying to compare two different data types.

Also, it would be a very good idea to use parameterized query or stored procedure to avoid possible SQL injection attacks
 
Share this answer
 
Comments
Himu from Orissa 8-Nov-11 9:11am    
the same code is working on other system but in my system its not working
First of all having a Label that displays a date is not such a good idea (unless it is actually bound to a DateTime Object). If that is the case you should really use your DateTime Object in a PARAMETERIZED QUERY!!!
Parameterized queries keep your calls to the database clean and safe (no SQL injection!) :)
The code should look something like this:
C#
DateTime dt = DateTime.Today;
String query = "select name from vacseat where dt = @dt";
DbCommand cmd = new DbCommand(query, connectionObject); // Probably an SqlCommand or something.
cmd.Parameters.AddWithValue("@dt", dt); // Replaces @dt in your query with an actual date!
// Etc...

This should do the trick :)
For more info on Command Object see this MSDN page[^].
Good luck! :)
 
Share this answer
 
Comments
Himu from Orissa 8-Nov-11 9:03am    
i want to use the name in a datatable,so i have to use a data adapter rather than command like.........
SqlDataAdapter da = new SqlDataAdapter(query, conn);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
so how can i use sqlcommand ??????
Himu from Orissa 8-Nov-11 9:26am    
i got it ,its something like...
SqlCommand cmd = new SqlCommand(query, conn)
SqlDataAdapter da = new SqlDataAdapter(cmd)
its working in other systems but not in my system..hope there is some other problem
Sander Rossel 8-Nov-11 17:35pm    
That should do the trick. You can indeed pass Command Objects to DataAdapters. Are you sure your connection is open before calling da.Fill(ds); ?
First of all, the date format in SQL Server(hoping this is your database tech.) and the date format in .NET has to match in there format. Usually the format you get in SQL Server is not directly match. Unless you convert it helper SQL function. One of SQL Server function that helps for such kind of case is Convert [^]function.So you query will look like
C#
string query = "select name from vacseat where CONVERT(VARCHAR(10),dt,105)= '"+lblDate.Text+"'"; // Will format native SQL date time value to dd-mm-yyyy format
In addition to this what Naerling has suggest about SQL Injection is quite helpful for database safety.

Home this will help you well.
 
Share this answer
 
v2
Comments
Himu from Orissa 8-Nov-11 9:05am    
the field in database is of type only date i.e it is getting stored like"08-11-2011",so no need to convert
Himu from Orissa 22-Nov-11 7:16am    
sorry i think i was gone mad when i first read ur solution,i dont know how to thank u bcoz it solved a big problem........."code project" u beauty........
Wonde Tadesse 22-Nov-11 8:57am    
You 're most welcome
Himu from Orissa 24-Nov-11 4:35am    
But sir i just want to know what is this 105
Wonde Tadesse 24-Nov-11 10:07am    
It is one of a formatting input of Convert SQL function. You can find the detail from this link http://www.w3schools.com/sql/func_convert.asp

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