I am a new ASP.NET developer and I am facing a problem with inserting some data to the database. I have a form for creating Event. The filled data should be inserted to the database. I have the following database design:
Columns: ID, Title, Description, StartDateTime, EndDateTime, IsActive
(StartDateTime and EndDateTime are DateTime data types)
When I tried to insert data, I faced the following problem and I don’t know why:

My code-behind (C#):
protected void submitButton_Click(object sender, EventArgs e)
{
string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=RegistrationSysDB;Integrated Security=True;";
string insertCommand = "INSERT INTO Events (Title, Description, Location, StartDateTime, EndDateTime) values (@Title, @Description, @Location, @StartDateTime, @EndDateTime)";
string title = txtTitle.Text;
string description = txtDescription.Text;
string location = txtLocation.Text;
string startDateTime = start_DateTime.ToString();
string endDateTime = end_DateTime.ToString();
using(SqlConnection conn = new SqlConnection(connString))
{
//open DB Connection
conn.Open();
using (SqlCommand cmd = new SqlCommand(insertCommand, conn))
{
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@Title", title);
cmd.Parameters.AddWithValue("@Description", description);
cmd.Parameters.AddWithValue("@Location", location);
cmd.Parameters.AddWithValue("@StartDateTime", startDateTime);
cmd.Parameters.AddWithValue("@EndDateTime", endDateTime);
cmd.ExecuteNonQuery();
}
conn.Close();
}
MultiView1.SetActiveView(ViewConfirm);
}
My ASP.NET code:
<div id="contactform">
<fieldset>
<label for="title">
Title</label>
<asp:TextBox ID="txtTitle" CssClass="text-input" runat="server"></asp:TextBox>
<br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Please enter a subject for your message"
ControlToValidate="txtTitle"></asp:RequiredFieldValidator>
<label for="description">
Description</label>
<asp:TextBox ID="txtDescription" TextMode="MultiLine" CssClass="textarea" Rows="6" cols="50"
runat="server"></asp:TextBox>
<br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Please enter your message"
ControlToValidate="txtDescription"></asp:RequiredFieldValidator>
<label for="location">
Location</label>
<asp:TextBox ID="txtLocation" CssClass="text-input" runat="server"></asp:TextBox>
<br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ErrorMessage="Please enter your message"
ControlToValidate="txtLocation"></asp:RequiredFieldValidator>
<label for="start_DateTime">
Start Date & Time</label>
<asp:TextBox ID="start_DateTime" CssClass="textarea" runat="server"></asp:TextBox>
<%--<ajaxToolkit:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="start_DateTime"
PopupPosition="Right">
</ajaxToolkit:CalendarExtender>--%>
<br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="Please enter your message"
ControlToValidate="start_DateTime"></asp:RequiredFieldValidator>
<label for="end_DateTime">
End Date & Time</label>
<asp:TextBox ID="end_DateTime" CssClass="textarea" runat="server"></asp:TextBox>
<%--<ajaxToolkit:CalendarExtender ID="CalendarExtender2" runat="server" TargetControlID="end_DateTime"
Format=" PopupPosition="Right">
</ajaxToolkit:CalendarExtender>--%>
<br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ErrorMessage="Please enter your message"
ControlToValidate="end_DateTime"></asp:RequiredFieldValidator>
<asp:Button ID="submitButton" runat="server" CssClass="button"
Text="Create →" onclick="submitButton_Click" />
</fieldset>
</div>
FYI, I am using jQuery UI DateTimePicker for inserting the StartDateTime and EndDateTime.
So how can I insert the data with DateTime type into the database?
UPDATE #1:
I changed my code to the following:
protected void submitButton_Click(object sender, EventArgs e)
{
string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=RegistrationSysDB;Integrated Security=True;";
string insertCommand = "INSERT INTO Events (Title, Description, Location, StartDateTime, EndDateTime) values (@Title, @Description, @Location, @StartDateTime, @EndDateTime)";
string title = txtTitle.Text;
string description = txtDescription.Text;
string location = txtLocation.Text;
string startDateTime = start_DateTime.ToString();
string endDateTime = end_DateTime.ToString();
using(SqlConnection conn = new SqlConnection(connString))
{
//open DB Connection
conn.Open();
using (SqlCommand cmd = new SqlCommand(insertCommand, conn))
{
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@Title", title);
cmd.Parameters.AddWithValue("@Description", description);
cmd.Parameters.AddWithValue("@Location", location);
cmd.Parameters.AddWithValue("@StartDateTime", "'" + startDateTime + "'");
cmd.Parameters.AddWithValue("@StartDateTime", "'" + endDateTime + "'");
cmd.ExecuteNonQuery();
}
conn.Close();
}
MultiView1.SetActiveView(ViewConfirm);
}
and I got the following error:

You’re calling ToString on the textbox instead of retrieving the text from it. So the value that is getting passed to the database is the string
"System.Web.UI.WebControls.TextBox"instead of the actual date/time.Try replacing this:
With this:
The first code-behind you had (before the update) should work with this change… at least it did for me. 🙂