Click here to Skip to main content
15,907,231 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="samp.aspx.cs" Inherits="samp._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table>
            <tr>
                <td><span style="color:#FF0000">*</span>Attach Excel File</td>
                <td><asp:FileUpload ID="fileuploadExcel" runat="server" /></td>
            </tr>
            <tr>
                <td></td>
                <td><asp:Button ID="btnSend" runat="server" Text="Export" OnClick="btnSend_click"></asp:Button></td>
            </tr>
        </table>
      <asp:GridView ID="GridView1" runat="server"></asp:GridView>
    </div>
    </form>
</body>
</html>




Code Behind is
C#
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Data.SqlClient;

namespace samp
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void btnSend_click(object sender, EventArgs e)
        {
            String strConnection = "Data Source=MANIADSBHI-PC;Initial Catalog=master;Integrated Security=True";
            //file upload path
            string path = fileuploadExcel.PostedFile.FileName;
            //Create connection string to Excel work book
            string excelConnectionString = @"Provider=SQLOLEDB.1;Data Source=" + path + ";Extended Properties=Excel 7.0;Persist Security Info=True";
            //Create Connection to Excel work book
            OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);
            //Create OleDbCommand to fetch data from Excel
            OleDbCommand cmd = new OleDbCommand("Select [sno],[fname],[lname],[mobnum],[city],[zip] from emp [Sheet1$]", excelConnection);
            excelConnection.Open();
            OleDbDataReader dReader;
            dReader = cmd.ExecuteReader();
            SqlBulkCopy sqlBulk = new SqlBulkCopy(strConnection);
            //Give your Destination table name
            sqlBulk.DestinationTableName = "emp";
            sqlBulk.WriteToServer(dReader);
            excelConnection.Close();
        }
    }
}


Now i got the error on code behind line no:30... ie:- excelCOnnenction.open();
Pls anyone solve the error..
Posted
Updated 5-Jun-14 1:25am
v2
Comments
[no name] 5-Jun-14 6:44am    
Since you are making us guess at which error you are seeing, my guess is that excelConnectionString is wrong.
Member 10463904 5-Jun-14 6:49am    
yes there was error on excel connection... i got a error as invalid connection string attribute.. I dont know how to resolve this... Please help me Wes Aday..
[no name] 5-Jun-14 7:02am    
www.connectionstrings.com

I think you have specified an incorrect Provider for your excel connectionstring.

Excel connections are usually made use an ACE or JET provider.

Try changing your connectionstring to something like:
C#
string excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 8.0";


or

C#
string excelConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties=Excel 8.0;";


Hope it helps.
 
Share this answer
 
Hi,
String strExcelConn = "Provider=Microsoft.ACE.OLEDB.12.0;" +
"Data Source=" + Server.MapPath("your .xlsx file path;") + ";"
+ "Extended Properties='Excel 12.0;HDR=Yes'";


Good Luck
 
Share this answer
 

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