unassigned variable - error in the code of C# -


while (rdr.read())          {     imgno = rdr.getstring(0); } httpcontext.current.response.write(imgno);           

this code generate error

(error 5 use of unassigned local variable 'imgno')

presumably declared variable above code so:

string imgno; while (rdr.read()) {     imgno = rdr.getstring(0); } httpcontext.current.response.write(imgno); 

the compiler can't guarantee loop ever entered. indeed, in situation rdr returns no records, loop skipped. in case, imgno never assigned value. since compiler can't guarantee it, code doesn't compile.

simply assign default value variable:

string imgno = string.empty; 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -