TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Santhonabin
NA
51
212.9k
image upload /download
Nov 23 2010 3:44 AM
hi..
pls help me to solve error
mainpage aspx.cs
------------------------------
using
System;
using
System.Data;
using
System.Configuration;
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;
public
partial
class
_Default
: System.Web.UI.
Page
{
protected
void
Page_Load(
object
sender,
EventArgs
e)
{
}
protected
void
btnSubmit_Click(
object
sender,
EventArgs
e)
{
SqlConnection
connection =
null
;
try
{
FileUpload
img = (
FileUpload
)imgUpload;
Byte
[] imgByte =
null
;
if
(img.HasFile && img.PostedFile !=
null
)
{
//To create a PostedFile
HttpPostedFile
File = imgUpload.PostedFile;
//Create byte Array with file len
imgByte =
new
Byte
[File.ContentLength];
//force the control to load data in array
File.InputStream.Read(imgByte, 0, File.ContentLength);
}
// Insert the employee name and image into db
string
conn =
ConfigurationManager
.ConnectionStrings[
"Connection"
].ConnectionString;
connection =
new
SqlConnection
(conn);
connection.Open();
string
sql =
"INSERT INTO EmpDetails1(empname,empimg) VALUES(@enm, @eimg) SELECT @@IDENTITY"
;
SqlCommand
cmd =
new
SqlCommand
(sql, connection);
cmd.CommandType =
CommandType
.Text;
cmd.Parameters.AddWithValue(
"@enm"
, txtEName.Text.Trim());
cmd.Parameters.AddWithValue(
"@eimg"
, imgByte);
int
id =
Convert
.ToInt32(cmd.ExecuteScalar());
lblResult.Text =
String
.Format(
"Employee ID is {0}"
, id);
}
catch
{
lblResult.Text =
"There was an error"
;
}
finally
{
connection.Close();
}
}
protected
void
btnGet_Click(
object
sender,
EventArgs
e)
{
int
id = 0;
id =
Convert
.ToInt32(txtEmpid.Text);
Image1.ImageUrl =
"~/ShowImage.ashx?id="
+ id;
}
}
handler.ashx
---------------
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)
{
Int32
empno;
if
(context.Request.QueryString[
"id"
] !=
null
)
empno =
Convert
.ToInt32(context.Request.QueryString[
"id"
]);
else
throw
new
ArgumentException
(
"No parameter specified"
);
context.Response.ContentType =
"image/jpeg"
;
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);
}
}
public
Stream
ShowEmpImage(
int
empno)
{
string
conn =
ConfigurationManager
.ConnectionStrings[
"Connection"
].ConnectionString;
SqlConnection
connection =
new
SqlConnection
(conn);
string
sql =
"SELECT empimg FROM empdetails1 WHERE empid = @ID"
;
SqlCommand
cmd =
new
SqlCommand
(sql, connection);
cmd.CommandType =
CommandType
.Text;
cmd.Parameters.AddWithValue(
"@ID"
, 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
;
}
}
}
--------------------------
error
object reference not set to be instance of an object
Reply
Answers (
1
)
How to know the parent node of a treeview?
how to retrieve image