Click here to Skip to main content
15,908,112 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hiii all
i have database which i retieve from it some images according to specific user or employee.
now, i tried a code , but it retieve the last image only.
i want to ask if anyone can help me in retieving all images according a specific user and displa these images???

any suggestions......

thanks
Posted
Comments
Uday P.Singh 31-Aug-11 12:37pm    
what are your table definitions for storing user and images?
moon2011 31-Aug-11 13:16pm    
my table is:
tbl_UserImage(int Id, image userImage, int UserId)

hiii all,
i have tried this code , but it retrieves the last image only.
can anyone tell me how can i make it retrieves all images of an item.
public class ShowImage : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        Int32 empno;

        if (context.Request.QueryString["itemid"] != null)

            empno = Convert.ToInt32(context.Request.QueryString["itemid"]);

        else

            throw new ArgumentException("No parameter specified");



        context.Response.ContentType = "image/jpeg";

        Stream strm = ShowEmpImage(empno);

        byte[] buffer = new byte[4096];

        int byteSeq = strm.Read(buffer, 0, 4096);



        while (byteSeq > 0)
        {

            context.Response.OutputStream.Write(buffer, 0, byteSeq);

            byteSeq = strm.Read(buffer, 0, 4096);

        }

        //context.Response.BinaryWrite(buffer);
    }

    public Stream ShowEmpImage(int empno)
    {

        string conn = ConfigurationManager.ConnectionStrings["sWebCon"].ConnectionString;

        SqlConnection connection = new SqlConnection(conn);

        string sql = "SELECT empimg FROM tbl_Itemimages WHERE itemid = @itemid";

        SqlCommand cmd = new SqlCommand(sql, connection);

        cmd.CommandType = CommandType.Text;

        cmd.Parameters.AddWithValue("@itemid", empno);

        connection.Open();

        object img = cmd.ExecuteScalar();

        try
        {

            return new MemoryStream((byte[])img);

        }

        catch
        {

            return null;

        }

        finally
        {

            connection.Close();

        }

    }


thanks
 
Share this answer
 
It depends on how you are displaying the images. If you use generic handler class (as I do, see A generic Image-From-DB class for ASP.NET[^]) you can use the Request.QueryString to specify which database row ID each displays:
<img alt="" src="ImageFromDb.ashx?RowId=1" />

<img alt="" src="ImageFromDb.ashx?RowId=2" />
 
Share this answer
 
Comments
moon2011 31-Aug-11 13:19pm    
Thanks alot for your help. I am already using Generic Handler , but what i want is to retrieve all the images first and then display them on the page as icons for example .
Using Girdview And bind all image from database its working like following code.

Aspx page code

XML
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <p style="margin-left: 160px">
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
                DataKeyNames="inum" DataSourceID="SqlDataSource1" CellPadding="4"
                ForeColor="#333333" GridLines="None">
                <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                <Columns>
                    <asp:BoundField DataField="inum" HeaderText="inum" InsertVisible="False" ReadOnly="True"
                        SortExpression="inum" />
                    <asp:BoundField DataField="iname" HeaderText="iname" SortExpression="iname" />
                    <asp:TemplateField HeaderText="image">
                    <ItemTemplate>

<asp:image ID="image1" runat="server"
ImageUrl='<%# "Handler.ashx?inum=" + Eval("inum")%>'/>
</ItemTemplate>
</asp:TemplateField>

                </Columns>
                <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <EditRowStyle BackColor="#999999" />
                <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
            </asp:GridView>

            <asp:SqlDataSource ID="SqlDataSource1" runat="server"
                ConnectionString="<%$ ConnectionStrings:SomeDataBase1 %>"
                SelectCommand="SELECT inum, iname, image FROM ft"
                ConflictDetection="CompareAllValues"
                DeleteCommand="DELETE FROM [ft] WHERE [inum] = @original_inum AND (([iname] = @original_iname) OR ([iname] IS NULL AND @original_iname IS NULL)) AND (([image] = @original_image) OR ([image] IS NULL AND @original_image IS NULL))"
                InsertCommand="INSERT INTO [ft] ([inum], [iname], [image]) VALUES (@inum, @iname, @image)"
                OldValuesParameterFormatString="original_{0}"
                UpdateCommand="UPDATE [ft] SET [iname] = @iname, [image] = @image WHERE [inum] = @original_inum AND (([iname] = @original_iname) OR ([iname] IS NULL AND @original_iname IS NULL)) AND (([image] = @original_image) OR ([image] IS NULL AND @original_image IS NULL))">
                <DeleteParameters>
                    <asp:Parameter Name="original_inum" Type="String" />
                    <asp:Parameter Name="original_iname" Type="String" />
                    <asp:Parameter Name="original_image" Type="Object" />
                </DeleteParameters>
                <UpdateParameters>
                    <asp:Parameter Name="iname" Type="String" />
                    <asp:Parameter Name="image" Type="Object" />
                    <asp:Parameter Name="original_inum" Type="String" />
                    <asp:Parameter Name="original_iname" Type="String" />
                    <asp:Parameter Name="original_image" Type="Object" />
                </UpdateParameters>
                <InsertParameters>
                    <asp:Parameter Name="inum" Type="String" />
                    <asp:Parameter Name="iname" Type="String" />
                    <asp:Parameter Name="image" Type="Object" />
                </InsertParameters>
            </asp:SqlDataSource>
        </p>
    </div>
    </form>
</body>
</html>



Code Behind C#

protected void Page_Load(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection();
            string constr = ConfigurationManager.ConnectionStrings["SomeDataBase1"].ToString();
            con = new SqlConnection(constr);
            SqlCommand cmd = new SqlCommand();
                
                con.Open();
                               
                GridView1.DataBind();
                con.Close();
              
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



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