You must Sign In to post a response.
  • Category: ASP.NET

    Can't to Able to Display Image

    Hi Developers,

    I am try to display image, there is no error on my code . but the image only does not displaying.
    Below is am adding my Source code.

    1 . Aspx Code :

    <div>
    <asp:DataList ID="DataList1" runat="server" BackColor="Gray" BorderColor="#666666"
    BorderStyle="None" BorderWidth="2px" CellPadding="3" CellSpacing="2"
    Font-Names="Verdana" Font-Size="Small" GridLines="Both" RepeatColumns="3" RepeatDirection="Horizontal"
    Width="600px">
    <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
    <HeaderStyle BackColor="#333333" Font-Bold="True" Font-Size="Large" ForeColor="White"
    HorizontalAlign="Center" VerticalAlign="Middle" />
    <HeaderTemplate>
    Employee Details</HeaderTemplate>
    <ItemStyle BackColor="White" ForeColor="Black" BorderWidth="2px" />
    <ItemTemplate>
    <asp:Image ID="imgEmp" runat="server" Width="100px" Height="120px" ImageUrl='<%# Bind("File") %>'
    style="padding-left:40px"/><br />

    </ItemTemplate>
    </asp:DataList>
    </div>

    2. C# Code

    protected void BindData()
    {
    OleDbConnection conn = Connection.DBconnection();
    DataSet ds = new DataSet();
    string cmdstr = "Select * from tbl_addnews where ststus='A'";
    OleDbCommand cmd = new OleDbCommand(cmdstr, conn);
    OleDbDataAdapter adp = new OleDbDataAdapter(cmd);
    adp.Fill(ds);
    DataList1.DataSource = ds.Tables[0];
    DataList1.DataBind();
    }

    3. My Image inserted Code:

    string path = Server.MapPath("~/");
    string phy_path = "http://localhost:1570/STEELS%20GLOBEL%2023-04-2016/Admin/Add_News.aspx";
    string FileUpload1_path = "";
    string File1 = "";
    if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName != "")
    {
    FileUpload1_path = "/FileUpload1/" + FileUpload1.PostedFile.FileName + getExtension(FileUpload1.FileName);
    File1 = phy_path + "/FileUpload1/" + FileUpload1.PostedFile.FileName + getExtension(FileUpload1.FileName);
    FileUpload1.SaveAs(path + FileUpload1_path);
    }
    OleDbConnection con = Connection.DBconnection();
    int i = objadd.insert_news(txtcourse.Text.Trim().ToString(), File1);
    if (i > 0)
    {
    lblmessage.Text = "Inserted Successfully";
    cgv.EditIndex = -1;
    bindgrid();
    }

    If I make any mistake or have to do any changes in my Source code please guide me.

    Thanking You
    Paul.S
  • #765630
    Here is the simple basic sample that add images in datalist, try it at your end

    < asp:DataList ID="DataList1" runat="server" RepeatColumns = "2" CellPadding = "4">
    < ItemTemplate>
    < table border="0" cellpadding="0" cellspacing="0" width = "120px">
    < tr>
    < td align = "center">
    < asp:Image ID="Image1" ImageUrl='<%# Eval("Value") %>' runat="server" Height="100" Width="100" />
    < /td>
    < /tr>
    < tr>
    < td align = "center">
    < %# Eval("Text") %>
    < /td>
    < /tr>
    < /table>
    < /ItemTemplate>
    < /asp:DataList>

    //in .cs file
    using System.IO;
    using System.Collections.Generic;

    //in page load
    if (!IsPostBack)
    {
    string[] filePaths = Directory.GetFiles(Server.MapPath("~/Images/"));
    List<ListItem> files = new List<ListItem>();
    foreach (string filePath in filePaths)
    {
    string fileName = Path.GetFileName(filePath);
    files.Add(new ListItem(fileName, "~/Images/" + fileName));
    }
    DataList1.DataSource = files;
    DataList1.DataBind();
    }

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #765631
    Thank you Mr.Prasad

    You are Absolutely Correct Mr. Prasad .
    But i want to get the images from tha database only.
    also i need to bind Selected images only

    Ex. Select * from tbl_images where status='S' - Like this query.

    Paul.S

  • #765652
    1. Get all the records from the database and bind it in the data list.
    2. Create ashx for handle the each image.
    3. In the asxh page read the image by getting the image id from as query string
    4. Return the image as image bites.
    2. Bind your image as follows in the main
    < asp:Image ID="Image1" ImageUrl='Image.asxh?ImageID<%# Eval("Value") %>' runat="server" Height="100" Width="100" />

    The above is the correct way to bind you image in you data list.

    By Nathan
    Direction is important than speed

  • #765705
    Hi,

    if you want to bind images into your gridview control then refer below sample code,

    protected void Bind_GV()
    {
    cmd = new SqlCommand("Get_Employees", con);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@DeptNo", DeptNo);
    dt = new DataTable();

    try
    {
    con.Open();
    da = new SqlDataAdapter(cmd);
    da.Fill(dt);
    if (dt.Rows.Count > 0)
    {
    GV.DataSource = dt;
    GV.DataBind();
    }
    else
    {
    DataRow dr = dt.NewRow();

    dt.Rows.Add(dr);
    dt.AcceptChanges();
    GV.DataSource = dt;
    GV.DataBind();
    GV.Rows[0].Visible = false;
    }
    if (num == 0)
    {
    int n = dt.Rows.Count;
    for (int i = 0; i < n; i++)
    {
    //fetch image from DataBase
    string file = dt.Rows[i][7].ToString();
    //give specific path of the image
    string path = "Image/" + file;
    //find image control from Gridview
    Image img = (Image)GV.Rows[i].FindControl("img");
    // assign path to image
    img.ImageUrl = path;
    }
    num = 1;
    }
    }
    catch (Exception ex)
    {

    }
    finally
    {
    con.Close();
    con.Dispose();
    }
    }


    Refer below link for more details about how to save, get back images...
    http://www.dotnetspider.com/resources/45091-Insert-Edit-Update-Delete-Get-back-Image-from-DataBase.aspx

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/


Sign In to post your comments