C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Forums » .NET » ASP.NET »

image retrieve problem urgent


Posted Date: 12 Aug 2008      Posted By: Thirumal Rao      Member Level: Silver     Points: 1   Responses: 4



HI
I WANT WITHOUT using Handler.ashx code

Here am write strem function for retrieve image from database.here am pass the imgid from texbox1.text.

and create object for comvert image into byte.but its not exucute
please tell me


publc strem imge()

{

connection = new SqlConnection(conn);
if (connection.State==ConnectionState.Open)
connection.Close();


connection.Open();
string query = "select empimg from empdetails where empid='" + TextBox1.Text + "'";
SqlCommand cmd1 = new SqlCommand(query, connection);
cmd1.CommandType = CommandType.Text;
object im = cmd1.ExecuteScalar();
try
{
return new MemoryStream((byte[])im);

}
catch(Exception ex)
{
Response.Write(ex.Message);
return null;
}
finally
{
connection.Close();
}



ERROR MESSAGE:ex.Message = "Buffer cannot be null.\r\nParameter name: buffer"














Regards
Thirumal Rao.D





Responses

Author: Deepa    12 Aug 2008Member Level: DiamondRating: 2 out of 52 out of 5     Points: 6

hi Thirumal,
here is a code m using and its working fine hope this ll help u

string id;
id = EmpNum.Text;
//lblResult.Text = String.Format("Employee ID is {0}", id);
Image1.ImageUrl = "~/ShowImage.ashx?id=" + id;
------------------
ShowImage.ashx

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

using System;
using System.Configuration;
using System.Web;
using System.IO;
using System.Data;
using System.Data.SqlClient;

public class ShowImage : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
string empno;
if (context.Request.QueryString["id"] != null)
empno = Convert.ToString(context.Request.QueryString["id"]);
//empno = Convert.ToInt32(context.Request.QueryString["id"]);
else
throw new ArgumentException("No parameter specified");

context.Response.ContentType = "image/jpg";
Stream strm = ShowEmpImage(empno);
byte[] buffer = new byte[4096];
int byteSeq = strm.Read(buffer, 0, 4096);

while (byteSeq > 0)
{
context.Response.OutputStream.Write(buffer, 0, byteSeq);
byteSeq = strm.Read(buffer, 0, 4096);
}
//context.Response.BinaryWrite(buffer);
}

public Stream ShowEmpImage(string empno)
{
string conn = ConfigurationManager.AppSettings["strConn"];
SqlConnection connection = new SqlConnection(conn);
string sql = "SELECT EmployeeImage FROM Employee WHERE EmployeeNumber = @EmployeeNumber";
SqlCommand cmd = new SqlCommand(sql, connection);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@EmployeeNumber", empno);
connection.Open();
object img = cmd.ExecuteScalar();
try
{
return new MemoryStream((byte[])img);
}
catch
{
return null;
}
finally
{
connection.Close();
}
}

public bool IsReusable
{
get
{
return false;
}
}


}



Author: Rathi    12 Aug 2008Member Level: GoldRating: 2 out of 52 out of 5     Points: 6

hai

try this

using handler page
----------
Imports System
Imports System.Web
Imports System.Data.SqlClient
Imports System.Data
Public Class showHandler : Implements IHttpHandler

Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
Dim con As String = ConfigurationManager.ConnectionStrings("ExampleConnection").ConnectionString
Dim connection As SqlConnection = New SqlConnection(con)
connection.Open()
Dim sql As String = "Select Image from art where AId=@ImageId"
Dim cmd As SqlCommand = New SqlCommand(sql, connection)
cmd.Parameters.Add("@ImageId", SqlDbType.Int).Value = context.Request.QueryString("AId")
cmd.Prepare()
Dim dr As SqlDataReader = cmd.ExecuteReader()
dr.Read()
context.Response.ContentType = "image/pjpeg"
context.Response.BinaryWrite(CType(dr("Image"), Byte()))
connection.Close()

End Sub

Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
Get
Return False
End Get
End Property

End Class

aspx page
---------
Image1.ImageUrl = "~/showHandler.ashx?AId=" + Request.QueryString("AId")
Image1.Height = "100"
Image1.Width = "100"
Image1.BorderStyle = BorderStyle.Double
Dim ImageID As Integer = Convert.ToInt32(Request.QueryString("AId"))

cmd = New SqlCommand
cmd.Connection = con
cmd.CommandType = Data.CommandType.Text
cmd.CommandText = "SELECT [Image],[Destin],[Mimetype] FROM [artical] WHERE [AId] = @ImageID"
cmd.Parameters.AddWithValue("@ImageID", ImageID)
dr = cmd.ExecuteReader()
.
.
.
.
.etc...



Author: vipul    12 Aug 2008Member Level: DiamondRating: 2 out of 52 out of 5     Points: 2

hi,
your code is write but check that your data is exit or not.


vipul,
http://dongavipul.blogspot.com

Please Rate This Answer If They Helpful

Thanks & Regards
Patel Vipul



Author: cnu    12 Aug 2008Member Level: GoldRating: 2 out of 52 out of 5     Points: 6

using System;
using System.Configuration;
using System.Web;
using System.IO;
using System.Data;
using System.Data.SqlClient;

public class ShowImage : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
string empno;
if (context.Request.QueryString["id"] != null)
empno = Convert.ToString(context.Request.QueryString["id"]);
//empno = Convert.ToInt32(context.Request.QueryString["id"]);
else
throw new ArgumentException("No parameter specified");

context.Response.ContentType = "image/jpg";
Stream strm = ShowEmpImage(empno);
byte[] buffer = new byte[4096];
int byteSeq = strm.Read(buffer, 0, 4096);

while (byteSeq > 0)
{
context.Response.OutputStream.Write(buffer, 0, byteSeq);
byteSeq = strm.Read(buffer, 0, 4096);
}
//context.Response.BinaryWrite(buffer);
}

public Stream ShowEmpImage(string empno)
{
string conn = ConfigurationManager.AppSettings["strConn"];
SqlConnection connection = new SqlConnection(conn);
string sql = "SELECT EmployeeImage FROM Employee WHERE EmployeeNumber = @EmployeeNumber";
SqlCommand cmd = new SqlCommand(sql, connection);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@EmployeeNumber", empno);
connection.Open();
object img = cmd.ExecuteScalar();
try
{
return new MemoryStream((byte[])img);
}
catch
{
return null;
}
finally
{
connection.Close();
}
}

public bool IsReusable
{
get
{
return false;
}
}


}



Post Reply

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.


Next : Page problem urgent
Previous : Cursor style is not working in mozilla
Return to Discussion Forum
Post New Message
Category: ASP.NET

Related Messages



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use