String was not recognized as a valid DateTime.

Posted by: wewewe123 on 10 September 2017, 2:48 am EST

  • Posted 10 September 2017, 2:48 am EST

    I am using C1CalendarExtender to display the calendar when the user click on the textbox.

    But now it is showing this error : String was not recognized as a valid DateTime.

    I not sure is because of my stored procedure having the problem , or my asp.net having the problem or it is because i am using C1CalendarExtender. That y i have to code it slightly differently.

    In my database record my data of the createddate data is : 2012-05-07 13:33:11.490

    What i am doing now is a search function, As calendar only display date so i wanted to search the record by date . Is it possible ?

    -------------------------------------------
    This is my stored procedure code
    -------------------------------------------


    USE [Test]
    GO
    /****** Object: StoredProcedure [VRM].[GET_Acknowledgement] Script Date: 07/18/2012 14:28:43 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    ALTER procedure [VRM].[GET_Acknowledgement]
    (
    @username varchar(200) = null,
    @deptname varchar(255) = null,
    @roledesc varchar(100) = null,
    @grantedon datetime,
    @status varchar(50) = null,
    @createdby varchar(50) = null
    )
    As

    IF @username = ''
    BEGIN
    SET @username = '%'
    END

    IF @deptname =''
    BEGIN
    SET @deptname = '%'
    END

    IF @roledesc =''
    BEGIN
    SET @roledesc = '%'
    END

    IF @grantedon =''
    BEGIN
    SET @grantedon = '%'
    END

    IF @status =''
    BEGIN
    SET @status = '%'
    END

    IF @createdby =''
    BEGIN
    SET @createdby = '%'
    END

    Select Common.tbl_employee.username , Common.department.DeptName , RBACRole.roleDesc ,
    RBACRole.createdDate , CAST(CONVERT(VARCHAR(10),RBACRole.createdBy,121)As DATETIME) , Common.tbl_employee.status , RBACRole.lastUpdatedDate
    From Common.tbl_employee , Common.section , Common.department , RBACEmployeeRole , RBACRole
    Where Common.tbl_employee.sectionid = Common.section.SectionID And
    Common.section.DeptID = Common.department.DeptID And
    Common.tbl_employee.userlogin = RBACEmployeeRole.userlogin And
    RBACEmployeeRole.roleId = RBACRole.roleId
    And
    ( Common.tbl_employee.username Like '%' + @username + '%')
    And (Common.department.DeptName Like '%' + @deptname + '%')
    And (VRM.RBACRole.roleDesc Like '%' + @roledesc + '%')
    And (CAST(CONVERT(VARCHAR(10),VRM.RBACRole.createdDate,121)As DATETIME) = @grantedon)
    And (Common.tbl_employee.status Like @status + '%')
    And (VRM.RBACRole.createdBy Like '%' + @createdby + '%')

    -----------------------------
    This is my asp.net code
    -----------------------------

    private DataTable Search()
    {

    DataTable SearchResultTable = new DataTable();
    SqlConnection con = new SqlConnection(@"User id=sa;Password=passw0rd;Server=L33524;Database=Test");


    try
    {
    SqlCommand cmd = new SqlCommand("VRM.GET_Acknowledgement", con);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@username", txtName.Text);
    cmd.Parameters.AddWithValue("@deptname", ddlDepartment.Text);
    cmd.Parameters.AddWithValue("@roledesc", txtRole.Text);
    cmd.Parameters.AddWithValue("@grantedon", Convert.ToDateTime(txtGrantedOn.Text)); <-- this is where error cmd.Parameters.AddWithValue("@status", ddlStatus.Text);
    cmd.Parameters.AddWithValue("@createdby", txtGrantedBy.Text);

    SqlDataAdapter adapter = new SqlDataAdapter(cmd);

    adapter.Fill(SearchResultTable);
    }

    catch (Exception ex)
    {
    Response.Write(ex.ToString());
    }
    finally
    {
    if (con != null)
    {
    con.Close();
    }
    }

    return SearchResultTable;
    }
  • Replied 10 September 2017, 2:48 am EST

    Hello,

    I am really sorry for the delay on this.
    I tried using C1CalendarExtender with the latest version and it worked properly at my end. Could you kindly try with the same:
    http://prerelease.componentone.com/dotnet30/C1WebWijmo.Webforms/2012-T2/

    If the issue persists, then I would request you to share a small sample replicating the issue so that I can check and can assist you accordingly.

    Regards
  • Replied 27 December 2020, 9:23 pm EST

    Use DateTime.ParseExact.- Converts the specified string representation of a date and time to its [url=http://csharp.net-informations.com/language/date.htm]C# DateTime[/url] equivalent using the specified format and culture-specific format information. The format of the string representation must match the specified format exactly. ParseExact and TryParseExact allows to use a custom format string.

    DateTime date = DateTime.ParseExact(strDate, "dd/MM/yyyy", CultureInfo.InvariantCulture);

    The IFormatProvider parameter specifies the culture to use to parse the date. Unless your string comes from the user, you should pass CultureInfo.InvariantCulture. If the string does come from the user, you should pass CultureInfo.CurrentCulture, which will use the settings that the user specified in Regional Options in Control Panel.

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels