Click here to Skip to main content
15,886,258 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
protected void btn_Click(object sender, EventArgs e)
    {

        try
        {

            if (FileUpload1.HasFile)
            {
                string path = string.Concat((Server.MapPath("~/temp/" + FileUpload1.FileName)));
                FileUpload1.PostedFile.SaveAs(path);
                OleDbConnection OleDbcon = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0 ;");

                OleDbCommand cmd = new OleDbCommand("SELECT * FROM [Sheet1$]", OleDbcon);
                OleDbDataAdapter objAdapter1 = new OleDbDataAdapter(cmd);
                OleDbcon.Open();
                DbDataReader dr = cmd.ExecuteReader();


                SqlBulkCopy bulkInsert = new SqlBulkCopy(con_str);
                bulkInsert.DestinationTableName = "table1";
                bulkInsert.WriteToServer(dr);
                OleDbcon.Close();
                Array.ForEach(Directory.GetFiles((Server.MapPath("~/temp/"))), File.Delete);
                Label1.ForeColor = Color.Green;
                Label1.Text = "successfully inserted";

              

            }
            else
            {
                Label1.ForeColor = Color.Red;
                Label1.Text = "Please select the File";

            }

        }
        catch (Exception ex)
        {

            Response.Write(ex.Message);


        }
        finally
        {


        }
    }
Posted
Updated 17-Sep-14 19:21pm
v2
Comments
Sergey Alexandrovich Kryukov 18-Sep-14 1:29am    
Not clear. This is just the code dump, not a well-formulated question.
—SA
10923679 18-Sep-14 1:40am    
alright...if this is a dump code... you give a better code... to import excel file to database .
Sergey Alexandrovich Kryukov 18-Sep-14 2:18am    
Not dump code (maybe), but code dump. Do you see the difference? :-)
—SA
10923679 18-Sep-14 2:28am    
yeah i see the difference .. if you know then give how to run in server...
because this code is searching temp in local system.
if i am using this code in local then it is working.
but from server it is not.
Sergey Alexandrovich Kryukov 18-Sep-14 2:51am    
My whole point so far was that I don't understand the problem. Your code looks like working for ASP.NET server part.
What's the problem?
—SA

C#

From your question it seems that you are confuse about your input excel file path setting . so what you can do is you can creat a one appsettings in your asp.net web.config file and put the actual folder path in that key and then access in your code.


Example

XML
<appsettings>
<add key="path" value="FOLDERPath" />
</appsettings>


then use

C#
string path = ConfigurationManager.Appsettings["path"] + "\" +  FileUpload1.FileName;
 
Share this answer
 
v2
Comments
10923679 18-Sep-14 6:46am    
put the actual folder path in that key ??

what does that mean ??

you mean the server address like 10.10.248.12 ?

can you please mention it clearly...
10923679 18-Sep-14 6:47am    
string path = ConfigurationManager.AppSettings["path"] + FileUpload1.FileName;

the above code was showing error.. i use this.. but while running still m getting error.
There is no path defined in the above displayed code. The path where the Excel files are saved can be define din the Web.cong file under the <appsettings> tags. That is not accessible at the run time i.e. the path is not known to the end user.
 
Share this answer
 
Comments
10923679 18-Sep-14 6:49am    
can you please give with some example..?
You probably apply like this :

C#
var Path = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath;

if (!System.IO.Directory.Exists(Path + "\\ExcelFiles"))
{
       System.IO.Directory.CreateDirectory(Path + "\\ExcelFiles");
}

var logFilePath = Path + "\\ExcelFiles";


This will create a folder inside your server deployed path...which will always access from the server where you have placed all dll's. So your server related issue is gone.
 
Share this answer
 
v2
Comments
10923679 18-Sep-14 6:50am    
if you have some more details... can please you help me with that
KaushalJB 18-Sep-14 7:29am    
Replace FileUpload1.PostedFile.SaveAs(path); with FileUpload1.PostedFile.SaveAs(logFilePath);
using the code which is posted in answer and proceed the same way you are doing in your code...
10923679 18-Sep-14 7:33am    
it shows that .. logFilePath doesnt exist.

this is the error (exception error)
\WebSites\myproject\ExcelFiles' is denied.
KaushalJB 19-Sep-14 2:20am    
\WebSites\myproject this folders are not available in your server where you have deployed the project. So donot write this \WebSites\myproject instead only use System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath + "\ExcelFiles"
10923679 19-Sep-14 2:28am    
try
{

if (FileUpload1.HasFile)
{

var Path = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath;

if (!System.IO.Directory.Exists(Path + "\\ExcelFiles"))
{
System.IO.Directory.CreateDirectory(Path + "\\ExcelFiles");
}

var logFilePath = Path + "\\ExcelFiles";

// string path = string.Concat((Server.MapPath("~/App_Data/" + FileUpload1.FileName)));
FileUpload1.PostedFile.SaveAs(logFilePath);
OleDbConnection OleDbcon = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Path + ";Extended Properties=Excel 12.0 ;");

OleDbCommand cmd = new OleDbCommand("SELECT * FROM [Sheet1$]", OleDbcon);
OleDbDataAdapter objAdapter1 = new OleDbDataAdapter(cmd);
OleDbcon.Open();
DbDataReader dr = cmd.ExecuteReader();


SqlBulkCopy bulkInsert = new SqlBulkCopy(con_str);
bulkInsert.DestinationTableName = "table";
bulkInsert.WriteToServer(dr);
OleDbcon.Close();
Array.ForEach(Directory.GetFiles((Server.MapPath("~/App_Data/"))), File.Delete);
Label1.ForeColor = Color.Green;
Label1.Text = "successfully inserted";

please edit this... and there is a line which delete the file..


what should i do there

Array.ForEach(Directory.GetFiles((Server.MapPath("~/App_Data/"))), File.Delete);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900