-2
while (rdr.Read())         
{
    imgno = rdr.GetString(0);
}
HttpContext.Current.Response.Write(imgno);          

This code generate the error

(Error 5 Use of unassigned local variable 'imgno')

Dan Lowe
  • 51,713
  • 20
  • 123
  • 112

1 Answers1

2

Presumably you declared the variable above this code like so:

string imgno;
while (rdr.Read())
{
    imgno = rdr.GetString(0);
}
HttpContext.Current.Response.Write(imgno);

The compiler can't guarantee that the loop will ever be entered. Indeed, in any situation where rdr returns no records, the loop would be skipped. In that case, imgno would never be assigned a value. Since the compiler can't guarantee it, the code doesn't compile.

Simply assign a default value to the variable:

string imgno = string.Empty;
David
  • 208,112
  • 36
  • 198
  • 279