0

I am working on a ShoppingCart....
I am keeping it simple ....
I have One DropDownList Product_Id whose values r retrived from database....
My Table have Following Fields...
Product_ID
Product_Name
Price
Image

So whenever user seelects a product_id from dropdownlist thats value gets added to the gridview which will display all fields.....

My Code Is As Follows

   SqlConnection con;
    SqlCommand cmd;
    static DataTable dt = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack != true)
        {
            Label1.Text = "";


            dt.Columns.Add("Prod_Id");
            dt.Columns.Add("Prod_Name");
            dt.Columns.Add("Price");
            dt.Columns.Add("Image");
        }

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
            DataRow row;
            int x = int.Parse(DropDownList1.Text);
            con = new SqlConnection();
            con.ConnectionString = "Data Source=HP-HP\\SQLEXPRESS;Initial Catalog=OnlineShop;Persist Security Info=True;User ID=sa;Password=rane@1234";
            con.Open();
            cmd = new SqlCommand("Select * from Product where Prod_Id='" + x + "'", con);
            SqlDataReader dr = cmd.ExecuteReader();

            if (dr.Read())
            {

                DataRow r = dt.NewRow();
                r[0]= dr[0];
                r[1] = dr[1];
                r[2] = dr[2];
                r[3]=dr[3];
               dt.Rows.Add(r);

            }
            GridView1.DataSource = dt;
            GridView1.DataBind();
            con.Close();

    }

i am using PostBack to check wheather the load is fresh or not if its fresh then the user have to fill cart from starting else the products get added to gridview...
Now the problem is that i am not able to retrive image from database insted its showing the path of that image....

3
Contributors
3
Replies
4
Views
5 Years
Discussion Span
Last Post by crazydevelopervish
0

I see that you are doing this code-behind. I wish I could provide you the answer via coding this by had.

If you use the GUI in Visual Studio, and access the properties of the GridView, you'll notice that your "image" column should be that of an ImageField, and then you would assign the source of the image that you have stored in the database to the src of the ImageField.

0

The tricky part is how he stored the image in the database. Did he save the url to the image or did he save the actual image as data type Image in the database. If second scenario, then he would need to create another page lets just call it ImageWrite.aspx . in the form load of that page he would have to write the logic for writing the image using Response.BinaryWrite(image);, i mean after retrieving from the database. Now he can assign the url of the page as the image source to the ImageField of the GridView.

0

I have Table named Product Have Fields as
Prod_ID int
Prod_Name varchar(50)
Price int
Image varchar(50)----save as url

I have form which have one DropDownList Having Boung To Prod_ID Coloum it shows all the data from prod_Id coloumn
and have one gridview...
if user selects prod_ID from dropdown then it should retrive Prod_Name, Price and Image from database for that particular prod_id and display it in Gridview....

Default.aspx looks as follows....

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

<!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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
            DataSourceID="SqlDataSource1" DataTextField="Prod_Id" DataValueField="Prod_Id">
        </asp:DropDownList>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:OnlineShopConnectionString %>" 
            SelectCommand="SELECT [Prod_Id] FROM [Product]"></asp:SqlDataSource>

        <asp:GridView ID="GridView1"
            runat="server">
        </asp:GridView>
    </div>
    </form>
</body>
</html>

and Default.aspx.cs looks as follows....

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        SqlConnection con = new SqlConnection();
        con.ConnectionString = "Data Source=HP-HP\\SQLEXPRESS;Initial Catalog=OnlineShop;Persist Security Info=True;User ID=sa;Password=rane@1234";
        con.Open();
        SqlCommand cmd = new SqlCommand("Select * from Product where Prod_Id='" + DropDownList1.Text + "'", con);
        SqlDataReader dr = cmd.ExecuteReader();

            GridView1.DataSource = dr;
            GridView1.DataBind();

    }

}









    When user selects dropdownlist value it displays the record but instead of image it shows me image url......
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.