Guest jmt Posted November 30, 2005 Share Posted November 30, 2005 Condtional ImagesHello,I am converting my site from asp to .net. I have a page that I am struggling with. Image names are stored in a database for this page. The page in question queries the database and gets the image name and completes the <img> tag. If the field is empty I want to display a defualt image. Here is how I am doing it with ASP:<%'Display the actual image if existsIf rs.Fields("imagename").ActualSize > 1 Then Response.Write "<img src=images/"Response.Write rs("imagename")Response.Write " width=200" Response.Write " height=142" Response.Write " border=1>"Else Response.Write "<img src=images/comming_soon.gif width=190 height=142 border=1>"End If%>I am having trouble converting it to .net using VB. I am planning on using the asp:repeater control rather that using the record set:<asp:Repeater id="listings" runat="server"><%# Container.DataItem("imagename") %>I have tried a few different things but I seem to be getting nowhere. Does anyone have any advice?ThanksJMT Link to comment Share on other sites More sharing options...
aspnetguy Posted December 1, 2005 Share Posted December 1, 2005 use a data reader.sorry this is in C# thats what I know. And for a SQL Server databaseyou need to use the System.Data.SqlClient and System.Collections namespaces for this example. SqlConnection cnt = new SqlConnection("ConnectionString");SqlCommand cmd = new SqlComamnd("sql statement", cnt);SqlDataReader data = null;ArrayList ImgPath = new ArrayList();cnt.Open();data = cmd.ExecuteReader();while(data.Read()){ ImgPath.Add(data["DatabaseField"].ToString());}data.Close();cnt.Close(); You now have an array of all your img paths, you can add extra ArrayLists based on what data you need.Setup a string and build your images. string Html = "";for(int i=0;i<ImgPath.Count;i++){ Html += "<img src=\"" + ImgPath[i].ToString() + "\" alt=\"\" />";} Now you just output the Html string to the screen however you want. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now