locked
How can i get image RRS feed

  • Question

  • User-665519561 posted

    How can i get image datatype from sql-server on an asp.net page.and which is the correct control to show this on the page. 

    Monday, February 2, 2009 11:23 PM

Answers

  • User-1284688060 posted
    using (SqlConnection myConnection = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Inetpub\\wwwroot\\WebserviceUpload2\\App_Data\\Database.mdf;Integrated Security=True;User Instance=True"))
            {
                const string SQL = "SELECT [MIMEType], [Image] FROM [PictureTable] WHERE [ImageID] = @ImageID";
                SqlCommand myCommand = new SqlCommand(SQL, myConnection);
                myCommand.Parameters.AddWithValue("@ImageID", imgid);
                myConnection.Open();
                SqlDataReader myReader = myCommand.ExecuteReader();
                if (myReader.Read())
                {
                    HttpContext.Current.Response.ContentType = myReader["MIMEType"].ToString();
                }
                byte[] imgbyte = (Byte[])myReader["image"];
                myReader.Close();
                myConnection.Close();
                return imgbyte;            
            }
     Try like this..
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 3, 2009 4:07 AM

All replies

  • User297951400 posted
    Defalut.aspx.cs =============== using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient; using System.IO; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { displayImages(); } } protected void btnUpload_Click(object sender, EventArgs e) { int imageSize; string imageType; Stream imageStream; // Gets the Size of the Image imageSize = fileImgUpload.PostedFile.ContentLength; // Gets the Image Type imageType = fileImgUpload.PostedFile.ContentType; // Reads the Image stream imageStream = fileImgUpload.PostedFile.InputStream; byte[] imageContent = new byte[imageSize]; int intStatus; intStatus = imageStream.Read(imageContent, 0, imageSize); // Create Instance of Connection and Command Object SqlConnection myConnection = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]); SqlCommand myCommand = new SqlCommand("insert into tbl_image(img_title,img_stream,img_type) values(@img_title,@img_stream,@img_type)", myConnection); // Mark the Command as a Text myCommand.CommandType = CommandType.Text; // Add Parameters to Command SqlParameter img_title = new SqlParameter("@img_title", SqlDbType.VarChar); img_title.Value = txtImgTitle.Text; myCommand.Parameters.Add(img_title); SqlParameter img_stream = new SqlParameter("@img_stream", SqlDbType.Image); img_stream.Value = imageContent; myCommand.Parameters.Add(img_stream); SqlParameter img_Type = new SqlParameter("@img_type", SqlDbType.VarChar); img_Type.Value = imageType; myCommand.Parameters.Add(img_Type); try { myConnection.Open(); myCommand.ExecuteNonQuery(); myConnection.Close(); Response.Redirect("default.aspx"); } catch (SqlException SQLexc) { Response.Write("Insert Failure. Error Details : " + SQLexc.ToString()); } } public void displayImages() { try { // Create Instance of Connection and Command Object SqlConnection myConnection = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]); myConnection.Open(); SqlCommand myCommand = new SqlCommand("select * from tbl_image", myConnection); // Mark the Command as a Text myCommand.CommandType = CommandType.Text; SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand); DataSet myDataSet = new DataSet(); myAdapter.Fill(myDataSet); GridView1.DataSource = myDataSet; GridView1.DataBind(); myConnection.Close(); } catch (SqlException SQLexc) { Response.Write("Data Retrival Failure. Error Details : " + SQLexc.ToString()); } } public string imageURL(string img_id) { return ("retrieveImages.aspx?id=" + img_id); } } retrieveImages.aspx.cs ====================== using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.IO; using System.Data.SqlClient; public partial class retrieveImages : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { try { string ID = Request.QueryString["id"]; // Create Instance of Connection and Command Object SqlConnection myConnection = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]); myConnection.Open(); SqlCommand myCommand = new SqlCommand("select * from tbl_image where img_id=@img_id", myConnection); // Mark the Command as a Text myCommand.CommandType = CommandType.Text; // Add Parameters to Command SqlParameter img_id = new SqlParameter("@img_id", SqlDbType.Int); img_id.Value = ID; myCommand.Parameters.Add(img_id); SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand); DataSet myDataSet = new DataSet(); myAdapter.Fill(myDataSet); foreach (DataRow dRow in myDataSet.Tables[0].Rows) { Response.ContentType = dRow["img_type"].ToString(); byte[] imageContent = (byte[])((dRow["img_stream"])); Response.BinaryWrite(imageContent); } myConnection.Close(); } catch(SqlException SQLexc) { Response.Write("Data Retrival Failure. Error Details : " + SQLexc.ToString()); } } }
    Tuesday, February 3, 2009 3:32 AM
  • User297951400 posted
    Sorry for the ugly view but how can i send that code nicely???
    Tuesday, February 3, 2009 3:42 AM
  • User-1284688060 posted
    using (SqlConnection myConnection = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Inetpub\\wwwroot\\WebserviceUpload2\\App_Data\\Database.mdf;Integrated Security=True;User Instance=True"))
            {
                const string SQL = "SELECT [MIMEType], [Image] FROM [PictureTable] WHERE [ImageID] = @ImageID";
                SqlCommand myCommand = new SqlCommand(SQL, myConnection);
                myCommand.Parameters.AddWithValue("@ImageID", imgid);
                myConnection.Open();
                SqlDataReader myReader = myCommand.ExecuteReader();
                if (myReader.Read())
                {
                    HttpContext.Current.Response.ContentType = myReader["MIMEType"].ToString();
                }
                byte[] imgbyte = (Byte[])myReader["image"];
                myReader.Close();
                myConnection.Close();
                return imgbyte;            
            }
     Try like this..
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 3, 2009 4:07 AM