OleDbException error
-
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.
-
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.
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.
-
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.
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.
-
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.
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"); }
-
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"); }
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.
-
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.
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
-
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
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.
-
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.
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.
-
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.
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!