1jaws
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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["update d"].ToStri ng()) == "0")
if (bool.Parse(reader["update
yes, ExecuteReader is what you need for that SELECT.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
first one didnt work but second one worked if (reader["updated"].ToStrin g() == "0") as a snytax but
it is giving me IndexOutOFRangeException {updated}
dont know why?
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?
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?
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...
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...
ASKER
reder.Read() first comes..
updated is bit
updated is bit
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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")
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
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.
if (reader[0].ToString() == "0")
if you are selecting message first. or
if (reader[1].ToString() == "0") if you are selecting message second.
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
instead of
if (reader[0].ToString() == "0")
you can also use the column names like this
if (reader["updated"].ToStrin g() == "0")
since you have ignored the first two results. previously it wasn't working since we were looking at the first result.
if (reader[0].ToString() == "0")
you can also use the column names like this
if (reader["updated"].ToStrin
since you have ignored the first two results. previously it wasn't working since we were looking at the first result.
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.
ASKER
Thanks All!!!
ASKER
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?