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. OleDbException error

OleDbException error

Scheduled Pinned Locked Moved C#
helpcsharpdatabasevisual-studiotutorial
9 Posts 4 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.
  • T Offline
    T Offline
    theStorminMormon
    wrote on last edited by
    #1

    I'm putting together my first C# database application. It compiles fine and seems to be connecting to my database fine, but when I create the command I get the following error: An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in system.data.dll This is the line VS claims is causing the error: this.cmdGetNewClients = new System.Data.OleDb.OleDbCommand(); The code around it is: this.cn.Open(); this.cmdGetNewClients = new System.Data.OleDb.OleDbCommand(); cmdGetNewClients.CommandText = @"SELECT ClientName FROM Clients"; cmdGetNewClients.Connection = this.cn; OleDbDataReader rdr = cmdGetNewClients.ExecuteReader(); while(rdr.Read()) newClients.Add(rdr["[Client Name]"]); rdr.Close(); cn.Close(); Can anyone tellme why I'm getting this error and how to fix it?\ Thanks! The ends can never justify the means. It is the means that determine the ends.

    S R L 3 Replies Last reply
    0
    • T theStorminMormon

      I'm putting together my first C# database application. It compiles fine and seems to be connecting to my database fine, but when I create the command I get the following error: An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in system.data.dll This is the line VS claims is causing the error: this.cmdGetNewClients = new System.Data.OleDb.OleDbCommand(); The code around it is: this.cn.Open(); this.cmdGetNewClients = new System.Data.OleDb.OleDbCommand(); cmdGetNewClients.CommandText = @"SELECT ClientName FROM Clients"; cmdGetNewClients.Connection = this.cn; OleDbDataReader rdr = cmdGetNewClients.ExecuteReader(); while(rdr.Read()) newClients.Add(rdr["[Client Name]"]); rdr.Close(); cn.Close(); Can anyone tellme why I'm getting this error and how to fix it?\ Thanks! The ends can never justify the means. It is the means that determine the ends.

      S Offline
      S Offline
      squawk
      wrote on last edited by
      #2

      The error is too general. Wrap the code in a try catch block and see what the true error is. If you catch and exception of type SqlException you can try this: foreach(SqlError err in sqlEx.Errors) Console.Writeline(e.Message); Otherwise just check the InnerException. It could be something simple like a login issue.

      T 1 Reply Last reply
      0
      • S squawk

        The error is too general. Wrap the code in a try catch block and see what the true error is. If you catch and exception of type SqlException you can try this: foreach(SqlError err in sqlEx.Errors) Console.Writeline(e.Message); Otherwise just check the InnerException. It could be something simple like a login issue.

        T Offline
        T Offline
        theStorminMormon
        wrote on last edited by
        #3

        Thanks for the advice, can you give me a hand writing the try/catch though? I'm afraid I'm a bit new to C# and rusty in the other stuff I knew in college. it would go something like this: try { this.cmdGetNewClients = new System.Data.OleDb.OleDbCommand(); } catch() { foreach(SqlError err in sqlEx.Errors) console.WriteLine(e.Message); } What goes in the catch()? Thanks so much for helping. The ends can never justify the means. It is the means that determine the ends.

        S 1 Reply Last reply
        0
        • T theStorminMormon

          Thanks for the advice, can you give me a hand writing the try/catch though? I'm afraid I'm a bit new to C# and rusty in the other stuff I knew in college. it would go something like this: try { this.cmdGetNewClients = new System.Data.OleDb.OleDbCommand(); } catch() { foreach(SqlError err in sqlEx.Errors) console.WriteLine(e.Message); } What goes in the catch()? Thanks so much for helping. The ends can never justify the means. It is the means that determine the ends.

          S Offline
          S Offline
          squawk
          wrote on last edited by
          #4

          Try something like this: try { //insert your code here } catch(OleDbException ex) { Console.WriteLine("Message: " + ex.Message + "\n"); if(ex.InnerException != null) Console.WriteLine("Message: " + ex.InnerException.Message + "\n"); foreach(OleDbError e in ex.Errors) Console.WriteLine(e.Message + "\n"); }

          T 1 Reply Last reply
          0
          • S squawk

            Try something like this: try { //insert your code here } catch(OleDbException ex) { Console.WriteLine("Message: " + ex.Message + "\n"); if(ex.InnerException != null) Console.WriteLine("Message: " + ex.InnerException.Message + "\n"); foreach(OleDbError e in ex.Errors) Console.WriteLine(e.Message + "\n"); }

            T Offline
            T Offline
            theStorminMormon
            wrote on last edited by
            #5

            OK, I got that coded in, but it doesn't seem to be actually catching the error. It's highlighting the lind of code inside the try, but it's still saying it's an "unhandled exception" and I'm not getting any console output. What gives now? The ends can never justify the means. It is the means that determine the ends.

            1 Reply Last reply
            0
            • T theStorminMormon

              I'm putting together my first C# database application. It compiles fine and seems to be connecting to my database fine, but when I create the command I get the following error: An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in system.data.dll This is the line VS claims is causing the error: this.cmdGetNewClients = new System.Data.OleDb.OleDbCommand(); The code around it is: this.cn.Open(); this.cmdGetNewClients = new System.Data.OleDb.OleDbCommand(); cmdGetNewClients.CommandText = @"SELECT ClientName FROM Clients"; cmdGetNewClients.Connection = this.cn; OleDbDataReader rdr = cmdGetNewClients.ExecuteReader(); while(rdr.Read()) newClients.Add(rdr["[Client Name]"]); rdr.Close(); cn.Close(); Can anyone tellme why I'm getting this error and how to fix it?\ Thanks! The ends can never justify the means. It is the means that determine the ends.

              R Offline
              R Offline
              Rob Graham
              wrote on last edited by
              #6

              Possibly a typo in your post, but... you select a field named [ClientName] (no space) then try to get the field value from [Client Name] (space btwn Client & Name) no such field exists in the readr, so an exception is thrown. Absolute faith corrupts as absolutely as absolute power Eric Hoffer The opposite of the religious fanatic is not the fanatical atheist but the gentle cynic who cares not whether there is a god or not. Eric Hoffer

              T 1 Reply Last reply
              0
              • R Rob Graham

                Possibly a typo in your post, but... you select a field named [ClientName] (no space) then try to get the field value from [Client Name] (space btwn Client & Name) no such field exists in the readr, so an exception is thrown. Absolute faith corrupts as absolutely as absolute power Eric Hoffer The opposite of the religious fanatic is not the fanatical atheist but the gentle cynic who cares not whether there is a god or not. Eric Hoffer

                T Offline
                T Offline
                theStorminMormon
                wrote on last edited by
                #7

                Thanks for spotting that. It was actually an error in the code (I'd renamed the field and forgotten to update all references to it), but it's not causing the error. The code doesn't even get that far. The ends can never justify the means. It is the means that determine the ends.

                S 1 Reply Last reply
                0
                • T theStorminMormon

                  Thanks for spotting that. It was actually an error in the code (I'd renamed the field and forgotten to update all references to it), but it's not causing the error. The code doesn't even get that far. The ends can never justify the means. It is the means that determine the ends.

                  S Offline
                  S Offline
                  squawk
                  wrote on last edited by
                  #8

                  I took your code and for the most part is worked ok. I filled in some of the blanks and got this code to complie and run: OleDbConnection cn = new OleDbConnection("User ID=xxx;Password=xxx;Initial Catalog=Northwind;Data Source=192.168.0.1;Provider=SQLOLEDB"); cn.Open(); System.Data.OleDb.OleDbCommand cmdGetNewClients = new System.Data.OleDb.OleDbCommand(); cmdGetNewClients.CommandText = @"SELECT LastName FROM Employees"; cmdGetNewClients.Connection = cn; OleDbDataReader rdr = cmdGetNewClients.ExecuteReader(); ArrayList newClients = new ArrayList(); while(rdr.Read()) newClients.Add(rdr["LastName"]); <-- change made here rdr.Close(); cn.Close(); Good luck.

                  1 Reply Last reply
                  0
                  • T theStorminMormon

                    I'm putting together my first C# database application. It compiles fine and seems to be connecting to my database fine, but when I create the command I get the following error: An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in system.data.dll This is the line VS claims is causing the error: this.cmdGetNewClients = new System.Data.OleDb.OleDbCommand(); The code around it is: this.cn.Open(); this.cmdGetNewClients = new System.Data.OleDb.OleDbCommand(); cmdGetNewClients.CommandText = @"SELECT ClientName FROM Clients"; cmdGetNewClients.Connection = this.cn; OleDbDataReader rdr = cmdGetNewClients.ExecuteReader(); while(rdr.Read()) newClients.Add(rdr["[Client Name]"]); rdr.Close(); cn.Close(); Can anyone tellme why I'm getting this error and how to fix it?\ Thanks! The ends can never justify the means. It is the means that determine the ends.

                    L Offline
                    L Offline
                    Luis Alonso Ramos
                    wrote on last edited by
                    #9

                    Maybe you are forgetting to set the connection string in the OleDbConnection object. -- LuisR


                    Luis Alonso Ramos Intelectix - Chihuahua, Mexico Not much here: My CP Blog!

                    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