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

  • Home
  • SEARCH
  • 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 9222467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:50:28+00:00 2026-06-18T03:50:28+00:00

I need to store the date fetch from the ASP.Net text box into a

  • 0

I need to store the date fetch from the ASP.Net text box into a SQL Server 2008 date column.

I am getting the following exception message.

Conversion failed when converting date and/or time from character string.

My code is:

SqlConnection cn;
SqlCommand cmd;
SqlDataAdapter da;

int n=4;
protected void Page_Load(object sender, EventArgs e)
{
    cn = new SqlConnection(<Connection String>);
}

protected void Button1_Click(object sender, EventArgs e)
 {
        try
      {
          string query = "insert into temp values('" + TextBox1.Text  + "')";
          cn.Open();
          cmd = new SqlCommand(query, cn);
          cmd.ExecuteNonQuery();
          cn.Close();
          Response.Write("Record inserted successfully");
     }
    catch (Exception ex)
    {
         Response.Write(ex.Message);
    }

}

Please help ..

Thanks in advance

  • 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-18T03:50:29+00:00Added an answer on June 18, 2026 at 3:50 am

    you have to do multiple changes

    1. Convert your date as DateTime
    2. Pass value as parameter to avoid SQL injection
    3. Set the field as DateTime in DB.

    Your code would be like that

    SqlConnection cn;
    SqlCommand cmd;
    SqlDataAdapter da;
    
    int n=4;
    protected void Page_Load(object sender, EventArgs e)
    {
    cn = new SqlConnection(<Connection String>);
    }
    
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
          string query = "insert into temp values(@date))";
          cn.Open();
          cmd = new SqlCommand(query, cn);
          cmd.Parameters.AddWithValue("@date",Convert.DateTime(TextBox1.Text));
          cmd.ExecuteNonQuery();
          cn.Close();
          Response.Write("Record inserted successfully");
        }
        catch (Exception ex)
        {
         Response.Write(ex.Message);
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using MS SQL Server 2005 and I need to store date values partially.
I am developing one web application using asp.net 2005 and Microsoft SQL Server 2008
I am in need of a MySQL column that can store a specific date
I need to store some date in my SQL database, the problem is that
When I need to store time/date information, how is it best to be stored
I need a field for store only a year, not a complete date. Can
I need store just 10 arrays in my app, which I can change from
I need to store a value with Null which is passed from a parameterized
I got a column in which I wanna store date formats that is dd/mm/yy,
I am creating web application where I need to store timestamp/ date. The problem

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.