Click here to Skip to main content
15,915,755 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
public void importDataFromExcel(string excelFilePath)
    {

        string myExcelDataQuery = "select * from [Sheet1$]";
        //string myExcelDataQuery = "select Date,BankCode,Ref.Id,Name,Fees from [Sheet1$]";
        try
        {

            //string sExcelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excelFilePath + ";Extended Properties=" + "\"Excel 12.0;HDR=YES;\"";
            string sExcelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + excelFilePath + @";Extended Properties=""Excel 8.0;HDR=YES;""";
               
             //string sSqlConnectionString="Data Source=.;Initial Catalog=RecCrc;User ID=sa;Password=nic123";
           string Cn1 = ConfigurationManager.ConnectionStrings["ANIL"].ConnectionString;
           string sSqlConnectionString = ConfigurationManager.ConnectionStrings["ANIL"].ConnectionString;
             string sClearSQL = "DELETE FROM " + "ErrorBankList";
             string sClearSqlDuplicate = "DELETE FROM " + "BankList";
            SqlConnection SqlConn = new SqlConnection(Cn1);
            SqlConn.Open();
            SqlCommand SqlCmd = new SqlCommand(sClearSQL, SqlConn);
            SqlCommand SqlcmdDupl = new SqlCommand(sClearSqlDuplicate, SqlConn);
            SqlCmd.ExecuteNonQuery();
            SqlcmdDupl.ExecuteNonQuery();
            SqlConn.Close();


            OleDbConnection OleDbConn = new OleDbConnection(sExcelConnectionString);
            OleDbCommand OleDbCmd = new OleDbCommand(myExcelDataQuery, OleDbConn);
            OleDbConn.Open();

            DataSet ds1E = new DataSet();
            OleDbDataAdapter ad1E = new OleDbDataAdapter(myExcelDataQuery, OleDbConn);
            ad1E.Fill(ds1E);
            int TotalRows = ds1E.Tables[0].Rows.Count;
            if (ds1E.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds1E.Tables[0].Rows.Count; i++)
                {
                    string Sno = ds1E.Tables[0].Rows[i][0].ToString().Replace("'", "");
                    string BranchCd = ds1E.Tables[0].Rows[i][1].ToString().Replace("'", "");
                    string BranchNm = ds1E.Tables[0].Rows[i][2].ToString().Replace("'", "");
                    string Name = "State Bank Of Mysore";
                    string NameOfBank = Name +","+ BranchNm;
                    if (Sno.Trim() == "" || BranchCd.Trim() == "" || BranchNm.Trim() == "")
                    {
                    }
                    else if (Sno.Trim() != "" || BranchCd.Trim() != "" || BranchNm.Trim() != "")
                    {
                       try{
                        string Query = "insert into BankList values ('" + Sno.Trim() + "','" + BranchCd.Trim() + "','" +  BranchNm.Trim() + "') ";
                        SqlConnection Cn = new SqlConnection(sSqlConnectionString);
                        Cn.Open();
                        SqlCommand cmdT = new SqlCommand(Query, Cn);
                        cmdT.ExecuteNonQuery();
                        Cn.Close();
                       }  
                        catch(Exception ex)
                       {
                           string Query = "insert into ErrorBankList values ('" + Sno.Trim() + "','" + BranchCd.Trim() + "','" + BranchNm.Trim() + "') ";
                        SqlConnection Cn = new SqlConnection(sSqlConnectionString);
                        Cn.Open();
                        SqlCommand cmdT = new SqlCommand(Query, Cn);
                        cmdT.ExecuteNonQuery();
                        Cn.Close();
                        }



                        
                    }
                }
 }
            LblMsg.Text = "Data saved without errors";
            OleDbConn.Close();
            LoadDetails_Grid();
            
            
        }
        catch (Exception ex)
        {
            string Error = ex.ToString();
            //Save_Error(Error);
           // Response.Write(ex.ToString());
            LblMsg.Text = "Invalied Excel";
            GridView1.Visible = false;
        }
    }


But Iam Getting Exception:The Microsoft Office Access database engine cannot open or write to the file ''. It is already opened exclusively by another user, or you need permission to view and write its data.

Can anyone Suggest me.
Posted
Updated 1-Sep-11 20:51pm
v2

1 solution

Make sure the file is not already open in another Excel window.
 
Share this answer
 
Comments
Anil Honey 206 2-Sep-11 2:54am    
NO Its Not Open i have checked but the Exception is coming

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