locked
Input string not in correct format RRS feed

  • Question

  • User-797751191 posted

    Hi

      I may be repeating again .

    I have below code . On same server in one database value is getting saved correctly in Sql . But in one database it is giving error , Input string not in a correct Datetime format.

    <script>
    $(function () {
    $(".datepicker").datepicker(
    {
    dateFormat: 'dd-mm-yy'
    });
    });
    </script>

    DateTime dt = DateTime.Parse(lblPDate.Text);
    cmdUpdate.Parameters.AddWithValue("@PDate", dt);

    Thanks

    Thursday, July 18, 2019 3:06 PM

All replies

  • User475983607 posted

    Hi

      I may be repeating again .

    I have below code . On same server in one database value is getting saved correctly in Sql . But in one database it is giving error , Input string not in a correct Datetime format.

    <script>
    $(function () {
    $(".datepicker").datepicker(
    {
    dateFormat: 'dd-mm-yy'
    });
    });
    </script>

    DateTime dt = DateTime.Parse(lblPDate.Text);
    cmdUpdate.Parameters.AddWithValue("@PDate", dt);

    Thanks

    Working example.

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DatepickerDemo.aspx.cs" Inherits="WebFormsDemo.DatepickerDemo" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" />
    
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:TextBox ID="TextBox1" runat="server" CssClass="datepicker"></asp:TextBox>
                <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
            </div>
            <div>
                <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
            </div>
        </form>
    
        <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
        <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
        <script>
            $(function () {
                $(".datepicker").datepicker(
                    {
                        dateFormat: 'dd-mm-yy'
                    });
            });
        </script>
    </body>
    </html>
    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Data.SqlClient;
    using System.Globalization;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace WebFormsDemo
    {
        public partial class DatepickerDemo : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
    
            protected void Button1_Click(object sender, EventArgs e)
            {
                int counter = 0;
                DateTime dt = DateTime.Now;
                DateTime.TryParseExact(TextBox1.Text, "dd-MM-yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out dt);
    
                SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["AdventureWorksLT2008R2ConnectionString"].ConnectionString);
                SqlCommand command = new SqlCommand($@"SELECT [ProductID]
                                                      ,[Name]
                                                      ,[ProductNumber]
                                                      ,[Color]
                                                      ,[StandardCost]
                                                      ,[ListPrice]
                                                      ,[Size]
                                                      ,[Weight]
                                                      ,[ProductCategoryID]
                                                      ,[ProductModelID]
                                                      ,[SellStartDate]
                                                      ,[SellEndDate]
                                                      ,[DiscontinuedDate]
                                                      ,[ThumbNailPhoto]
                                                      ,[ThumbnailPhotoFileName]
                                                      ,[rowguid]
                                                      ,[ModifiedDate]
                                                  FROM [SalesLT].[Product]
                                                  WHERE [ModifiedDate] > @modDate", conn);
    
                command.Parameters.AddWithValue("@modDate", dt);
                conn.Open();
                SqlDataReader reader = command.ExecuteReader();
                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        counter++;
                    }
                    reader.Close();
                }
                conn.Close();
    
                Label1.Text = counter.ToString();
            }
        }
    }

    Thursday, July 18, 2019 4:01 PM