Hello, you miss understood the use of the datareader.The data reader is a class which help you to read results from a query row by row.
So it is what we call connected way to use with databases. After the line with executereader, you must do dr.read(); then dr.get(x) or dr.get ("columnName"); for your help, when you want to get only a number,so when you know that the query you use will return only a value, so a table formed from a row and a column,
you can use better executeScalar instead of others.Execute scalar retyurns an object you can cast using regular casting algoritm so you can do:
int val=(int)cmd.executescalar(...);----- Original Message ----- From: "sameer manohtra" <sameermanohtra@xxxxxxxxx>
To: "programmingblind" <programmingblind@xxxxxxxxxxxxx> Sent: Monday, August 09, 2010 1:47 PM Subject: help required in c# code Hi mates, I am doing a project in c# with ado.net and I got stuck at a place. At one place in the code, i’m reading a column of table of my database and want to store it in a local variable. Basically, the table has a number, which I will use further for some addition/subtraction operation. The problem is, that I’m not able to convert the output of the dataReader into integer. It gives the following exception: --- System.InvalidCastException was unhandled Message="Unable to cast object of type 'System.Data.OleDb.OleDbDataReader' to type 'System.IConvertible'." --- my code is this: --- con.Open(); OleDbCommand cmd = new OleDbCommand("select lastBalance from Textile"); cmd.Connection = con; OleDbDataReader dr= cmd.ExecuteReader(); dr.Close(); con.Close(); int balance = Convert.ToInt32(dr); --- I have tried all that I knew, but could not find a solution. Can you help?Please, tell me how can I convert the output of dataReader to an integer value.
The help will be appreciated. sameer __________ View the list's information and change your settings at //www.freelists.org/list/programmingblind __________View the list's information and change your settings at //www.freelists.org/list/programmingblind