Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
I want to retrieve previous date from database in datetime control.
 
what I tried is
queryString = "SELECT Startdate FROM master WHERE srno="+ t +"";
while (dr.Read())
{                    
    DateTime stdate = Convert.ToDateTime(dr.GetValue(2).ToString());
    this.dtpBlockStartDate.Value = new DateTime(stdate.Year , stdate.Month , stdate.Day);
}
datetime control is displaying current date and not the date from database.
Posted 25-Aug-14 1:56am
Edited 25-Aug-14 2:01am
v2
Comments
Neetin_1809 at 25-Aug-14 7:26am
   
modify Your QUeryString to TOp 1 clause and Use Order By Clause
sachees123 at 25-Aug-14 7:38am
   
getting date is not a problem, but getting it displayed in datetime control is a BIG problem. Also there are many other fields in query.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

1.The parameter t from your command should be the date that you want, if you want yesterday should be:
DateTime t = DateTime.Now.AddDays(-1);
 
2.Then you should refine your reading code like this:
if(dr.Read())
{
    this.dtpBlockStartDate.Value = (DateTime)dr["Startdate"]; 
}
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

When you are working with date and time objects. Then do not convert it to a string.
Only convert to a string when you want to show it to the user.
 
If table column "Startdate" is a datetime type in MS SQL. You can use the value direct like:
var sqlConnection= new SqlConnection(sqlConnectionString);
sqlConnection.Open()
 
var sqlCommand = sqlConnection.CreateCommand();
sqlCommand.CommandText = "SELECT Startdate FROM master WHERE srno=@srno";
sqlCommand.Parameters.AddWithValue("srno", t);
 
object result = sqlCommand.ExecuteScalar();
 
if (result != null)
{
  dtpBlockStartDate.Value = (DateTime)result;
}
 
Remember to use parameterized sql statement Smile | :)
http://blog.codinghorror.com/give-me-parameterized-sql-or-give-me-death/[^]
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Hello ,
If you DataType is DateTime & Using with C#.Try the Following Code to set a value.
There is a CustomFormat Property for the DatePicker control that is needed to be specified.
 dtpBlockStartDate.CustomFormat = "dd-MMM-yyyy";
 dtpBlockStartDate.Value =Convert.ToDateTime(ds.Tables[0].Rows[0][0]);
 

 
ds refers to dataset & i am suposing that it is in first column.
 
Try To debug it .SomeWhere you might be resetting it to Currrent Date.
 
If any Problem Then Paste Your Complete Code
Hope It Helps You
Happy coding Smile | :)
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 DamithSL 265
1 CPallini 235
2 OriginalGriff 233
3 Maciej Los 175
4 George Jonsson 170
0 OriginalGriff 5,305
1 DamithSL 4,382
2 Maciej Los 3,760
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,901


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 28 Aug 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100