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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T04:05:21+00:00 2026-06-08T04:05:21+00:00

Whenever I run this script, I receive the error Conversion failed when converting date

  • 0

Whenever I run this script, I receive the error “Conversion failed when converting date and/or time from character string”

It points to “While (objReader.Read())” section

namespace MedicalDataExporter
{
    public partial class frmSales : Form
    {
        public frmSales()
        {
            InitializeComponent();
        }

        private void dtpFrom_ValueChanged(object sender, EventArgs e)
        {

        }

        private void btnExtract_Click(object sender, EventArgs e)
        {
            SqlConnection objConn = new SqlConnection("Data Source=XXXX;Initial Catalog=YYYY;Persist Security Info=True;User ID=blah;Password=blah");

            // String conStr = Properties.Settings.Default.MedpracConnectionString;
            // System.Data.SqlClient.SqlConnection objConn = new System.Data.SqlClient.SqlConnection(conStr);
            objConn.Open();

            SqlCommand objCmd = new SqlCommand("SELECT CONVERT(char(8), InvDate,3) AS InvDate,InvoiceNo,EmployerCode,TaxAmount + SubTotal AS Amount,'' AS Payment FROM Invoice WHERE (InvDate >= CONVERT(datetime, '"+dtpFrom.Text +"', 3)) AND (InvDate <= CONVERT(datetime, '"+dtpTo.Text+"',3))", objConn);

            SqlDataReader objReader;
            objReader = objCmd.ExecuteReader();

            System.IO.FileStream fs = new System.IO.FileStream("C:\\CMSExportedData\\Sales-" + DateTime.Now.ToString("dd-MM-yy") + ".txt", System.IO.FileMode.Create);
            System.IO.StreamWriter sw = new System.IO.StreamWriter(fs, System.Text.Encoding.Default);

            int count = 0;
            while (objReader.Read())
            {

                for (int i = 0; i < 5; i++)
                {
                    if (!objReader.IsDBNull(i))
                    {
                        string s;
                        s = objReader.GetDataTypeName(i);
                        //MessageBox.Show(s);
                        if (objReader.GetDataTypeName(i) == "char")
                        {
                            sw.Write(objReader.GetString(i));
                        }
                        else if (objReader.GetDataTypeName(i) == "money")
                        {
                            sw.Write(objReader.GetSqlMoney(i).ToString());
                        }
                        else if (objReader.GetDataTypeName(i) == "nvarchar")
                        {
                            sw.Write(objReader.GetString(i));
                        }
                    }
                    if (i < 4)
                    {
                        sw.Write("\t");
                    }
                }
                count = count + 1;

                sw.WriteLine();

            }
            sw.Flush();
            fs.Close();
            objReader.Close();
            objConn.Close();
            MessageBox.Show(count + " records exported successfully.");
            this.Close();
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {
        }
    }
}
  • 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-08T04:05:24+00:00Added an answer on June 8, 2026 at 4:05 am

    This line that converting your string to datetime causes the exception.

       CONVERT(datetime,'"+dtpFrom.Text +"', 3)
    

    change it to this

    CONVERT(datetime,'"+dtpFrom.Text +"', 0)
    

    see datetimeconversion for more info

    test this two query on your sql server:

    SELECT CONVERT(datetime, '11/12/2012', 3) //this causes the exception
    SELECT CONVERT(datetime, '11/12/2012', 0)
    

    On your code:

    SqlCommand objCmd = new SqlCommand("SELECT CONVERT(char(8), InvDate,3) AS InvDate,InvoiceNo,EmployerCode,TaxAmount + SubTotal AS Amount,'' AS Payment FROM Invoice WHERE (InvDate >= CONVERT(datetime, '"+dtpFrom.Text +"', 0)) AND (InvDate <= CONVERT(datetime, '"+dtpTo.Text+"',0))", objConn);
    

    Best Regards

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

Sidebar

Related Questions

I'm trying to run a PHP script, but I keep getting this error whenever
Hey, my script seems to be getting this warning message whenever I run it.
i have written this python program. whenever i run the script using parameters like
Whenever I run this script the find part executes but the if statement causes
Whenever i try to run sqlmetal, i get this: 'sqlmetal' is not recognized as
I am facing this issue whenever I am trying to run Play 1.2.1 in
Thanks in advance. This is my sample code. Whenever i try to run it
I am trying to run an andriod application from emulator , but whenever i
myfile = open('out.txt', 'a') myfile.write(caption) I write something like this whenever my script is
Whenever I run my script a temp file gets created in my local directory:

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.