Error in the Function

  • Thread starter Thread starter Sats
  • Start date Start date
S

Sats

I am trying to write a function to check whether the selected value
(thru SQL query)is avalable in table or not,but every time in the
below function, CheckValue is becoming true.Can any one help me where
am I going wrong.

Private Function CheckValue() As Boolean

Dim cn As SqlCeConnection
Dim cmd As SqlCeCommand
Dim dr As SqlCeDataReader
Const strLocalConnect As String = "Data
Source=\Windows\sql.sdf"
cn = New SqlCeConnection(strLocalConnect)
cn.Open()
Dim CheckSql As String
CheckSql = "Select Assetid FROM Asset WHERE Assetid ='" &
VarValue & "'"
'MsgBox(CheckSql)
cmd = New SqlCeCommand(CheckSql, cn)
dr = cmd.ExecuteReader()
dr.Read()

If dr.IsClosed = False Then
CheckValue= True
Else
CheckValue= False
End If

cn.Close()
dr = Nothing

End Function
 
Wouldn't it make more sense to check for a non-zero number of result rows?
Maybe something like this:

-----
SqlCommand cmd = new SqlCommand( theQueryText, sqlconnect );

DataSet ds = new DataSet();

SqlDataAdapter adapter = new SqlDataAdapter();

adapter.SelectCommand = cmd;

adapter.Fill(ds);

DataTable dt = ds.Tables[ 0 ];

bool res;

if ( dt.Rows > 0 )

res = true;

else

res = false;

return res;
 
Back
Top