Click here to Skip to main content
15,895,656 members

Add image path dynamically to img tag in html by fetching image url from database..?

Member 9877729 asked:

Open original thread
hi,
Can i add images dynamically from database in img tag without using any upload button,i want to load images in img tag on page_load, so how can i do..? suppose there are 3 images in database and they must be added on img tag respectively while page will load...
I have code like...

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="div.aspx.cs" Inherits="User_Images_div" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Data" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <link href="Styles/StyleSheet.css" rel="stylesheet" type="text/css" />

    <script runat="server">
        SqlConnection cn = new SqlConnection("Data Source=COMPUTER;Initial Catalog=demo_page;Integrated Security=True");
        SqlCommand cmd;
        SqlDataReader dr;
        DataSet ds;
        SqlDataAdapter adp;
        int j;
        static string str;
        public int getCount()
        {
            cn.Open();
            string qry = "select count(id) from Image";
            cmd = new SqlCommand(qry, cn);
            dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                j = (int)dr[0];
            }
            cn.Close();
            return j;
        }

        public string getImgUrl()
        {
            cn.Open();
            cmd = new SqlCommand("select img_url from Image",cn);
            string qry = "select img_url from Image";
            adp = new SqlDataAdapter(qry, cn);
            ds = new DataSet();
            adp.Fill(ds);
            for (int i = 0; i < getCount(); i++)
            {
                str = ds.Tables[0].Rows[i]["img_url"].ToString();
            }
           
            cn.Close();
            return str;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
   <%  int c = getCount(); %>
        <table cellpadding="0" cellspacing="0" style="width: 100%">
       <%for (int i = 0; i < c; i++){
       %>
       <tr>
       <%    for (int j = 0; j < 3; j++)
          { str = getImgUrl();
       %>
           <td>
            <div class="prod_box">
                    <div class="top_prod_box"></div>
                    <div class="center_prod_box">
                    <div class="product_title"></div>
                    <div class="product_img">
                    <asp:Image ID="Image1" ImageUrl='<%=str %>' Width="146px" Height="73px" runat="server" />
                    <%--<img src=<%#str%> alt="" />--%></div></div>
                    <div class="bottom_prod_box"></div>
                    <div class="prod_details_tab" align="center"></div>
        </div>
        </td>
    <% } %>
    </tr>
    <% } %>
    </table>
    </form>
</body>
</html>


plz help me..
thank u..........
Tags: C#, HTML, ASP.NET

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900