Link to home
Start Free TrialLog in
Avatar of 1jaws
1jawsFlag for United States of America

asked on

executeReader or ExecuteScalar?

if I am selecting two values from sql sp like select @updated as update, @message as Message
updated is bit and message is varchar

on cs code can I use executeScalar to see if updated returns 1 or 0 and especially Message reuturns a message  or Do I need to use ExecuteReader
SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of 1jaws

ASKER

ok good explanation angellll :) I have this select @updated as update, @message as Message
 is that means I need ExecuteNonQuery or Reader, not too clear about it.. those two parameters are giving me value like
updated -- 1
message -  my message blah blah.
when I executed sp, which means I need ExecuteNonQuery than?
Avatar of 1jaws

ASKER

I do actually need to use ExecuteReader because I am checking and seeing the values comes from sp which is I am trying to do this and giving me error saying == cannot be using bool to string for this statement
 if (bool.Parse(reader["updated"].ToString()) == "0")
yes, ExecuteReader is what you need for that SELECT.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of 1jaws

ASKER

first one didnt work but second one worked  if (reader["updated"].ToString() == "0") as a snytax but
it is giving me IndexOutOFRangeException {updated}  
dont know why?
are you calling reader.Read() method before reader["updated"] or not ?
we need to see more code for that part...
also what is the type of "updated" field  in your DB is it bit  or string or int?
Avatar of 1jaws

ASKER

I think I know why  on sp I have two other select statement that get executed before the last one with updated and message, so it is looking for and cant find it, but what I need to know to only point out to last select statement I dont know

like first one
Select blah blah    
Select blah
select @updated as update, @message as Message      -->>> I want this one not the other two select statement...
Avatar of 1jaws

ASKER

reder.Read() first comes..
updated is bit
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of 1jaws

ASKER

I have two values at the third select statement which I want third one. dont think can use ExecuteScalar .. I need to do simpliest and easiest way to do that..
hi,
as you have mentioned your select query is
select @updated as update, @message as Message
so what i think is that you gave an allias to @updated variable as update and so you should refer that with update in your if condition so your if statement should be  
 if (reader["update"].ToString() == "0")
Avatar of 1jaws

ASKER

no I gave updated that part is correct...
ok if that so go for the index value index just in case. try this
 if (reader[0].ToString() == "0")
if you are selecting message first. or
 if (reader[1].ToString() == "0") if you are selecting message second.
Avatar of 1jaws

ASKER

my results looks like this when I executed my sp

No column Name
1

ID
12232

updated     message
1                it is updated

as you can see I am getting three different resutls. but when I am reading from cs code I just want third result... Thats all I need.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
instead of
if (reader[0].ToString() == "0")
you can also use the column names like this
if (reader["updated"].ToString() == "0")
since you have ignored the first two results. previously it wasn't working since we were looking at the first result.
Avatar of 1jaws

ASKER

YES it worked, thank you so much guys, I have learned something new!!! I will give points momentarily...
Thanks for sharing your result and i am pleased to help you.
Avatar of 1jaws

ASKER

Thanks All!!!