Problem while retrieving image bytes from database


i trying store , retrieving images in database .no problem  saving data .but when image .i getting exception says

 invalid attempt read when no data present

   byte[] byts = (byte[])dr["images"]; ---at line

but data exisit in db .  have 2 columns named id , images(varbinary(max))

this code

  using (sqlconnection cnn = new sqlconnection(@"server=.\sqlexpress;database=project;user id=sa;password=1"))              {                  string cmd = "select images images id=1";                  using (sqlcommand kmt = new sqlcommand(cmd, cnn))                  {                      cnn.open();                      sqldatareader dr=kmt.executereader();                      byte[] byts = (byte[])dr["images"];                      memorystream ms = new memorystream(byts);                      image img = image.fromstream(ms);                      picturebox1.image = img;                      }              }


hi,

you have initialize reader, try this

 using (sqlconnection cnn = new sqlconnection(@"server=.\sqlexpress;database=project;user id=sa;password=1"))             {                 string cmd = "select images images id=1";                 using (sqlcommand kmt = new sqlcommand(cmd, cnn))                 {                     cnn.open();                     sqldatareader dr=kmt.executereader(); dr.read()                     byte[] byts = (byte[])dr["images"];                     memorystream ms = new memorystream(byts);                     image img = image.fromstream(ms);                     picturebox1.image = img;                   }             }
regards


if post answers question, please click mark answer on post , vote helpful



Visual Studio Languages  ,  .NET Framework  >  Visual C#



Comments

Popular posts from this blog

Azure DocumentDB Owner resource does not exist

job syspolicy_purge_history job fail in sqlserver 2008

Trying to register with public marketplace error with 'Get-AzureStackStampInformation'