Click here to Skip to main content
14,738,280 members
Please Sign up or sign in to vote.
2.85/5 (4 votes)
See more:
Iam trying to make a shops search engine now i want to upload a image to database. i have databse name Shops it's have two fields Id,ImageName,ImagePath
Help me to Retrive image from database.i dont know how to show image for specific shop id's and shows

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Services.Description;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;


public partial class avatar : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        con = new SqlConnection(ConfigurationManager.ConnectionStrings["VeinConnectionString"].ConnectionString);


        con.Open();
        cmd = new SqlCommand("select * from UserReg where Id", con);
        cmd.Parameters.AddWithValue("@Id", Convert.ToInt32(txtsrno.Text));
        dr = cmd.ExecuteReader();
        if (dr.Read())
        {
            txtname.Text = dr["Name"].ToString();
            Image1.ImageUrl = "~/Image/" + dr["UserImg"].ToString();

        }

        con.Close();




    }
        SqlConnection con;
        SqlCommand cmd;
        SqlDataReader dr;
    protected void save_btn_Click(object sender, EventArgs e)
    {
        con.Open();
        cmd = new SqlCommand("insert into Shops (ImageName,ImagePath) values (@ImageName,@ImagePath)", con);
        cmd.Parameters.AddWithValue("@ImageName", txtname.Text);
        cmd.Parameters.AddWithValue("@ImagePath", imageUpload.FileName);
        imageUpload.SaveAs(Server.MapPath("~/Images/") + imageUpload.FileName);
        cmd.ExecuteNonQuery();
        Message.Visible = true;
        Message.Text = "Add Successfully ";
        con.Close();
        txtname.Text = "";
    }
}
Posted
Comments
saber rezaii magham 19-Apr-14 15:07pm
   
Do you want to save image to database and read it?
Bajpangosh 19-Apr-14 15:22pm
   
yes :)

Start by using valid SQL:
cmd = new SqlCommand("select * from UserReg where Id", con);
Should probably be:
cmd = new SqlCommand("select * from UserReg where Id=@Id", con);
   
use this to save data:

con.Open();
cmd = new SqlCommand("insert into Shops (ImageFile) values (@ImageFile)", con);
System.IO.MemoryStream msGoodSrc = new System.IO.MemoryStream();
pictureBox1.Image.Save(msGoodSrc, pictureBox1.Image.RawFormat);
byte[] arrPicGoodSrc = msGoodSrc.GetBuffer();
msGoodSrc.Close();
cmd.Parameters.Add("@ImageFile", DbType.Binary).Value = arrPicGoodSrc;
cmd.ExecuteNonQuery();
con.Close();


and this is for read image data:

DataTable dt = new DataTable();
con.Open();
cmd = new SqlCommand("select ImageFile from Shops where Id='???'", con);
SQLeDataAdapter dataadapter = new SQLeDataAdapter(cmd);
dataadapter.Fill(dt);
byte[] arrPicture = (byte[])(dt.Rows[0][0]);
System.IO.MemoryStream ms = new System.IO.MemoryStream(arrPicture);
pictureBox1.Image = System.Drawing.Image.FromStream(ms);
ms.Close();
con.Close();
   

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