views:

167

answers:

7

Hi,

I've encountered a very, very strange situation in my application. I say strange because the errors are intermitent and I cannot find out why they occur.

I've pass a sqlcommand to the DB (Sql 2005) trying to get a bigint (column ID_Facultate) from a single table. The problem is that I've got a string (the name of some lecture) that has nothing to do with the table I've tried to access.

Also, when I get these errors, all my application is going crazy.

In the SQL Profiler everything seems fine (the last executing rows seem to be the ones sent by my application)

Here are some links to 3 captured screens during the errors and the code I've used. Please note the Watch1 with the very wrong string value where I expected some integer

Please HELP !!!!!

Catalin

A: 

Can you think of why the idr.FieldCount has a value of 4 in screens 1 and 2 but only 3 in screen 3. Could the problem be resolved by using the column name instead of the ordinal position?

id_faculate = long.Parse(idr["ColumnName"].ToString());
Kane
+1  A: 

From your code I could only think of getting the value using the field name.

rd["ID_Facultate"].ToString()

Although your code seems correct, I always prefer using field names to get the values instead of index position, that can be easily changed in a number of situations. Some of them, maybe like this one, you can't even figure it out.

Nelson Reis
A: 

Since you're only getting 1 value back, you should consider using a SqlCommand.ExecuteScalar instead of getting a DataReader.

Julien Poulin
A: 

I will consider using both the name of the column and/or ExecuteScalar.

Still, the problem is, as seen in the Watch1 that the query returns a value that has nothing to do with that table.

The code was expecting an integer (14) while the DB returned a string... (http://www.unitbv.ro/cata/errors/2.jpg) The same in http://www.unitbv.ro/cata/errors/3.jpg : I was expecting a value from 1 to 20 while the DB/layer seems to return the value 305 which is not in that table...

Anyone?

Catalin

Catalin
Try using SQL Profiler and running a database trace to see what queries are being called and the data being returned. It might help?
Kane
+1 to Kane's comment - you need a breakpoint on your sql query and then take that query and watch it in profiler and check what is coming back from it.
Lance McNearney
The query in the Profiler is the same that is sent by the .net code. The result are, sometimes, strange - they are not from the table the code queried but from other tables... and this is very strange to me...
Catalin
A: 

Anyone ? HELP !!!! S.O.S.!!!

Catalin
A: 

From everything you've told us, nothing seems wrong. It's probably something else for which you didn't give us the have info.

Some other things to try:

  • Run DBCC CHECKDB
  • Ensuring the query returns the correct result in SQL Server Management Studio
  • Try the database on another machine/SQL instance and see if the same problems persist
Mufasa