Insert Data using Uploading Excel file and upload image and set water mark

Demo.aspx
  1. <body>  
  2.     <form id="form1" runat="server">  
  3.         <div>  
  4.             <table>  
  5.                 <tr>  
  6.                     <td>  
  7.                         <asp:FileUpload ID ="fu" runat ="server" />  
  8.                     </td>  
  9.                     <td>  
  10.                         <asp:Button ID ="btnupload" runat ="server" Text="Upload File"  
  11. onclick="btnupload_Click" />  
  12.                     </td>  
  13.                 </tr>  
  14.             </table>  
  15.         </div>  
  16.     </form>  
  17. </body>  

Demo.aspx .cs
  1. DataTable dt = new DataTable();  
  2.   
  3. protected void btnupload_Click(object sender, EventArgs e)  
  4. {  
  5.     if (fu.HasFile)  
  6.     {  
  7.         if (fu.PostedFile.ContentType == "application/vnd.ms-excel" || fu.PostedFile.ContentType == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")  
  8.         {  
  9.             string fileName = Path.Combine(Server.MapPath("~/localization"), Guid.NewGuid().ToString() + Path.GetExtension(fu.PostedFile.FileName));  
  10.             fu.PostedFile.SaveAs(fileName);  
  11.   
  12.             string conString = "";  
  13.             string ext = Path.GetExtension(fu.PostedFile.FileName);  
  14.             if (ext.ToLower() == ".xls")  
  15.             {  
  16.                 conString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";  
  17.             }   
  18.             else if (ext.ToLower() == ".xlsx")   
  19.             {  
  20.                 conString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";  
  21.             }  
  22.             string query = "Select * from [Sheet1$]";  
  23.             OleDbConnection con = new OleDbConnection(conString);  
  24.             OleDbDataAdapter data = new OleDbDataAdapter(query, con);  
  25.             data.Fill(dt);  
  26.             int i = 0;  
  27.             //File_Upload file = new File_Upload();  
  28.             for (i = 0; i < dt.Rows.Count; i++)  
  29.             {  
  30.                 string name = dt.Rows[i]["name"].ToString();  
  31.                 string email = dt.Rows[i]["email"].ToString();  
  32.                 string pass = dt.Rows[i]["Password"].ToString();  
  33.                 string mobile = dt.Rows[i]["mobile"].ToString();  
  34.                 string state = dt.Rows[i]["state"].ToString();  
  35.                 string city = dt.Rows[i]["city"].ToString();  
  36.                 string photo = dt.Rows[i]["Photo"].ToString();  
  37.   
  38.                 string[] pathArr = photo.Split('\\');  
  39.                 string[] fileArr = pathArr.Last().Split('.');  
  40.                 string fName = fileArr[0].ToString();  
  41.                 Random rnd = new Random();  
  42.                 string fn = fName + "_" + rnd.Next(111, 999) + "_" + rnd.Next(111, 999) + ".jpg";  
  43.                 string path = "~/photo/" + fn;  
  44.                 string pat = Server.MapPath(path);  
  45.                 Wattermark w = new Wattermark();  
  46.                 Dal odal = new Dal();  
  47.                 System.Drawing.Image image = System.Drawing.Image.FromFile(photo);  
  48.                 Graphics graphics = Graphics.FromImage(image);  
  49.                 Font font = new Font("Times New Roman", 42.0f);  
  50.                 PointF point = new PointF(10, 10);  
  51.                 graphics.DrawString("Tusharsangani", font, Brushes.Red, point);  
  52.                 image.Save(pat);  
  53.                 odal.fetch("insert into Registeruser (name,E_id,password,mobile,state,city,photo) values('" + name + "','" + email + "','" + pass + "','" + mobile + "','" + state + "','" + city + "','" + path + "')");  
  54.             }  
  55.         }  
  56.     }   
  57.     else   
  58.     {  
  59.   
  60.     }  
  61. }  
Ebook Download
View all
milton
Read by 0 people
Download Now!
Learn
View all