Click here to Skip to main content
15,885,141 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi, i'm trying to use datepicker but when i insert date selected in datepicker i get error.
here is html code :

XML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.5/themes/base/jquery-ui.css" type="text/css" media="all" />
            <link rel="stylesheet" href="http://static.jquery.com/ui/css/demo-docs-theme/ui.theme.css" type="text/css" media="all" />
            <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
            <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.5/jquery-ui.min.js" type="text/javascript"></script>

   
    <script type="text/javascript">
        $(function () {
            $("#TextBox1").datepicker({
                changeMonth: true,
                changeYear: true
            });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <%--<input runat="server" type="text" id="txtFoo"  />--%>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" /></div>
    <asp:Label ID="Label1" runat="server" ></asp:Label>
    </form>
</body>
</html>


here is code:
C#
namespace datepicker
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        SqlConnection cn = new SqlConnection("Data Source=AB19726;Initial Catalog=datepicker;Integrated Security=True");
        SqlCommand cmd = new SqlCommand();
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            cn.Open();
            //DateTime dat= DateTime.Parse(TextBox1.Text.ToString());
            cmd = new SqlCommand("insert into date (ID,date)values(3," +Convert.ToDateTime(TextBox1.Text.ToString()) + ")", cn);
            cmd.ExecuteNonQuery();
            Label1.Text = "insert successed !!";
            cn.Close();

        }
    }
}


i get this error :

The string was not recognized as a valid DateTime.
Posted
Updated 14-Apr-14 19:01pm
v3
Comments
Rahul VB 15-Apr-14 0:54am    
An important question, people do get this error....
Ajay_Babu 15-Apr-14 3:00am    
i posted an answer which plays good in any type of situation.Just check that once..
youseph 15-Apr-14 8:17am    
plz can give link to your post??
Ajay_Babu 15-Apr-14 8:41am    
sol4 in your question

Try this.
C#
DateTime dat = DateTime.ParseExact(TextBox1.Text.ToString().Trim(), "M/d/yyyy h:mm:ss tt", CultureInfo.InvariantCulture);


Make sure the format and what inside the textbox match.
 
Share this answer
 
Hi,
Try this
DateTime dat = Convert.ToDateTime(TextBox1.Text);

Hope this helps
Cheers
 
Share this answer
 
DateTime dt;
DateTime.TryParseExact(TextBox1.Text,
                       "dd/MM/yyyy",
                       CultureInfo.InvariantCulture,
                       DateTimeStyles.None,
                       out dt);
TextBox1.Text = dt.ToString("yyyy/MM/dd");


It works good in many cases..
 
Share this answer
 
Comments
youseph 15-Apr-14 8:52am    
i try it but not working for me
Ajay_Babu 16-Apr-14 0:48am    
which error u got?
Hi, thnx all i've solved here the code:

dt = DateTime.ParseExact(TextBox1.Text.ToString().Trim(), "M/d/yyyy", CultureInfo.InvariantCulture);
 
Share this answer
 
what datatype did you give in (database) table column?
 
Share this answer
 
Comments
youseph 15-Apr-14 7:48am    
datetime for this datadype

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900