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
youhana metry
NA
58
3.6k
Facing Problem When Upload File Using Asp.net
Nov 26 2018 2:44 PM
iam facing problem when i uplad file to sql database it gave me the following message
can you please push your help
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 20: { Line 21: Line 22: string filename = Path.GetFileName(FileUpload1.PostedFile.FileName); Line 23: string extension = Path.GetExtension(filename); Line 24: string contentType = FileUpload1.PostedFile.ContentType;
the code that i use
protected
void
Button1_Click(
object
sender, EventArgs e)
{
string
filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
string
extension = Path.GetExtension(filename);
string
contentType = FileUpload1.PostedFile.ContentType;
HttpPostedFile file = FileUpload1.PostedFile;
byte
[] document =
new
byte
[file.ContentLength];
file.InputStream.Read(document, 0, file.ContentLength);
if
((extension ==
".pdf"
) || (extension ==
".doc"
) || (extension ==
".docx"
) || (extension ==
".xls"
))
//extension
{
if
(file.ContentLength <= 31457280)
//size
{
//Insert the Data in the Table
SCP.Insert_Script(filename, txtfilename.Text.Trim(), extension, contentType, document, document.Length);
}
else
{ Response.Write(
"Inavlid File size"
);
return
; }
}
else
{
Response.Write(
"Inavlid File"
);
return
;
}
}
for
business layer I Used the following Code
public
void
Insert_Script(
string
_Name_File,
string
_DisplayName,
string
_Extension,
string
_ContentType,
byte
[] _FileData,
int
_FileSize)
{
DAL_CCRMUI.DataAccessLayer Dal =
new
DAL_CCRMUI.DataAccessLayer();
Dal.Open();
SqlParameter[] param =
new
SqlParameter[6];
param[0] =
new
SqlParameter(
"@Name_File"
, SqlDbType.VarChar,100)
{
Value =_Name_File
};
param[1] =
new
SqlParameter(
"@DisplayName"
, SqlDbType.VarChar,50)
{
Value = _DisplayName
};
param[2] =
new
SqlParameter(
"@Extension"
, SqlDbType.VarChar,10)
{
Value = _Extension
};
param[3] =
new
SqlParameter(
"@ContentType"
, SqlDbType.VarChar,200)
{
Value = _ContentType
};
param[4] =
new
SqlParameter(
"@FileData"
, SqlDbType.VarBinary, 5000)
{
Value = _FileData
};
param[5] =
new
SqlParameter(
"@FileSize"
, SqlDbType.BigInt)
{
Value = _FileSize
};
Dal.ExecuteProcedure(
"Insert_Document"
, param);
Dal.Close();
}
Reply
Answers (
4
)
What all are the report tools in asp.net ?
Page not displaying correctly in Mobile view