Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Error: Unable to cast object of type 'System.DateTime' to type 'System.String'.

Error: Unable to cast object of type 'System.DateTime' to type 'System.String'.

Scheduled Pinned Locked Moved C#
databasehelp
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    KIDYA
    wrote on last edited by
    #1

    Hello experts, I got above error,I am trying to do reading data from sql and write it into csv file format.However when it read data row by row from sql then gives above error. My code is: <pre> while (dr.Read()) { strRow = ""; for (int i = 0; i < dr.FieldCount; i++) { strRow += (string)dr.GetString(i); // Gives error here if (i < dr.FieldCount - 1) { strRow += this.separator; } } sw.WriteLine(strRow); }

    M C 2 Replies Last reply
    0
    • K KIDYA

      Hello experts, I got above error,I am trying to do reading data from sql and write it into csv file format.However when it read data row by row from sql then gives above error. My code is: <pre> while (dr.Read()) { strRow = ""; for (int i = 0; i < dr.FieldCount; i++) { strRow += (string)dr.GetString(i); // Gives error here if (i < dr.FieldCount - 1) { strRow += this.separator; } } sw.WriteLine(strRow); }

      M Offline
      M Offline
      Mbah Dhaim
      wrote on last edited by
      #2

      I also had experienced this, so i change the Get[Type]() methods such as GetString(index), GetInt16(index) etc with GetValue(i) or DataReader indexer it self, but it must be checked first if return value is DBNull. In your case replace your code in line that gives error with

      KIDYA wrote:

      strRow += (string)dr.GetString(i); // Gives error here

      if (!Convert.IsDBNull(dr[i]){
      strRow +=dr[i].ToString();
      }

      or

      if (!Convert.IsDBNull(dr.GetValue(i)){
      strRow +=dr.GetValue(i).ToString();
      }

      hope it helps

      dhaim ing ngarso sung tulodho, ing madyo mangun karso, tut wuri handayani. "Ki Hajar Dewantoro" in the front line gave a lead, in the middle line build goodwill, in the behind give power support

      K 1 Reply Last reply
      0
      • K KIDYA

        Hello experts, I got above error,I am trying to do reading data from sql and write it into csv file format.However when it read data row by row from sql then gives above error. My code is: <pre> while (dr.Read()) { strRow = ""; for (int i = 0; i < dr.FieldCount; i++) { strRow += (string)dr.GetString(i); // Gives error here if (i < dr.FieldCount - 1) { strRow += this.separator; } } sw.WriteLine(strRow); }

        C Offline
        C Offline
        Christian Graus
        wrote on last edited by
        #3

        The error means what it says. Can you read ? You can use ToString() if you want to convert the type to a string.

        Christian Graus Driven to the arms of OSX by Vista. Please read this[^] if you don't like the answer I gave to your question.

        1 Reply Last reply
        0
        • M Mbah Dhaim

          I also had experienced this, so i change the Get[Type]() methods such as GetString(index), GetInt16(index) etc with GetValue(i) or DataReader indexer it self, but it must be checked first if return value is DBNull. In your case replace your code in line that gives error with

          KIDYA wrote:

          strRow += (string)dr.GetString(i); // Gives error here

          if (!Convert.IsDBNull(dr[i]){
          strRow +=dr[i].ToString();
          }

          or

          if (!Convert.IsDBNull(dr.GetValue(i)){
          strRow +=dr.GetValue(i).ToString();
          }

          hope it helps

          dhaim ing ngarso sung tulodho, ing madyo mangun karso, tut wuri handayani. "Ki Hajar Dewantoro" in the front line gave a lead, in the middle line build goodwill, in the behind give power support

          K Offline
          K Offline
          KIDYA
          wrote on last edited by
          #4

          hi Mbah Dhaim It works,Thanx.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups