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" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Panel ID="popUpPanel" runat="server">
<table frame="border">
<tr>
<td>
<asp:Label ID="lblName" runat="server" Text="Task Name"></asp:Label></td>
<td class="style1">
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="txtName" ErrorMessage="Enter TaskName"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
<asp:Label Id="lblDescription" runat="server" Text="Description"></asp:Label>
</td>
<td class="style1">
<asp:TextBox ID="txtdescription" runat="server" TextMode="MultiLine"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="txtdescription" ErrorMessage="Enter description"
ForeColor="Red"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblAssigned" runat="server" Text="Assign To"></asp:Label>
</td>
<td class="style1">
<asp:TextBox ID="TextBox1" runat="server" ReadOnly="true"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblDate" runat="server" Text="Task Date"></asp:Label>
</td>
<td class="style1">
<asp:TextBox ID="txtdate" runat="server"></asp:TextBox>
<asp:Button ID="Calendarbtn" runat="server" Text="..." CausesValidation="false" onclick="Calendarbtn_Click"
/>
<asp:calendar id="cdrCalendar" runat="server"
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"
>
<TodayDayStyle ForeColor="White" BackColor="Black"></TodayDayStyle>
<NextPrevStyle Font-Size="12px" Font-Bold="True" ForeColor="#333333">
</NextPrevStyle>
<DayHeaderStyle Font-Size="12px" Font-Bold="True"></DayHeaderStyle>
<TitleStyle Font-Size="14px" Font-Bold="True" BorderWidth="2px"
ForeColor="#000055"></TitleStyle>
<OtherMonthDayStyle ForeColor="#CCCCCC"></OtherMonthDayStyle>
</asp:calendar>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
ControlToValidate="txtdate" ErrorMessage="Select Date"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblTaskStart" runat="server" Text="Start Time"></asp:Label></td>
<td class="style1">
<asp:DropDownList ID="drpdwnhr" runat="server">
<asp:ListItem>01</asp:ListItem>
<asp:ListItem>02</asp:ListItem>
<asp:ListItem>03</asp:ListItem>
<asp:ListItem>04</asp:ListItem>
<asp:ListItem>05</asp:ListItem>
<asp:ListItem>06</asp:ListItem>
<asp:ListItem>07</asp:ListItem>
<asp:ListItem>08</asp:ListItem>
<asp:ListItem>09</asp:ListItem>
<asp:ListItem>10</asp:ListItem>
<asp:ListItem>11</asp:ListItem>
<asp:ListItem>12</asp:ListItem>
<asp:ListItem>11</asp:ListItem>
<asp:ListItem>12</asp:ListItem>
<asp:ListItem>13</asp:ListItem>
<asp:ListItem>14</asp:ListItem>
<asp:ListItem>15</asp:ListItem>
<asp:ListItem>16</asp:ListItem>
<asp:ListItem>17</asp:ListItem>
<asp:ListItem>18</asp:ListItem>
<asp:ListItem>19</asp:ListItem>
<asp:ListItem>20</asp:ListItem>
<asp:ListItem>21</asp:ListItem>
<asp:ListItem>22</asp:ListItem>
<asp:ListItem>23</asp:ListItem>
<asp:ListItem>24</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="drpdwnmin" runat="server">
<asp:ListItem>00</asp:ListItem>
<asp:ListItem>15</asp:ListItem>
<asp:ListItem>30</asp:ListItem>
<asp:ListItem>45</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblend" runat="server" Text="End Time"></asp:Label></td>
<td class="style1">
<asp:DropDownList ID="DropDownListhr" runat="server">
<asp:ListItem>01</asp:ListItem>
<asp:ListItem>02</asp:ListItem>
<asp:ListItem>03</asp:ListItem>
<asp:ListItem>04</asp:ListItem>
<asp:ListItem>05</asp:ListItem>
<asp:ListItem>06</asp:ListItem>
<asp:ListItem>07</asp:ListItem>
<asp:ListItem>08</asp:ListItem>
<asp:ListItem>09</asp:ListItem>
<asp:ListItem>10</asp:ListItem>
<asp:ListItem>11</asp:ListItem>
<asp:ListItem>12</asp:ListItem>
<asp:ListItem>11</asp:ListItem>
<asp:ListItem>12</asp:ListItem>
<asp:ListItem>13</asp:ListItem>
<asp:ListItem>14</asp:ListItem>
<asp:ListItem>15</asp:ListItem>
<asp:ListItem>16</asp:ListItem>
<asp:ListItem>17</asp:ListItem>
<asp:ListItem>18</asp:ListItem>
<asp:ListItem>19</asp:ListItem>
<asp:ListItem>20</asp:ListItem>
<asp:ListItem>21</asp:ListItem>
<asp:ListItem>22</asp:ListItem>
<asp:ListItem>23</asp:ListItem>
<asp:ListItem>24</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="DropDownListmin" runat="server">
<asp:ListItem>00</asp:ListItem>
<asp:ListItem>15</asp:ListItem>
<asp:ListItem>30</asp:ListItem>
<asp:ListItem>45</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td>
<asp:Label Id="lblHours" runat="server" Text="Task hours"></asp:Label>
</td>
<td class="style1">
<asp:TextBox ID="txthours" runat="server" ></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label Id="lblPriorty" runat="server" Text="Task Priority"></asp:Label>
</td>
<td class="style1">
<asp:DropDownList ID="Priority" runat="server">
<asp:ListItem></asp:ListItem>
<asp:ListItem>Very High*</asp:ListItem>
<asp:ListItem>High</asp:ListItem>
<asp:ListItem>Medium</asp:ListItem>
<asp:ListItem>Low</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnsave" runat="server" Text="Save" onclick="btnsave_Click" />
</td>
<td class="style1">
<asp:Button ID="btnCancel" runat="server" Text="Cancel" />
</td>
</tr>
</table>
</asp:Panel>
</div>
</form>
</body>
</html>
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