Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am Working on My Project. I have Successfully Insert Pictures into the the database but when i want to display this on the web page using Grid View, it displays other details but doesn't display the images.
This is the Code For Inserting Images into the Database.

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

public partial class AddParty : System.Web.UI.Page
{
    DBClass db1 = new DBClass();

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void BtnAddParty_Click1(object sender, EventArgs e)
    {

        int userId = 0;
        string message = string.Empty;
        try
        {
            FileUpload img = (FileUpload)PartyimgUpload;
            FileUpload img2 = (FileUpload)PartyFlagimgUpload;
            Byte[] imgByte = null;
            Byte[] imgByte2 = null;
            if (img.HasFile && img.PostedFile != null)
            {
                //To create a PostedFile
                HttpPostedFile File = PartyimgUpload.PostedFile;
                //Create byte Array with file len
                imgByte = new Byte[File.ContentLength];
                //force the control to load data in array
                File.InputStream.Read(imgByte, 0, File.ContentLength);
            }
            if (img2.HasFile && img2.PostedFile != null)
            {
                //To create a PostedFile
                HttpPostedFile File1 = PartyFlagimgUpload.PostedFile;
                //Create byte Array with file len
                imgByte2 = new Byte[File1.ContentLength];
                //force the control to load data in array
                File1.InputStream.Read(imgByte, 0, File1.ContentLength);
            }

            db1.sqlcmd = new SqlCommand("UspAddParty");
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                db1.sqlcmd.CommandType = CommandType.StoredProcedure;
                db1.sqlcmd.Parameters.AddWithValue("@PartyName", TxtBxPartyName.Text.Trim());
                db1.sqlcmd.Parameters.AddWithValue("@PartyLeader", TxtBxPLeader.Text.Trim());
                db1.sqlcmd.Parameters.AddWithValue("@PartyAddress", TxtBxPAdress.Text.Trim());
                db1.sqlcmd.Parameters.AddWithValue("@PartyLogo", imgByte);
                db1.sqlcmd.Parameters.AddWithValue("@PartyFlag", imgByte2);
                db1.sqlcmd.Parameters.AddWithValue("@PartyDescription", TxtBoxPartyDescription.Text.Trim());
                db1.sqlcmd.Connection = db1.sqlcon;
                db1.sqlcon.Open();
                userId = Convert.ToInt32(db1.sqlcmd.ExecuteScalar());
                //Response.Write(userId);
                //int id = Convert.ToInt32(cmd.ExecuteScalar());
                //lblResult.Text = String.Format("Employee ID is {0}", id);
                //Image1.ImageUrl = "~/ShowImage.ashx?id=" + id;
                switch (userId)
                {
                    case -1:
                        message = "Party Name already exists.\\nPlease choose a different Party Name.";
                        break;
                    default:
                        message = "Registration successful.\\nParty Id: " + userId.ToString();
                        break;
                }
                ClientScript.RegisterStartupScript(GetType(), "alert", "alert('" + message + "');", true);
            }
        }
        catch
        {

        }
        finally
        {
            TxtBxPartyName.Text = null;
            TxtBoxPartyDescription.Text = null;
            db1.sqlcon.Close();
            Server.TransferRequest(Request.Url.AbsolutePath, false);
        }


    }
}

SQL
CREATE PROC UspAddParty 																													
 @PartyName NVARCHAR(30),@PartyLeader Varchar(50),@PartyAddress Varchar(max), @PartyLogo image,@PartyFlag image, @PartyDescription VARCHAR(MAX)
AS
BEGIN
      SET NOCOUNT ON;
      IF EXISTS(SELECT PartyId FROM TblParty WHERE PartyName = @PartyName)
      BEGIN
            SELECT -1 -- Party exists.
      END
      ELSE
      BEGIN

INSERT INTO TblParty (PartyName,PartyLeader,PartyLogo,PartyFlag,PartyAddress,PartyDescription) VALUES(@PartyName,@PartyLeader,@PartyLogo,@PartyFlag,@PartyAddress,@PartyDescription) SELECT @@IDENTITY 
            SELECT SCOPE_IDENTITY() -- PartyId                 
     END
END



Here is the Code For Displaying This Whole data on the Web page.
ASP.NET
<div style="width:96%; height:450px;  margin:0 auto; margin-removed70px; border:3px groove black; overflow: scroll;">

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="4" DataSourceID="SqlDataSource1" ForeColor="Black" GridLines="Horizontal">
        <Columns>
            <asp:BoundField DataField="PartyName" HeaderText="PartyName" SortExpression="PartyName" />
            <asp:BoundField DataField="PartyLeader" HeaderText="PartyLeader" SortExpression="PartyLeader" />
            <asp:BoundField DataField="PartyAddress" HeaderText="PartyAddress" SortExpression="PartyAddress" />
            <asp:BoundField DataField="PartyLogo" HeaderText="PartyLogo" SortExpression="PartyLogo" />
            <asp:BoundField DataField="PartyFlag" HeaderText="PartyFlag" SortExpression="PartyFlag" />
            <asp:BoundField DataField="PartyDescription" HeaderText="PartyDescription" SortExpression="PartyDescription" />
        </Columns>
        <FooterStyle BackColor="#CCCC99" ForeColor="Black" />
        <HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" />
        <SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
        <SortedAscendingCellStyle BackColor="#F7F7F7" />
        <SortedAscendingHeaderStyle BackColor="#4B4B4B" />
        <SortedDescendingCellStyle BackColor="#E5E5E5" />
        <SortedDescendingHeaderStyle BackColor="#242121" />
    </asp:GridView>

    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ECII(Old)ConnectionString %>" SelectCommand="SELECT [PartyName], [PartyLeader], [PartyLogo], [PartyFlag], [PartyAddress], [PartyDescription] FROM [TblParty]"></asp:SqlDataSource>







</div>
Posted
Comments
syed shanu 16-Apr-14 22:28pm    
Chk this link : http://www.codeproject.com/Articles/268123/Display-Store-and-Retrieve-Image-Data-from-Databas
syed shanu 16-Apr-14 22:30pm    
In BoundField how can you display the image chk the above link which has sample program how to store and retrive image from DB

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