Date format confusion

Please support our MS SQL advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Apr 2007
Posts: 45
Reputation: Eager_Beever is an unknown quantity at this point 
Solved Threads: 0
Eager_Beever Eager_Beever is offline Offline
Light Poster

Date format confusion

 
0
  #1
Mar 9th, 2009
I have an ASP.net 2.0 website with SQL Server as database and C# 2005 as the language. On my webpage I have a calender from which I choose a date and display only the date part in a textbox (readonly) in the format dd/mm/yyyy. But internally SQL Server uses m/d/y date format. I am using parameterised queries to insert and select records. But when I checked the database using the management tool and link supplied to me by the hosting service, I found that a few records have been saved in dd/mm/yyyy format while the rest have been saved the the default mm/dd/yyyy format. I suspect, it happened when I made some changes in the date format handling code and saved some records. But since the no. of records are not many, I am ready to delete all existing records and use the entry page again and add ALL records. But how should I ensure that even if I display the date in dd/mm/yyyy format on my webpage it gets saved in only one format (either m/d/y or d/m/y). The date format is very important to me, because most of the time the records will be fetched based on a specific date.

Is there any way, by which I can choose the date format to be d/m/y while saving and retrieving the records from the database??

Lalit Kumar Barik
India
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,162
Reputation: dickersonka will become famous soon enough dickersonka will become famous soon enough 
Solved Threads: 137
dickersonka dickersonka is offline Offline
Veteran Poster

Re: Date format confusion

 
0
  #2
Mar 10th, 2009
Are you sure you set the column's data type to datetime? It sounds like you are using varchar
Last edited by dickersonka; Mar 10th, 2009 at 12:55 pm.
Custom Application & Software Development
www.houseshark.net
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 45
Reputation: Eager_Beever is an unknown quantity at this point 
Solved Threads: 0
Eager_Beever Eager_Beever is offline Offline
Light Poster

Re: Date format confusion

 
0
  #3
Mar 10th, 2009
The RDate field's datatype is smalldatetime. My requirement is that a calender is displayed on the page along with a collection of times (displayed in a dropdown list and which contains values like 09:00 AM, 09:30 AM, etc )for the RTime field. The record contains RDate, DTime and other values. For the same RDate, there can be multiple records each having a different RTime. No two records can exist with the same combination of RDate & RTime. I am facing problems in implementing this simple requirements. When the user chooses a date from the calender, I am storing it in a DateTime variable as it is and also displaying it in a readonly textbox in the format "dd/MM/yyyy" for user's reference. While inserting the record I am checking against the DateTime variable and also using that value in the record. But I am facing problem while checking for unique record. Even if I change the Date by choosing a different date from the calender and select the same RTime value as an earlier choosen one, I am getting an error for duplicate entry. My code to check duplicates is as follows:
  1.  
  2. DateTime dtCurrentDate ;
  3. CalendarRDate.SelectedDate = DateTime.Today;
  4. dtCurrentDate = CalendarRDate.SelectedDate;
  5.  
  6.  
  7. string strConnection = ConfigurationManager.ConnectionStrings["SuperConnString"].ConnectionString;
  8. string strSQLQueryCheckDuplicates = @"Select Count(*) from Results where RDate = @RDate And RTime = @RTime";
  9.  
  10. SqlConnection sqlConnCheckDuplicates = NULL;
  11.  
  12. try
  13. {
  14. sqlConnCheckDuplicates = new SqlConnection(strConnection);
  15. sqlConnCheckDuplicates.Open();
  16.  
  17. SqlCommand sqlCmdCheckDuplicates = new SqlCommand(strSQLQueryCheckDuplicates, sqlConnCheckDuplicates);
  18. sqlCmdCheckDuplicates.Parameters.ADD(new SqlParameter("@RDate", dtCurrentDate));
  19. sqlCmdCheckDuplicates.Parameters.ADD(new SqlParameter("@ResultTime", ddRTime.Text.Trim()));
  20.  
  21. IF (Convert.ToInt32(sqlCmdCheckDuplicates.ExecuteScalar()) > 0)
  22. {
  23. lblMessage.Text = "Results for choosen timeslot has been entered already";
  24. ddRTime.Focus();
  25. RETURN;
  26. }
  27. else
  28. lblMessage.Text = string.Empty;
  29. }
  30. finally
  31. {
  32. sqlConnCheckDuplicates.Close();
  33. }

Where am I making the mistake??

Lalit Kumar barik
India
Last edited by peter_budo; Mar 18th, 2009 at 7:23 am. Reason: Please use [code][/code] instead of [quote][/quote]
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,162
Reputation: dickersonka will become famous soon enough dickersonka will become famous soon enough 
Solved Threads: 137
dickersonka dickersonka is offline Offline
Veteran Poster

Re: Date format confusion

 
0
  #4
Mar 10th, 2009
For starters lets start off with syntax:

Did you see this line?
SqlParameter("@ResultTime", ddRTime.Text.Trim()));
your parameter is named @RTime
Last edited by peter_budo; Mar 18th, 2009 at 7:24 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Custom Application & Software Development
www.houseshark.net
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 45
Reputation: Eager_Beever is an unknown quantity at this point 
Solved Threads: 0
Eager_Beever Eager_Beever is offline Offline
Light Poster

Re: Date format confusion

 
0
  #5
Mar 10th, 2009
Originally Posted by dickersonka View Post
For starters lets start off with syntax:

Did you see this line?
SqlParameter("@ResultTime", ddRTime.Text.Trim()));

your parameter is named @RTime
I acknowledge my mistake. But in the real life coding it was rectified already. I didn't notice it when I did some copy paste job.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,162
Reputation: dickersonka will become famous soon enough dickersonka will become famous soon enough 
Solved Threads: 137
dickersonka dickersonka is offline Offline
Veteran Poster

Re: Date format confusion

 
1
  #6
Mar 10th, 2009
Ok, just wanted to make sure we aren't chasing a ghost

Rather than just adding the parameters with a name and value, try to add them with a name and type
  1. SqlParameter param1 = new SqlParameter("@RDate", SqlDbType.DateTime);
  2. param1.Value = dtCurrentDate;
  3. sqlCmdCheckDuplicates.Parameters.ADD(param1);

and do the same for the other one
Custom Application & Software Development
www.houseshark.net
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 45
Reputation: Eager_Beever is an unknown quantity at this point 
Solved Threads: 0
Eager_Beever Eager_Beever is offline Offline
Light Poster

Re: Date format confusion

 
0
  #7
Mar 10th, 2009
Ok, I will revise my code to implement this feature at ALL places.

Thank you for your suggestions.

Lalit Kumar Barik
India
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC