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
Bineesh Viswanath
NA
1k
777.7k
Insert Image into database using AsyncFileUpload in Asp.Net
Dec 3 2013 4:24 AM
Sir, me having a problem of insert image into database using Ajax AsyncFileUpload control. P
Please verify the code and correct and send back.
The database table to which image to be inserted is:-
tbl_Studentdetails:-
The AsyncFileUpload is implemented as:-
<td>
<ajax:AsyncFileUpload ID="FupStudent" runat="server" UploaderStyle="Traditional"
UploadingBackColor="OliveDrab" Width="212px" />
</td>
Finally, the code to add data into database is below. This code getting a error:-
the line in Red color, is where exception take place.
protected void
btnFileUpload_Click(
object
sender,
EventArgs
e)
{
try
{
string
strFilePath = FupStudent.PostedFile.FileName;
string
strFileName =
Path
.GetFileName(strFilePath);
string
strExtension =
Path
.GetExtension(strFileName);
string
strContentType = strExtension;
switch
(strExtension)
{
case ".jpeg":
strContentType = "Image/jpeg";
break;
case ".Png":
strContentType = "Image/PNG";
break;
case ".gif":
strContentType = "Image/GIF";
break;
}
if (strContentType != string.Empty)
{
Stream fStream = FupStudent.PostedFile.InputStream;
BinaryReader bReader = new BinaryReader(fStream);
Byte[] bytes = bReader.ReadBytes((int)fStream.Length);
if (sqlCon.State == ConnectionState.Closed)
{
sqlCon.Open();
}
SqlCommand cmd = new SqlCommand("StudentAdd", sqlCon); // see the stored procedure below
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@firstName", txtFirstName.Text);
cmd.Parameters.AddWithValue("@lastName", txtLastName.Text);
cmd.Parameters.AddWithValue("@Age", txtAge.Text);
cmd.Parameters.AddWithValue("@Location", txtLocation.Text);
cmd.Parameters.Add(new SqlParameter("@Photo", DbType.Binary));
cmd.Parameters["@Photo"].Value = FupStudent.PostedFile;
int inCount=cmd.ExecuteNonQuery();
if (inCount > 0)
{
HttpContext.Current.Response.Write("<script>alert('Data Inserted Successfully')</script>");
}
}
}
catch (Exception ex)
{
HttpContext.Current.Response.Write("<script>alert('Student: 06')</script>" + ex.Message);
}
ALTER PROCEDURE StudentAdd
@firstName varchar(50),
@lastName varchar(50),
@Age int,
@Location varchar(50),
@Photo VarBinary(Max)
AS
INSERT INTO tbl_StudentDetails
(firstName, lastName, Age, Location,Photo)
VALUES (@firstName, @lastName, @Age, @Location,@Photo)
Reply
Answers (
2
)
How to freez grid view of header in asp.net
Barcode generation and then print on label in c#.net