Date Conversion Problem
-
Hi, I have a ASP.NET page, in which I have to compare two Dates. One comes from Database, and other one is selected from Calendar Control, assigned to textbox. When I code... If CDate(txtDespDate.Text) < CDate(MasterDispDt) Then LabelMsg.Text = "Invalid Date Selection" Exit Sub End If This is the Error I am getting, Error Message: Conversion from string "21-04-2009" to type 'Date' is not valid. Source : Microsoft.VisualBasic Stack Trace : at Microsoft.VisualBasic.CompilerServices.Conversions.ToDate(String Value) at DocTransfers.BtnTransfer_Click(Object sender, EventArgs e) at System.Web.UI.WebControls.Button.OnClick(EventArgs e) at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) at System.Web.UI.WebControls.Button.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)
-
Hi, I have a ASP.NET page, in which I have to compare two Dates. One comes from Database, and other one is selected from Calendar Control, assigned to textbox. When I code... If CDate(txtDespDate.Text) < CDate(MasterDispDt) Then LabelMsg.Text = "Invalid Date Selection" Exit Sub End If This is the Error I am getting, Error Message: Conversion from string "21-04-2009" to type 'Date' is not valid. Source : Microsoft.VisualBasic Stack Trace : at Microsoft.VisualBasic.CompilerServices.Conversions.ToDate(String Value) at DocTransfers.BtnTransfer_Click(Object sender, EventArgs e) at System.Web.UI.WebControls.Button.OnClick(EventArgs e) at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) at System.Web.UI.WebControls.Button.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)
Dim time1 As String = DateTime.Parse("2:15 PM").ToString("t") Dim time2 As String = DateTime.Now.ToString("t") If DateTime.Compare(DateTime.Parse(time1), DateTime.Parse(time2)) < 0 Then Response.Write("time1 is less than time2") Else Response.Write("time1 is greater than time2") End If
I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post.
-
Hi, I have a ASP.NET page, in which I have to compare two Dates. One comes from Database, and other one is selected from Calendar Control, assigned to textbox. When I code... If CDate(txtDespDate.Text) < CDate(MasterDispDt) Then LabelMsg.Text = "Invalid Date Selection" Exit Sub End If This is the Error I am getting, Error Message: Conversion from string "21-04-2009" to type 'Date' is not valid. Source : Microsoft.VisualBasic Stack Trace : at Microsoft.VisualBasic.CompilerServices.Conversions.ToDate(String Value) at DocTransfers.BtnTransfer_Click(Object sender, EventArgs e) at System.Web.UI.WebControls.Button.OnClick(EventArgs e) at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) at System.Web.UI.WebControls.Button.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)
use DateTime.ParseExact to convert your date in "dd/mm/yyyy" fromat to "mm/dd/yyyy" format, since sql only recognises the latter format. eg:
Dim invoicedate As DateTime = DateTime.ParseExact(Inv_Date.Text, "dd/MM/yyyy", System.Globalization.CultureInfo.CurrentCulture)
Hope it helps...
When you fail to plan, you are planning to fail.
-
Hi, I have a ASP.NET page, in which I have to compare two Dates. One comes from Database, and other one is selected from Calendar Control, assigned to textbox. When I code... If CDate(txtDespDate.Text) < CDate(MasterDispDt) Then LabelMsg.Text = "Invalid Date Selection" Exit Sub End If This is the Error I am getting, Error Message: Conversion from string "21-04-2009" to type 'Date' is not valid. Source : Microsoft.VisualBasic Stack Trace : at Microsoft.VisualBasic.CompilerServices.Conversions.ToDate(String Value) at DocTransfers.BtnTransfer_Click(Object sender, EventArgs e) at System.Web.UI.WebControls.Button.OnClick(EventArgs e) at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) at System.Web.UI.WebControls.Button.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)