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 508671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:55:53+00:00 2026-05-13T06:55:53+00:00

when i compile the following code , Conversion failed when converting datetime from character

  • 0

when i compile the following code , “Conversion failed when converting datetime from character string” exception raises , what is wrong with that ?

code :

DateTime after3Dyas = DateTime.Now.AddDays(3);

        try
        {
            Con.Open();
            SqlCommand Command = Con.CreateCommand();
            Command.CommandText = "Select * from Forcast Where City='" + city + "' And Date between '" + DateTime.Now.Date + "' and '" + after3Dyas.Date + "'";

            SqlDataReader thisReader = Command.ExecuteReader();

            int i=0;
            while (thisReader.Read())
            {
                //do something
                i++;

            }

            thisReader.Close();
  • 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-05-13T06:55:53+00:00Added an answer on May 13, 2026 at 6:55 am

    The database is trying to convert the value from whatever DateTime.ToString is giving you… do you really want to trust that .NET on your calling machine and SQL Server use exactly the same format? That sounds brittle to me.

    Avoid this by not putting the value into the SQL directly in the first place – use a parameterized query. This not only avoids conversion issues, but also (equally importantly) avoids SQL injection attacks.

    Sample code:

    DateTime start = DateTime.Now;
    DateTime end = start.AddDays(3);
    string sql = @"
    SELECT * FROM Forecast
    WHERE City = @City AND Date BETWEEN @StartDate AND @EndDate";
    
    // Don't forget to close this somewhere. Why not create a new connection
    // and dispose it?
    Con.Open();
    using (SqlCommand command = new SqlCommand(sql, Con))
    {
        command.Parameters.Add("@City", SqlDbType.NVarChar).Value = city;
        command.Parameters.Add("@StartDate", SqlDbType.DateTime).Value = start;
        command.Parameters.Add("@EndDate", SqlDbType.DateTime).Value = end;
        using (SqlDataReader reader = command.ExecuteReader())
        {
            int i = 0;
            while (reader.Read())
            {
                //do something
                i++;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm attempting to get the following code from nmdepend to compile const std::string Bfd::packageName(const
When trying to compile the following code, I am getting a warning that line
The eclipse compiler refuses to compile the following code, stating that the field s
I got error when I compile the following code, the error tells me that
I have the following code that is to be propperly converted to cython: from
I'm trying to compile the following simple DL library example code from Program-Library-HOWTO with
When I compile the following code, compiler gives me the warning: Implicit conversion loses
When I compile the following code, I only see the error during Run-time which
When I compile the following code with g++, the object of class A seems
I am trying to compile the following code and i am getting the error:

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.