asp.net - How to display images in datalist from database? -
i have datalist image control aspx code is
<asp:datalist id="datalist1" runat="server" repeatdirection="horizontal" repeatcolumns="4"> <itemtemplate> <div> <asp:image id="image1" imageurl='<%#eval("image") %>' runat="server" height="75px" width="75px" /> </div> </itemtemplate> </asp:datalist>
the code i'm trying images database is
pageddatasource objds = new pageddatasource(); string query = "select * icon"; sqlconnection con = new sqlconnection(); con.connectionstring = system.configuration.configurationmanager.connectionstrings["iconbankconnectionstring"].connectionstring; try { sqlcommand cmd = new sqlcommand(query, con); con.open(); //sqldatareader dr = cmd.executereader(); sqldataadapter da = new sqldataadapter(); da.selectcommand = new sqlcommand(query, con); dataset ds = new dataset(); da.fill(ds, "images"); objds.datasource = ds.tables[0].defaultview; datalist1.datasource = objds; datalist1.databind(); con.close(); } catch (exception) { throw; }
the sql table looks this
icon( id, subcat_id, album_id, subcat_name, album_name, image, icon_size, blackwhite, name, designer, hits, ratings, price)
when debug code shows table rows in dataset when run page not showing me image.... kindly guide me problem behind it. 1 more thing image stored in database in binary format
you setting byte[] or binary data imageurl field, wrong. image url expects path.
check out question , answers :
convert binary data image control in asp.net
on itemdatabound event of datalist, find image control. if you're using html tag set src property, if using , check out answer :
//find image control (image1 lets say) on item databound event of datalist. byte[] myimage = getmyimagefrommydatasource(); string st = server.mappath("myimagenameorid.jpg"); // try name + id make unique filestream fs = new filestream(st, filemode.create, fileaccess.write); fs.write(myimage, 0, myimage.length); fs.close(); image1.imageurl = "myimagenameorid.jpg";
edit : check out link know more using itemdatabound event of datalist :
Comments
Post a Comment