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
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Insert Data using Uploading Excel file and upload image and set water mark
Tushar Sangani
Dec 11, 2017
19.1
k
0
4
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
Insert Data in data base with image and set the water mark in Image
Demo.aspx
<
body
>
<
form
id
=
"form1"
runat
=
"server"
>
<
div
>
<
table
>
<
tr
>
<
td
>
<
asp:FileUpload
ID
=
"fu"
runat
=
"server"
/>
</
td
>
<
td
>
<
asp:Button
ID
=
"btnupload"
runat
=
"server"
Text
=
"Upload File"
onclick
=
"btnupload_Click"
/>
</
td
>
</
tr
>
</
table
>
</
div
>
</
form
>
</
body
>
Demo.aspx .cs
DataTable dt =
new
DataTable();
protected
void
btnupload_Click(
object
sender, EventArgs e)
{
if
(fu.HasFile)
{
if
(fu.PostedFile.ContentType ==
"application/vnd.ms-excel"
|| fu.PostedFile.ContentType ==
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
)
{
string
fileName = Path.Combine(Server.MapPath(
"~/localization"
), Guid.NewGuid().ToString() + Path.GetExtension(fu.PostedFile.FileName));
fu.PostedFile.SaveAs(fileName);
string
conString =
""
;
string
ext = Path.GetExtension(fu.PostedFile.FileName);
if
(ext.ToLower() ==
".xls"
)
{
conString =
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
+ fileName +
";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\""
;
}
else
if
(ext.ToLower() ==
".xlsx"
)
{
conString =
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source="
+ fileName +
";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\""
;
}
string
query =
"Select * from [Sheet1$]"
;
OleDbConnection con =
new
OleDbConnection(conString);
OleDbDataAdapter data =
new
OleDbDataAdapter(query, con);
data.Fill(dt);
int
i = 0;
//File_Upload file =
new
File_Upload();
for
(i = 0; i < dt.Rows.Count; i++)
{
string
name = dt.Rows[i][
"name"
].ToString();
string
email = dt.Rows[i][
"email"
].ToString();
string
pass = dt.Rows[i][
"Password"
].ToString();
string
mobile = dt.Rows[i][
"mobile"
].ToString();
string
state = dt.Rows[i][
"state"
].ToString();
string
city = dt.Rows[i][
"city"
].ToString();
string
photo = dt.Rows[i][
"Photo"
].ToString();
string
[] pathArr = photo.Split(
'\\'
);
string
[] fileArr = pathArr.Last().Split(
'.'
);
string
fName = fileArr[0].ToString();
Random rnd =
new
Random();
string
fn = fName +
"_"
+ rnd.Next(111, 999) +
"_"
+ rnd.Next(111, 999) +
".jpg"
;
string
path =
"~/photo/"
+ fn;
string
pat = Server.MapPath(path);
Wattermark w =
new
Wattermark();
Dal odal =
new
Dal();
System.Drawing.Image image = System.Drawing.Image.FromFile(photo);
Graphics graphics = Graphics.FromImage(image);
Font font =
new
Font(
"Times New Roman"
, 42.0f);
PointF point =
new
PointF(10, 10);
graphics.DrawString(
"Tusharsangani"
, font, Brushes.Red, point);
image.Save(pat);
odal.fetch(
"insert into Registeruser (name,E_id,password,mobile,state,city,photo) values('"
+ name +
"','"
+ email +
"','"
+ pass +
"','"
+ mobile +
"','"
+ state +
"','"
+ city +
"','"
+ path +
"')"
);
}
}
}
else
{
}
}
Insert Data using Uploading Excel file and upload image and set water mark
Next Recommended Reading
Convert PDF File Into Image File(png,jpg,jpeg) Using GhostScript