Click here to Skip to main content
16,016,527 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi

I am designing a page which shows images on it by using grid view.
can you please help me.

my data base field are as follows:

Prod_code  (Primary key,varchar(50))
Prod_category_id(varchar(50))
Prod_name        (varchar(50))
Prod_desc(text)
Prod_image(image)
Price(money)
Status(char(2))


I am using asp.net with c# and sql server as database

can some help me with this problem .

I am able to add the products in my table but not able to retrieve it in grid view.
Posted
Updated 6-Jun-11 3:15am
v2
Comments
senguptaamlan 6-Jun-11 8:50am    
please share your code....
Ra-one 6-Jun-11 9:13am    
Show the code where you are not able to retrieve the data from db

 
Share this answer
 
v2
http://msdn.microsoft.com/en-us/library/aa479350.aspx[^]

Displaying Images from a Database in a GridView[^]
Follow these link .

Second one will give solution of your problem.
 
Share this answer
 
v2
This is the aspx.cs page code
protected void btnadd_Click(object sender, EventArgs e)
    {
        string qu;
        if (fileupload.PostedFile != null &&
            fileupload.PostedFile.FileName != "")
        {
            byte[] imageSize = new byte
                          [fileupload.PostedFile.ContentLength];
            HttpPostedFile uploadedImage = fileupload.PostedFile;
            uploadedImage.InputStream.Read
               (imageSize, 0, (int)fileupload.PostedFile.ContentLength);
            conn.connect();
            qu = "insert into Prod_detail1 values ('" + txtprodid.Text + "','" + txtprodcatid.Text + "','" + txtprodname.Text + "','" + txtproddesc.Text + "',@Image,'" + txtprice.Text + "','" + txtstatus.Text + "')";
            conn.cmd = new System.Data.SqlClient.SqlCommand(qu, conn.con);
            SqlParameter UploadedImage = new SqlParameter("@Image", SqlDbType.Image, imageSize.Length);
                            UploadedImage.Value = imageSize;
                            conn.cmd.Parameters.Add(UploadedImage);
            
            conn.cmd.ExecuteNonQuery();
            fill();
        }
    }
    public void fill()
    {
        conn.con = new SqlConnection(conn.str1);
        conn.ad = new System.Data.SqlClient.SqlDataAdapter("select Prod_code,Prod_cate_id,Prod_name,Prod_desc,Prod_image,Price from Prod_detail1 where Prod_code='" + Request.QueryString["Prod_code"]+"'", conn.con);
        conn.ds = new DataSet();
        conn.ad.Fill(conn.ds);
        GridView1.DataSource = conn.dr;
        GridView1.DataBind();
        conn.con.Close();
        DisplayImage();
    }
    private void DisplayImage()
    {
        if (Request.QueryString["Prod_code"] != null)
        {
           
            conn.con.Open();
            conn.ad = new SqlDataAdapter("select Prod_image from Prod_detail1 where id='" + Request.QueryString["Prod_code"] + "'", conn.con);
            DataTable dt = new DataTable();
            conn.ad.Fill(dt);
            conn.con.Close();
            Byte[] bytes = (Byte[])dt.Rows[0]["Prod_image"];
            Response.BinaryWrite(bytes);
        }
    }

this is the aspx page code

XML
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="add_prod.aspx.cs" Inherits="add_prod" %>

<!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>
    <style type="text/css">
        .style1
        {
            width: 100%;
            height: 50%;
            border: 2px solid #808000;
            background-color: #c0c0c0;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <table cellpadding="2" class="style1">
            <tr>
                <td>
                    Product id:</td>
                <td>
                    <asp:TextBox ID="txtprodid" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    Product category id:</td>
                <td>
                    <asp:TextBox ID="txtprodcatid" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    Product Name:</td>
                <td>
                    <asp:TextBox ID="txtprodname" runat="server" ></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    Product description:</td>
                <td>
                    <asp:TextBox ID="txtproddesc" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    Product image:</td>
                <td>
                    <asp:FileUpload ID="fileupload" runat="server" />&nbsp;</td>
            </tr>
            <tr>
                <td>
                    Price:</td>
                <td>
                    <asp:TextBox ID="txtprice" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    Status:</td>
                <td>
                    <asp:TextBox ID="txtstatus" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    &nbsp;</td>
                <td>
                    <asp:Button ID="btnadd" runat="server" onclick="btnadd_Click"
                        Text="add product" />
                </td>
            </tr>
            <tr>
                <td>
                    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
                        DataKeyNames="Prod_code" DataSourceID="SqlDataSource1">
                        <Columns>
                            <asp:BoundField DataField="Prod_code" HeaderText="Prod_code" ReadOnly="True"
                                SortExpression="Prod_code" />
                            <asp:BoundField DataField="Prod_cate_id" HeaderText="Prod_cate_id"
                                SortExpression="Prod_cate_id" />
                            <asp:BoundField DataField="Prod_name" HeaderText="Prod_name"
                                SortExpression="Prod_name" />
                            <asp:BoundField DataField="Prod_desc" HeaderText="Prod_desc"
                                SortExpression="Prod_desc" />
                               <asp:TemplateField HeaderText="Image">
                               <ItemTemplate>
                               <asp:Image ID="Image1" runat="server"
                                   ImageUrl='<%# Eval("Prod_code", "add_prod.aspx?Prod_code={0}")%>'/>

                               </ItemTemplate>
                               </asp:TemplateField>


                            <asp:BoundField DataField="Price" HeaderText="Price" SortExpression="Price" />
                            <asp:BoundField DataField="Status" HeaderText="Status"
                                SortExpression="Status" />
                        </Columns>
                    </asp:GridView>
                    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
                        ConnectionString="<%$ ConnectionStrings:centurydbConnectionString %>"
                        SelectCommand="SELECT * FROM [Prod_detail1]"></asp:SqlDataSource>
                </td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
        </table>

    </div>
    </form>
</body>
</html>



Now can you advise me something ravi
 
Share this answer
 
v2
Read this article of msdn
http://msdn.microsoft.com/en-us/library/aa479350.aspx[^]

It may help you..............
 
Share this answer
 
hi ravi ,
The link which you provided to me is giving some error/exception
at :
SqlDataReader dr1 = c1.cmd.ExecuteReader();


the full code is as follows:

<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;
using System.Configuration;
using System.Data.SqlClient;
public class Handler : IHttpHandler {
    connection_class c1 = new connection_class();
    public void ProcessRequest (HttpContext context)
    {
        c1.con = new SqlConnection(c1.str1);
        c1.cmd = new SqlCommand();
        c1.cmd.CommandText="select Prod_cate_id,Prod_name,Prod_desc,Prod_image,Price,Status where=Prod_code=@Prod_code";
        c1.cmd.CommandType = System.Data.CommandType.Text;
        c1.cmd.Connection = c1.con;
        SqlParameter imageid = new SqlParameter("@Prod_code",System.Data.SqlDbType.VarChar);
        imageid.Value = context.Request.QueryString["Prod_code"];
        c1.cmd.Parameters.Add(imageid);
        c1.con.Open();
        SqlDataReader dr1 = c1.cmd.ExecuteReader();
        //c1.dr = c1.cmd.ExecuteReader();
        c1.dr.Read();
        context.Response.BinaryWrite((byte[])c1.dr["Prod_image"]);
        c1.dr.Close();
        c1.con.Close();
        //context.Response.ContentType = "text/plain";
        //context.Response.Write("Hello World");
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }
}
 
Share this answer
 
v2

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