Hello all,
I am stuck with below error i am unable to understand the error. Please help me out.
Conversion failed when converting date and/or time from character string
below is my source code and C# code.
Source code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
ControlToValidate="txtName" ErrorMessage="Enter TaskName">
ControlToValidate="txtdescription" ErrorMessage="Enter description"
ForeColor="Red">
/>
backcolor="#ffffff" width="250px" height="200px"
font-size="12px" font-names="Arial" borderwidth="2px"
bordercolor=175"#000000" nextprevformat="shortmonth"
daynameformat="firsttwoletters" Visible="False" onselectionchanged="cdrCalendar_SelectionChanged"
>
ForeColor="#000055">
ControlToValidate="txtdate" ErrorMessage="Select Date">
C# code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnsave_Click(object sender, EventArgs e)
{
SqlConnection con1 = new SqlConnection(ConfigurationManager.ConnectionStrings["NewTaskConnectionString"].ConnectionString);
try
{
if (con1.State == ConnectionState.Open)
con1.Close();
con1.Open();
string strQuery = "INSERT INTO Task(Taskname,Taskdescription,Assignedto,Taskdate,Starttime,Endtime,Hours,Priority) VALUES('" + txtName.Text + "','" + txtdescription.Text + "','" + Session["Username"].ToString() + "','" + txtdate.Text + "','" + drpdwnhr.SelectedItem.Text.Trim() + "-" + drpdwnmin.SelectedItem.Text.Trim() + "-" + "','" + drpdwnhr.SelectedItem.Text.Trim() + "-" + drpdwnmin.SelectedItem.Text.Trim() + "','" + txthours.Text + "','" + Priority.SelectedValue + "')";
SqlCommand sqlcmd = new SqlCommand(strQuery, con1);
int i = Convert.ToInt32(sqlcmd.ExecuteNonQuery());
con1.Close();
if (i == 0)
{
Response.Write( "Error");
}
else if (i >= 1)
{
Response.Write ("Data Saved Successfully");
}
}
catch (Exception ex)
{
throw ex;
}
}
protected void Calendarbtn_Click(object sender, EventArgs e)
{
try
{
if (txtdate.Text.Trim() != "")
cdrCalendar.SelectedDate = Convert.ToDateTime(txtdate.Text);
}
catch
{ }
cdrCalendar.Visible = true;
}
protected void cdrCalendar_SelectionChanged(object sender, EventArgs e)
{
txtdate.Text = cdrCalendar.SelectedDate.ToShortDateString();
cdrCalendar.Visible = false; //hiding the calendar.
}
}
Thanks,
Laxman
Manish Kumar ChoudharyPosted Jan 23, 2015, 10:18 PM
DateTime result;CultureInfo provider = CultureInfo.InvariantCulture;format = "d";
try {
result = DateTime.ParseExact(dateString, format, provider);
}