c# - Extracting count from where clause -
i have table of movie reviews:
movieid userid rating 72 64817 5 238 4984 4 167 48176 2 37 19841 1 122 987981 2 48 , on.... 129 143 27 177 47 201 220 12 247
if specify particular movieid, know how display userid's rated movie, along rating.
my question: if there no reviews submitted users, how display message saying so? if there no reviews, how capture characteristic? tried many things (working gui in c#):
sql = string.format("select count(1) num reviews movieid = '{0}'", textbox1.text); cmd = new sqlcommand(); cmd.connection = db; cmd.commandtext = sql; object result = cmd.executescalar(); int numreviews = system.convert.toint32(result); if (numreviews == 0) { messagebox.show("no reviews"); } else { //i display them. got part }
if result supposed number (count(1)), why cant assign integer variable , test that?
when debug code, error exception:
conversion failed when converting varchar value 'newmovienoreview' data type int.
on line
result = cmd.executescalar();
update: silly mistake!! after printing in string, movieid= newmovienoreview instead of id! erik "examine in sql after call string.format()"! -resolved
the error
incorrect syntax near ')'.
is sql complaining syntax of sql command.
try
select count(1) num reviews movieid = {0}
note: no single quotes around {0} if movieid integer.
conversion failed when converting varchar value 'newmovienoreview' data type int.
you did not int
result because there error executing sql statement.
note:
rather trusting whatever input in textbox1.text
, should using parameterized query. otherwise, mean user enter textbox ;delete table reviews
.
Comments
Post a Comment