Saturday, March 24, 2012

Stripping the time portion from date field

Hi all
I've got a textbox on the web page that captures a date.
When an insert is done, the information captured into the database is of the format dd/mm/yyyy hh:mm:ss.
How do I strip the time portion and save only the date part into the database ?
I'm using a formview, sqldatasource controls for this web page.
I tried the following in formview iteminserting event:
Dim culture_2 As System.Globalization.CultureInfo = New CultureInfo("en-GB", True)
Dim wAcceptanceD As TextBox = CType(FormView1.Row.FindControl("dtAcceptDTextBox"), TextBox)
e.Values.Add("dtAcceptD", DateTime.Parse(wAcceptanceD.Text, culture_2, DateTimeStyles.NoCurrentDateDefault))

When I run the web page, I get the following error:

System.ArgumentException was unhandled by user code
Message="Item has already been added. Key in dictionary: 'dtAcceptD' Key being added: 'dtAcceptD'"
Source="mscorlib"
StackTrace:
at System.Collections.Hashtable.Insert(Object key, Object nvalue, Boolean add)

at System.Collections.Hashtable.Add(Object key, Object value)

at System.Collections.Specialized.OrderedDictionary.Add(Object key, Object value)

at Default3.FormView1_ItemInserting(Object sender, FormViewInsertEventArgs e) in C:\SRS\Default3.aspx.vb:line 12

at System.Web.UI.WebControls.FormView.OnItemInserting(FormViewInsertEventArgs e)

at System.Web.UI.WebControls.FormView.HandleInsert(String commandArg, Boolean causesValidation)

at System.Web.UI.WebControls.FormView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup)

at System.Web.UI.WebControls.FormView.OnBubbleEvent(Object source, EventArgs e)

at System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)

at System.Web.UI.WebControls.FormViewRow.OnBubbleEvent(Object source, EventArgs e)

at System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)

at System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e)

at System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument)

at System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)

at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)

at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)

at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

Can someone pleaseeeeeeeeeeeeeeeee tell me what's wrong ?
TIA.

The problem here is not the date parsing. Well, it is a problem too but not the one you're seeing.
You're using always the same key in your dictionary: "dtAcceptD". The first time it's ok, the second time, it crashes with the error you're seeing. A dictionary can have only one entry with a given key.
As for date parsing, you should specify the date format you're expecting explicitly. There is an overload of ParseExact that takes a format string.
Once you've gotten a DateTime object, it's easy to strip the time part (but why are you asking for it if you're going to throw it away? ) by constructing a new date object from the parts of the old one, something like:
new DateTime(myDate.Year, myDate.Month, myDate.Day)

Hi
Thanks for your reply.
Realised the following:
1) SQL saves the information for a datetime field with date and time information.
2) Viewing the information from the following sources have different results:
From SQL Enterprise Manager, information displayed as dd/mm/yyy.
From SQL Query Analyzer, information displayed as yyyy-mm-dd 00:00:00.
From VS (via the Database Explorer), its displayed as dd/mm/yyyy 12:00:00.
I probably dont need to do any conversion in the ItemInserting event of the formview.
Thanks for the tip on stripping the time portion of a datetime object. I'm sure it will come in handy.

0 comments:

Post a Comment