Click here to Skip to main content
15,884,838 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i wanted to insert image in grid view with 4 columns and 5 rows and i got 'bid' error "bid does not exist in the current content" and i also want to know is this code below is correct for populating in gridview as 4 rows and 5 columns

my db quires are : create table myimages(bid varchar(50), pid varchar(50), imageid image)

Thanks in advance for my friends helping all the time and making me learning and thanks so much and i need same support from all the friends here in this forum



imgret.aspx

C#
<asp:GridView ID="GridView1" runat="server" EnableViewState="false" AutoGenerateColumns="false">
            <columns>
                <asp:TemplateField HeaderText="images">
                    <itemtemplate>
                        <img src="ShowImage.ashx?bid=<%# Eval("bid") %>" alt="<%# Eval("pid") %>" />
                    </itemtemplate>
                
                <asp:TemplateField HeaderText="images">
                    <itemtemplate>
                        <img src="ShowImage.ashx?bid=<%# Eval("bid") %>" alt="<%# Eval("pid") %>" />
                    </itemtemplate>
                
                <asp:TemplateField HeaderText="images">
                    <itemtemplate>
                        <img src="ShowImage.ashx?bid=<%# Eval("bid") %>" alt="<%# Eval("pid") %>" />
                    </itemtemplate>
                
                <asp:TemplateField HeaderText="images">
                    <itemtemplate>
                        <img src="ShowImage.ashx?bid=<%# Eval("bid") %>" alt="<%# Eval("pid") %>" />
                    </itemtemplate>
                
            </columns>
        

imgret.aspx.cs

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindFiles();
        }
    }

    /// <summary>
    /// Binds the files.
    /// </summary>
    private void BindFiles()
    {
        DataTable table = new DataTable();

        // get the connection
        SqlConnection _conn1 = new SqlConnection("Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=Northwind");
        {
            // write the sql statement to execute
            string sql = "SELECT imageid FROM myimages Order By bid";
            // instantiate the command object to fire
            using (SqlCommand cmd = new SqlCommand(sql, _conn1))
            {
                // get the adapter object and attach the command object to it
                using (SqlDataAdapter ad = new SqlDataAdapter(cmd))
                {
                    // fire Fill method to fetch the data and fill into DataTable
                    ad.Fill(table);
                }
                // DataAdapter doesn't need open connection, it takes care of opening and closing the database connection
            }
        }
        GridView1.DataSource = table;
        GridView1.DataBind();
    }

ShowImage.ashx

<%@ WebHandler Language="C#" Class="ShowImage" %>

using System;
using System.Web;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
 
public class ShowImage : IHttpHandler 
{
   public void ProcessRequest (HttpContext context)
   {
       if (context.Request.QueryString["bid"] == null) return;

       string connStr = ConfigurationManager.ConnectionStrings["Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=Northwind"].ToString();
       string autoId = context.Request.QueryString["bid"];
       using (SqlConnection conn = new SqlConnection(connStr))
       {
           using (SqlCommand cmd = new SqlCommand("SELECT imageid FROM myimages WHERE bid = @bid", conn))
            {
               cmd.Parameters.Add(new SqlParameter("@bid", bid));
               conn.Open();
               using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                {
                     reader.Read();
                     context.Response.BinaryWrite((Byte[])reader[reader.GetOrdinal("myimages")]);
                     reader.Close();
                }
            }
        }
   }
 
   public bool IsReusable 
   {
       get 
       {
           return true;
       }
    }
    }
Posted
Updated 7-Apr-12 2:17am
v3

your query is for gridview

string sql = "SELECT imageid FROM myimages Order By bid";
....

GridView1.DataSource = table;
GridView1.DataBind();
----------------------------------
And you are using

<![CDATA[<%# Eval("bid") %>

then where is bid
and same for pid
alt="<%# Eval("pid") %>"
----------------------------------------
try this
string sql = "SELECT imageid,bid,pid FROM myimages Order By bid";
 
Share this answer
 
v2
Comments
karthikh87 7-Apr-12 8:37am    
no my dear friend its not working same error again..:-( thanks for replying.. please check this again n resolve this error
karthikh87 7-Apr-12 8:39am    
not working still
string sql = "SELECT imageid,bid,pid FROM myimages Order By bid";
i had not declared bid before using it so it was showing error thanks for you all helping me and the error was at
" string autoId = context.Request.QueryString["bid"]; "

it has to be like this as show below
" string bid = context.Request.QueryString["bid"];"
 
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