Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8365163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:35:43+00:00 2026-06-09T12:35:43+00:00

I am a new ASP.NET developer and I am facing a problem with inserting

  • 0

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:

enter image description here

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 &rarr;" 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:

enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T12:35:44+00:00Added an answer on June 9, 2026 at 12:35 pm

    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:

    string startDateTime = start_DateTime.ToString();
    string endDateTime = end_DateTime.ToString();
    

    With this:

    string startDateTime = start_DateTime.Text;
    string endDateTime = end_DateTime.Text;
    

    The first code-behind you had (before the update) should work with this change… at least it did for me. 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some internal-facing ASP.NET web services that have had numerous API additions over
I am a brand new Java developer (I have been working in asp.net) and
I have been a web developer for some time now using ASP.NET and C#,
I'm not a WinForms developer, but have been doing ASP.NET for quite some time.
I'm a C# ASP.net MVC Developer. I got a new job and have to
I am a new ASP.NET developer and I have to create a quiz engine
I am a new ASP.NET developer and I have to create a quiz engine
I am a new ASP.NET developer with a good knowledge in Database Design. I
Fist off, I'm very new to ASP.NET and Visual Web Developer Express. I have
I am a new ASP.NET developer and I am developing a web-based suggestions box

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.