public List <SubletImage> GetSubletImages(string SubId) { SqlConnection con = null; List <SubletImage> sil = new List <SubletImage>(); try { con = connect("ConnectionStringName"); // create a connection to the database using the connection String defined in the web config file String selectSTR = "SELECT * FROM SubImgTbl WHERE " + SubId + ";"; SqlCommand cmd = new SqlCommand(selectSTR, con); // get a reader SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); // CommandBehavior.CloseConnection: the connection will be closed after reading has reached the end while (dr.Read()) { // Read till the end of the data into a row SubletImage si = new SubletImage(); si.SubletID = Convert.ToInt32(dr["SubletID"]); si.ImagePath1 = (string)dr["ImgPath1"]; si.ImagePath2 = (string)dr["ImgPath2"]; si.ImagePath3 = (string)dr["ImgPath3"]; si.ImagePath4 = (string)dr["ImgPath4"]; si.ImagePath5 = (string)dr["ImgPath5"]; sil.Add(si); } return(sil); } catch (Exception ex) { // write to log throw (ex); } finally { if (con != null) { con.Close(); } } }
// GET api/<controller>/5 public List <SubletImage> Get(string SubId) { SubletImage sl = new SubletImage(); return(sl.GetSubletImages(SubId)); }