locked
Problem with retrieving images from database RRS feed

  • Question

  • User1407015516 posted

    Error: DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'id'.

    Source Error:
     <asp:Image ID="Image1" runat="server" ImageUrl='<%# "ImageHandler.ashx?ImID="+ Eval("id") %>' Height="150px" Width="150px"/>
    
    public class ImageHandler : IHttpHandler {
        
        public void ProcessRequest (HttpContext context) {
            string id = context.Request.QueryString["ImID"];
            SqlConnection connection = new SqlConnection("Data Source=STD-258E51EA446\\SQLEXPRESS;Initial Catalog=images;Integrated Security=True");
            connection.Open();
            SqlCommand command = new SqlCommand("select Image from Imagess where id=" + id, connection);
            SqlDataReader dr = command.ExecuteReader();
            dr.Read();
            context.Response.BinaryWrite((Byte[])dr[0]);
            connection.Close();
            context.Response.End();
        }
    
    private void BindGridData()
        {
            SqlConnection connection = new SqlConnection("Data Source=STD-258E51EA446\\SQLEXPRESS;Initial Catalog=images;Integrated Security=True");
            SqlCommand command = new SqlCommand("SELECT imagename,image from imagess", connection);
            SqlDataAdapter daimages = new SqlDataAdapter(command);
            DataTable dt = new DataTable();
            daimages.Fill(dt);
            GridView1.DataSource = dt;
            GridView1.DataBind();
            GridView1.Attributes.Add("bordercolor", "black");
        }
        protected void btnUpload_Click(object sender, EventArgs e)
        {
              
            if (fileuploadImage.HasFile)
            {
                
                int length = fileuploadImage.PostedFile.ContentLength;
                byte[] imgbyte = new byte[length];
                HttpPostedFile img = fileuploadImage.PostedFile;
                img.InputStream.Read(imgbyte, 0, length);
                string imagename = txtImageName.Text;
                SqlConnection connection = new SqlConnection("Data Source=STD-258E51EA446\\SQLEXPRESS;Initial Catalog=images;Integrated Security=True");
                connection.Open();
                SqlCommand cmd = new SqlCommand("INSERT INTO Imagess (imagename,image) VALUES (@imagename,@image)", connection);
                cmd.Parameters.Add("@imagename", SqlDbType.VarChar, 50).Value = imagename;
                cmd.Parameters.Add("@image", SqlDbType.Image).Value = imgbyte;
                int count = cmd.ExecuteNonQuery();
                connection.Close();
                if (count == 1)
                {
                    BindGridData();
                    txtImageName.Text = string.Empty;
                    //scriptmanager .RegisterStartupScript(this, this.GetType(), "alertmessage", "javascript:alert('" + imagename + " image inserted successfully')", true);
                }
            }
        }
    Friday, February 3, 2012 1:51 AM

Answers

  • User-68639941 posted

    hi, include id in select statement like below

    private void BindGridData()
        {
            SqlConnection connection = new SqlConnection("Data Source=STD-258E51EA446\\SQLEXPRESS;Initial Catalog=images;Integrated Security=True");
           SqlCommand command = new SqlCommand("SELECT id, imagename,image from imagess", connection);
            SqlDataAdapter daimages = new SqlDataAdapter(command);
            DataTable dt = new DataTable();
            daimages.Fill(dt);
            GridView1.DataSource = dt;
            GridView1.DataBind();
            GridView1.Attributes.Add("bordercolor", "black");
        }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 3, 2012 1:58 AM

All replies

  • User-68639941 posted

    hi, include id in select statement like below

    private void BindGridData()
        {
            SqlConnection connection = new SqlConnection("Data Source=STD-258E51EA446\\SQLEXPRESS;Initial Catalog=images;Integrated Security=True");
           SqlCommand command = new SqlCommand("SELECT id, imagename,image from imagess", connection);
            SqlDataAdapter daimages = new SqlDataAdapter(command);
            DataTable dt = new DataTable();
            daimages.Fill(dt);
            GridView1.DataSource = dt;
            GridView1.DataBind();
            GridView1.Attributes.Add("bordercolor", "black");
        }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 3, 2012 1:58 AM
  • User1407015516 posted

    Error:    The name 'Scriptmanager' does not exist in the current context    in btn click event. how to make that work?

    Friday, February 3, 2012 2:23 AM
  • User-1466490582 posted

    Are u using Update Panel ?

    if yes then place a Scriptmanager inside the form 

    Friday, February 3, 2012 2:34 AM
  • User1407015516 posted

    no i'm not using..

    Friday, February 3, 2012 2:38 AM