views:

340

answers:

4

Hi,

I am getting the following error while running a select statement (using OleDbCommand).

My query is

SELECT CME 
  FROM Personnel 
 WHERE CME = '11349D'

If objOleDbCom.ExecuteScalar() > 0 Then

When i execute the above statement i got this error

Conversion from string "11349D" to type 'Double' is not valid.

My field CME data type is Text

My database is Access 2007

I tried by running my query directly inside database and it is running fine.

Please suggest.

Thanks.

A: 

A double is a number (CME seems to be of that type) while '11249D' is a string. They cannot be compared.

What happens when you try:

SELECT CME 
  FROM Personnel 
 WHERE CME = 11349
lexu
Not going to help if his column is text
pdr
@pdr: He says he's executing the code, so I assume (but I may be wrong), that the type conversion error comes from the DB, not the compiler. Since '11249D' is a string, I assumed CME must be double. But then, since I don't know Access, I might be on the wrong track??
lexu
@lexu: The way I understand it, he says CME is Text and it's failing on the If. That makes sense as ExecuteScalar returns an object (the string "11249D") and it's then trying to compare it to a number (> 0). That said, I'm faintly surprised it even compiles.
pdr
@pdr: I guess we'll need more input from Adnan Badar to find out more!
lexu
@pdr: It is text
Adnan Badar
@pdr, @lexu: I just triedactually i was doing the wrong way to check the existence of a record.i was expecting count of records by running objOleDbCom.ExecuteScalar() and then matching in the if statement which was "If objOleDbCom.ExecuteScalar() > 0 Then"
Adnan Badar
Adnan Badar
A: 

WHERE CME = '11349D'

should be

WHERE CME = 11349

No '

TomTom
+1  A: 

ExecuteScalar simply returns the column 0, row 0 field in the table returned by your sql. I think what you really want is

SELECT COUNT(1) FROM Personnel WHERE CME = '11349D'
pdr
@pdr: this one is correct
Adnan Badar
A: 

I just tried actually i was doing the wrong way to check the existence of a record.

i was expecting count of records by running objOleDbCom.ExecuteScalar() and then matching in the if statement which was

"If objOleDbCom.ExecuteScalar() > 0 Then"

now i am using

Dim _strSelectCME As String = "SELECT CME FROM Personnel WHERE CME = '"
Public Function IsPersonnelExits(ByVal p_strCME As String) As Boolean
Dim objOleDbCom As 
    New OleDbCommand(_strSelectCME & p_strCME & "'"
                     , DBRelated.GetDBConnection()
                    )

Dim objObject As Object = objOleDbCom.ExecuteScalar()
If Not IsNothing(objObject) Then
  Return True    
  Exit Function    
End If
Return False
End Function

Problem solved

Adnan Badar